简单工厂(Simple Factory)
工厂方法(Factory Method)
抽象工厂(Abstract Factory)
1. 简单工厂
- 产品相关
- Product总接口
-
public interface Product{}//声明类所需继承的共同接口,也可以是抽象类
-
- 产品实现类:产品A和产品B
-
public class ProductA implements Product { public ProductA() { System.out.println("ProductA"); } } public class ProductB implements Product { public ProductB() { System.out.println("ProductB"); } }
-
- Product总接口
- 工厂相关
-
public class Factory { //可以在工厂类中添加任何你所需要的逻辑 public static Product create(String str){ if(str.equalsIgnoreCase("ProductA")){ //生成ProductA return new ProductA(); } else if(str.equalsIgnoreCase("ProductB")){ //生成ProductB return new ProductB(); } return null; } }
-
- 客户端创建
-
public class Client { public static void main(String[] args) { //调用Factory的静态方法生成所要的类 Factory.create("productA"); Factory.create("ProductB"); } }
-
简单工厂
- 优点:简单
- 缺点:违反了设计模式的 ocp原则(对扩展开放,对修改关闭)。即:当我们给类增加新功能的时候,尽量不修改代码,或者少修改代码。
- eg. 如果需要增加ProductC产品,则需要新增if-else的判断
2. 工厂方法
- 产品相关
- Product总接口
-
public interface Product{}//声明类所需继承的共同接口,也可以是抽象类
-
- 产品A和产品B
-
public class ProductA implements Product { public ProductA() { System.out.println("ProductA"); } } public class ProductB implements Product { public ProductB() { System.out.println("ProductB"); } }
-
- Product总接口
- 工厂相关
- Factory总接口
-
public interface Factory { //声明产生产品类的方法 public Product createProduct(); }
-
- Factory实现类:FactoryA 和 FactoryB
-
public class FactoryA implements Factory { //实现工厂类的方法生成产品类A public Product createProduct(){ return new ProductA(); } } public class FactoryB implements Factory { //实现工厂类的方法生成产品类B public Product createProduct(){ return new ProductB(); } }
-
- Factory总接口
- 客户端创建
-
public class Client { public static void main(String[] args) { Factory factory; factory = new FactoryA(); factory.createProduct(); factory = new FactoryB(); factory.createProduct(); } }
-
工厂方法
- 优点:实现了“开发-封闭”原则,无论增加多少产品类,不用修改原来的代码,只需增加Product和Factory类
- 缺点:如果产品过多,需要增加的工厂类也会过多。同时只能针对一类产品,如果需要组合,则无法实现
3. 抽象工厂
抽象工厂解决了上面两种只能针对一类产品的问题,这里为不同产品附加对应的产地,也就是说不同Product的Location不同
- 产品相关
- Product总接口
-
public interface Product{}//声明类所需继承的共同接口,也可以是抽象类
-
- 产品实现类:产品A和产品B
-
public class ProductA implements Product { public ProductA() { System.out.println("ProductA"); } } public class ProductB implements Product { public ProductB() { System.out.println("ProductB"); } }
-
- Product总接口
- 产地相关
- Location总接口
-
public interface Location{}//声明类所需继承的共同接口,也可以是抽象类
-
- 产地实现类:产地X和产地Y
-
public class LocationX implements Location { public LocationX() { System.out.println("LocationX"); } } public class LocationY implements Location { public LocationY() { System.out.println("LocationY"); } }
-
- Location总接口
- 工厂相关
- Factory总接口
-
public interface AbstractFactory{ public Product createProduct(); public Location createLocation(); }
-
- 对应Factory的实现类:FactoryA 和 FactoryB
-
public class FactoryA implements AbstractFactory { @Override public Product createProduct(){ return new ProductA(); } @Override public Gift createLocation(){ return new LocationX(); } } public class FactoryB implements AbstractFactory { @Override public Product createProduct(){ return new ProductB(); } @Override public Gift createLocation(){ return new LocationY(); } }
-
- Factory总接口
- 客户端创建
-
public class Client { public static void main(String[] args) { AbstarctFactory factory; factory = new FactoryA(); factory.createProduct(); factory.createLocation(); factory = new FactoryB(); factory.createProduct(); factory.createLocation(); } }
-
类图:待补充
抽象工厂
- 抽象工厂模式可以将简单工厂模式和工厂方法模式进行整合,在Factory中多加了一层
- 优点:程序员可以根据创建对象类型使用对应的工厂子类。这样将单个的简单工厂类变成了工厂簇,更利于代码的维护和扩展。
- 缺点:产品族扩展非常困难,要增加一个系列的某一产品,既要在抽象的 Creator 里加代码,又要在具体的里面加代码