- 简单工厂
- 工厂模式
- 抽象工厂
简单(静态)工厂
- 简单工厂是工厂模式的一种,属于创建型模式,是工厂模式中最简单实用的一种模式
- 定义一个创建对象的类,由这个类来封装创建对象的行为
- 当我们大量创建某类的对象时,就会用到该模式
以生产汽车为例,目前有五菱和特斯拉两个品牌的车,代码如下
//特斯拉
public class Tesla implements Car{
public Tesla() {
System.out.println("生产了一辆特斯拉");
}
}//五菱
public class Wuling implements Car{
public Wuling(){
System.out.println("生产了一辆五菱");
}
}如果没有一家工厂生产这两种车,那么消费者只能自己制造,如果车中有很多的零件,那么每个消费者都要耗时耗力的去新建
public class Consumer {
public static void main(String[] args) {
new Wuling();
new Tesla();
}
}此时,我们将所有的零件与组装全都交给工厂,消费者只需要告诉工厂,我要的汽车型号,工厂会自动把组装好的汽车提供给消费者
以下是工厂的代码
public class CarFactory {
public static Car getCar(String name) {
if ("五菱".equals(name)) {
return new Wuling();
} else if ("特斯拉".equals(name)) {
return new Tesla();
} else {
return null;
}
}
}
有了工厂以后,消费者只需要知道自己想要的型号,就可以得到车的对象。
public class Consumer {
public static void main(String[] args) {
CarFactory.getCar("五菱");
CarFactory.getCar("特斯拉");
}
}以上就是简单工厂,在对象参数特别多的情况下,新建对象会很费事,统一交给工厂赋值,就只需要费事一次。
弊端:如果我要增加新的产品,比如再生产大众的车型,必然要去工厂的生产方法中修改逻辑代码,违反了开闭原则
~~~~~~~~~~~~~~~~~~~~我是模式的分割线~~~~~~~~~~~~~~~~~~~~~~~~~
工厂模式
为了完全的符合开闭原则,就有了下面的工厂模式
此时,为每一个汽车品牌都建立一个一家工厂,消费者不再是从一家工厂抽取任意车型,而是要到指定的工厂抽取他生产的唯一车型。
新增生产一个品牌,所以现在有三个车型
public class Tesla implements Car{
public Tesla() {
System.out.println("生产了一辆特斯拉");
}
}
public class Wuling implements Car{
public Wuling(){
System.out.println("生产了一辆五菱");
}
}
public class Volkswagen implements Car{
public Volkswagen() {
System.out.println("生产了一辆大众");
}
}为这三个品牌分别建立三家工厂
//五菱
public class WulingFactory implements CarFactory{
@Override
public Car getCar() {
// TODO Auto-generated method stub
return new Wuling();
}
}
//大众
public class VolkswagenFactory implements CarFactory{
@Override
public Car getCar() {
// TODO Auto-generated method stub
return new Volkswagen();
}
}//特斯拉
public class TeslaFactory implements CarFactory{
@Override
public Car getCar() {
// TODO Auto-generated method stub
return new Tesla();
}
}
此时,消费者去寻找指定工厂
public class Consumer {
public static void main(String[] args) {
new TeslaFactory().getCar();
new WulingFactory().getCar();
new VolkswagenFactory().getCar();
}
}
在这种结构下,哪怕再新增品牌,也无需修改原有的生产逻辑代码,但是会导致类过于臃肿,为了实现开闭原则,付出的代价略大。
从设计原则来说,选择工厂模式
从实际业务来说,选择简单工厂
~~~~~~~~~~~~~~~~~~~~我是模式的分割线~~~~~~~~~~~~~~~~~~~~~~~~~
抽象工厂
抽象工厂是创建其他工厂的工厂
一辆车需要有很多的零件,比如说引擎,轮胎等等,抽象工厂可以制约他的子类工厂生产必须的零件。
现在特斯拉工厂和五菱工厂都需要生产出他们自己的引擎和轮胎
public class TeslaTyre implements ITyre{
public TeslaTyre() {
System.out.println("特斯拉的轮胎被生产");
}
}
public class TeslaEngine implements IEngineProduct {
public TeslaEngine() {
System.out.println("特斯拉的引擎被生产");
}
}
public class WulingEngine implements IEngineProduct{
public WulingEngine() {
System.out.println("五菱汽车的引擎被生产");
}
}
public class WulingTyre implements ITyre{
public WulingTyre() {
System.out.println("五菱汽车的轮胎被生产");
}
}此时,我们需要这两个品牌各自的工厂去生产
//特斯拉工厂,实现了超级工厂的方法
public class TeslaFactory implements SuperFactory{
@Override
public IEngineProduct getEngine() {
// TODO Auto-generated method stub
return new TeslaEngine();
}
@Override
public ITyre getTyre() {
// TODO Auto-generated method stub
return new TeslaTyre();
}
}//五菱工厂,实现了超级工厂的方法public class WulingFactory implements SuperFactory{
@Override
public IEngineProduct getEngine() {
// TODO Auto-generated method stub
return new WulingEngine();
}
@Override
public ITyre getTyre() {
// TODO Auto-generated method stub
return new WulingTyre();
}
}超级工厂
public interface SuperFactory {
//制约其他的工厂,必须要有引擎的生产线
IEngineProduct getEngine();
//制约其他的工厂,必须要有轮胎的生产线
ITyre getTyre();
}消费
public class Consumer {
public static void main(String[] args) {
new TeslaFactory().getEngine();
}
}如果我们需要一个大众的工厂,直接创建大众工厂类,实现超级工厂方法即可,所以说抽象工厂是创建工厂的工厂,但是一旦需要增加产品类,比如新增一条玻璃产线,需要修改的代码就会变得很多,由此得出,抽象工厂最好应用在产品类稳定的情况下。