定义:
定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法使一个类的实例化延迟到其子类。
结构:(书中图,侵删)
一个工厂的抽象接口
若干个具体的工厂类
一个需要创建对象的抽象接口
若干个所需创建的对象的类
较之简单工厂方法,多了一层抽象——将工厂也抽象了;由原来的一个万能工厂变为现在的若干个各司其职的工厂。
![](https://img2018.cnblogs.com/blog/1532854/201904/1532854-20190409195810631-1160049641.png)
![](https://img2018.cnblogs.com/blog/1532854/201904/1532854-20190409195824699-1830239134.png)
导致的结果就是:原本新增一个要创建的对象需要直接修改万能工厂类,增加判断,违反了开放-封闭原则;
现在工厂相关代码不用改,只需要新增工厂类即可,同时将判断转移给了客户端。
实例:
既然是和简单工厂相对比,就还是延用之前计算器的例子:(同样,为了代码简洁不考虑输入不合法等代码健壮性)
抽象工厂接口:
package designpattern.factorymethod; import designpattern.staticfactorymethod.Calculate; public interface CalculateFactory { Calculate create(); }
若干工厂接口:
加
package designpattern.factorymethod; import designpattern.staticfactorymethod.Calculate; import designpattern.staticfactorymethod.Plus; public class PlusFactory implements CalculateFactory{ @Override public Calculate create() { return new Plus(); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package designpattern.factorymethod; import designpattern.staticfactorymethod.Calculate; import designpattern.staticfactorymethod.Minus; public class MinusFactory implements CalculateFactory { @Override public Calculate create() { return new Minus(); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package designpattern.factorymethod; import designpattern.staticfactorymethod.Calculate; import designpattern.staticfactorymethod.Multiply; public class MultiplyFactory implements CalculateFactory { @Override public Calculate create() { return new Multiply(); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package designpattern.factorymethod; import designpattern.staticfactorymethod.Calculate; import designpattern.staticfactorymethod.Divide; public class DivideFactory implements CalculateFactory { @Override public Calculate create() { return new Divide(); } }
抽象计算类及若干具体计算类:(延用之前简单工厂方法的代码)
抽象类:
package designpattern.staticfactorymethod; public abstract class Calculate { public double num1; public double num2; Calculate() { } Calculate(double num1, double num2) { this.num1 = num1; this.num2 = num2; } public abstract double calculate(); }
加:
package designpattern.staticfactorymethod; public class Plus extends Calculate { public Plus() { } Plus(double num1, double num2) { super(num1, num2); } @Override public double calculate() { return num1 + num2; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package designpattern.staticfactorymethod; public class Minus extends Calculate { public Minus() { } Minus(double num1, double num2) { super(num1, num2); } @Override public double calculate() { return num1 - num2; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package designpattern.staticfactorymethod; public class Multiply extends Calculate { public Multiply() { } Multiply(double num1, double num2) { super(num1, num2); } @Override public double calculate() { return num1 * num2; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package designpattern.staticfactorymethod; public class Divide extends Calculate { public Divide() { } Divide(double num1, double num2) { super(num1, num2); } @Override public double calculate() { return num1 / num2; } }
客户端:
package designpattern.factorymethod; import java.util.Scanner; import designpattern.staticfactorymethod.Calculate; public class Client { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入一个数字"); double num1 = scanner.nextDouble(); System.out.println("请输入一个运算符:+、-、*、/"); String operator = scanner.next();// 不能用nextLine(),会把上一个回车给吸收 System.out.println("请输入一个数字"); double num2 = scanner.nextDouble(); CalculateFactory calculateFactory = null; switch (operator) { case "+": calculateFactory = new PlusFactory(); break; case "-": calculateFactory = new MinusFactory(); break; case "*": calculateFactory = new MultiplyFactory(); break; case "/": calculateFactory = new DivideFactory(); break; default: break; } Calculate calculate = calculateFactory.create(); calculate.num1 = num1; calculate.num2 = num2; System.out.println(calculate.calculate()); scanner.close(); } }
总结:
工厂方法模式就是在简单工厂方法模式的基础上进一步抽象,使其符合开放-封闭原则。
个人理解,在轻量级系统,或者工厂所能创建的东西较少可能修改的情况下,可以考虑使用简单工厂模式;
否则,需要使用工厂方法模式。