赞
踩
工厂设计模式属于创建型模式,它提供了一种创建对象的最佳方式。工厂模式提供了一种创建对象的方式,而无需指定要创建的具体类。工厂模式属于创建型模式,它在创建对象时提供了一种封装机制,将实际创建对象的代码与使用代码分离。
面相对象编程中,万物皆对象。每个对象如果都通过关键字new创建,则会导致对象与其调用者耦合严重。假如更换要new的对象,则所有new对象的地方都需要改变代码,这显然违背了“开-闭原则”。如果使用工厂来生产对象,我们只需要和工厂打交道,彻底和对象解耦,如果更换对象,只需要在工厂里更换对象,而不需要再所有的地方进行更换,这样达到了与对象解耦的目的。工厂模式最大优点就是解耦。
简单工厂模式根据传入的参数,决定创建哪个类型的实例。简单工厂模式本着高内聚低耦合的原则,将系统的逻辑部分和功能分开。
首先当然是在你需要new一个类的对象的时候,此时各种状况出现啦:
QQ 换皮肤,一整套一起换。
生成不同操作系统的程序。

简易计算器为例,需求:输入两个数和运算符,得到结果。
- public abstract class Operation {
- protected abstract double compute (double number1, double number2) throws Exception;
- }
- // 加法
- public class OperationAdd extends Operation{
- @Override
- protected double compute(double number1, double number2) {
- return number1 + number2;
- }
- }
-
-
-
- // 减法
- public class OperationSub extends Operation{
- @Override
- protected double compute(double number1, double number2) {
- return number1 - number2;
- }
- }
-
-
- // 乘法
- public class OperationMul extends Operation{
- @Override
- protected double compute(double number1, double number2) {
- return number1 * number2;
- }
- }
-
-
- // 除法
- public class OperationDiv extends Operation{
- @Override
- protected double compute(double number1, double number2) throws Exception {
- if(number2 == 0){
- throw new Exception("除数不能为0!");
- }
- return number1 / number2;
- }
- }

这里对简单工厂创建产品进行了优化,可以通过缓存、配置文件、反射和泛型等技术了优化。
如果运算业务需要扩展,需要创建具体运算类即可,不需要再修改简单工厂类了。
- public class OperationFactory {
-
- private static final Map<String, Operation> cachedParsers = new HashMap<>();
-
- static {
- cachedParsers.put("+", new OperationAdd());
- cachedParsers.put("-", new OperationSub());
- cachedParsers.put("*", new OperationMul());
- cachedParsers.put("/", new OperationDiv());
- }
-
- public static Operation createOperate0(String operate) {
- if (operate == null || operate.isEmpty()) {
- return null;//返回null还是IllegalArgumentException全凭你自己说了算
- }
- return cachedParsers.get(operate);
- }
-
- /**
- * createOperate0运用缓存技术,节省内存和对象创建的时间
- * createOperate0 和 createOperate1 同理
- */
- public static Operation createOperate1(String operate){
- Operation operation = null;
- switch (operate){
- case "+":
- operation = new OperationAdd(); break;
- case "-":
- operation = new OperationSub(); break;
- case "*":
- operation = new OperationMul(); break;
- case "/":
- operation = new OperationDiv(); break;
- }
- return operation;
- }
-
- /**
- * createOperate1方法不符合开闭原则
- * 如果运算业务继续扩展,需要创建具体运算类,也需要修改简单工厂的 createOperate1方法。
- * 采用反射技术进行优化,即createOperate2方法
- */
- public static Operation createOperate2(String className){
- Operation operation = null;
- try {
- if(!(null == className || "".equals(className))){
- operation = (Operation) Class.forName(className).newInstance();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return operation;
- }
-
- /**
- * createOperate2方法中类型强制转型,参数为字符串,可控性不高
- * 采用反射和泛型技术进行优化,即createOperate3方法
- */
- public static Operation createOperate3(Class<? extends Operation> clazz){
- try {
- if(null != clazz){
- return clazz.newInstance();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
-
- }

- public class Test {
- public static void main(String[] args) throws Exception {
- double num1 = 10;
- double num2 = 2;
- System.out.println(OperationFactory.createOperate0("*").compute(num1, num2));
- System.out.println(OperationFactory.createOperate1("*").compute(num1, num2));
-
- Operation operate2 = OperationFactory.createOperate2("cn.jq.learn.simplefactorypattern.OperationDiv");
- System.out.println(operate2.compute(num1, num2));
-
- Operation operate3 = OperationFactory.createOperate3(OperationAdd.class);
- System.out.println(operate3.compute(num1, num2));
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。