设计模式 --工厂模式

设计模式 --工厂模式,第1张

设计模式 --工厂模式

Go to 简书

1. 简单工厂
  • 简单工厂只有一个工厂
  • 产品只有一种
  • 根据类型去判断生产什么产品
工厂生产对象 – 俱乐部
public interface FootballClub {

    void fighting();

}
俱乐部 1 – 曼联
public class ManchesterUnitedClub implements FootballClub{
    @Override
    public void fighting() {
        System.out.println("MUN go go go");
    }
}
俱乐部 2 – 拜仁
public class FCBayern implements FootballClub {
    @Override
    public void fighting() {
        System.out.println("FCB fighting...");
    }
}
工厂类
public class FootballClubFactory {

    public FootballClub getFootballClub(Class c) {
        FootballClub footballClub = null;
        try {
            footballClub = (FootballClub) Class.forName(c.getName()).newInstance();
        } catch (IllegalAccessException | ClassNotFoundException | InstantiationException e) {
            e.printStackTrace();
        }
        return footballClub;
    }

    public FootballClub getFootballClub(String type) {
        if ("MUN".equals(type)) {
            return new ManchesterUnitedClub();
        } else if ("FCB".equals(type)) {
            return new FCBayern();
        }
        return null;
    }

}
测试
public class Test {
    public static void main(String[] args) {
        FootballClubFactory factory = new FootballClubFactory();
        FootballClub club = factory.getFootballClub("MUN");
        club.fighting();
        club = factory.getFootballClub(FCBayern.class);
        club.fighting();
    }
}

运行结果

MUN go go go
FCB fighting...
2. 工厂方法
  • 也是只有一种产品
  • 每个类型的产品都有自己的工厂
抽象产物 – 球衣
public abstract class Jersey {
    public abstract void keepWarm();
}
尤文球衣
public class JuvJersey extends Jersey {
    @Override
    public void keepWarm() {
        System.out.println("尤文:这球衣不保暖啊...");
    }
}
巴萨球衣
public class FCBarcelonaJersey extends Jersey {
    @Override
    public void keepWarm() {
        System.out.println("巴萨:球衣哪有保暖的...");
    }
}
球衣抽象工厂
public abstract class JerseyFactory {
    public abstract Jersey produce();
}
尤文球衣工厂
public class JuvJerseyFactory extends JerseyFactory {
    @Override
    public Jersey produce() {
        return new JuvJersey();
    }
}
巴萨球衣工厂
public class FCBarcelonaJerseyFactory extends JerseyFactory {
    @Override
    public Jersey produce() {
        return new FCBarcelonaJersey();
    }
}
测试类
public class Test {
    public static void main(String[] args) {
        JerseyFactory jerseyFactory = new JuvJerseyFactory();
        Jersey jersey = jerseyFactory.produce();
        jersey.keepWarm();

        jerseyFactory = new FCBarcelonaJerseyFactory();
        jersey = jerseyFactory.produce();
        jersey.keepWarm();
    }
}

运行结果

尤文:这球衣不保暖啊...
巴萨:球衣哪有保暖的...
3. 抽象工厂
  • 每个工厂生产多种产品(围巾、帽子)
  • 每个工厂生产的产品属于同一品牌/阵营(利物浦、巴黎)
工厂产品1 – 围巾
public abstract class Scarf {
    public abstract void keepWarm();
}
工厂产品2 – 帽子
public abstract class Hat {
    public abstract void keepWarm();
}
利物浦产品
public class LiverpoolHat extends Hat {
    @Override
    public void keepWarm() {
        System.out.println("利物浦帽子:只护头");
    }
}
public class LiverpoolScarf extends Scarf {
    @Override
    public void keepWarm() {
        System.out.println("利物浦围巾:还是只护头...");
    }
}
巴黎产品
public class ParisHat extends Hat {
    @Override
    public void keepWarm() {
        System.out.println("巴黎帽子:也是只护头!!?");
    }
}
public class ParisScarf extends Scarf {
    @Override
    public void keepWarm() {
        System.out.println("巴黎围巾:应该造点衣服...");
    }
}
俱乐部产品工厂

每个工厂都应该生产帽子和围巾两种产品

public interface ClubFactory {
    Scarf getScarf();
    Hat getHat();
}
利物浦工厂
public class LiverpoolFactory implements ClubFactory {
    @Override
    public Scarf getScarf() {
        return new LiverpoolScarf();
    }

    @Override
    public Hat getHat() {
        return new LiverpoolHat();
    }
}
巴黎工厂
public class ParisFactory implements ClubFactory {
    @Override
    public Scarf getScarf() {
        return new ParisScarf();
    }

    @Override
    public Hat getHat() {
        return new ParisHat();
    }
}
测试
public class Test {
    public static void main(String[] args) {
        ClubFactory clubFactory = new LiverpoolFactory();
        Hat hat = clubFactory.getHat();
        Scarf scarf = clubFactory.getScarf();
        hat.keepWarm();
        scarf.keepWarm();

        clubFactory = new ParisFactory();
        scarf = clubFactory.getScarf();
        hat = clubFactory.getHat();
        hat.keepWarm();
        scarf.keepWarm();
    }
}

运行结果

利物浦帽子:只护头
利物浦围巾:还是只护头...
巴黎帽子:也是只护头!!?
巴黎围巾:应该造点衣服...

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5637076.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存