美文网首页
八、工厂模式

八、工厂模式

作者: Serenity那年 | 来源:发表于2018-10-28 21:30 被阅读9次

    在学习工厂模式之前,我们先回顾下“简单工厂模式”;简单工厂模式就是问题了解决对象的创建问题;具体例子如下:

    • 1.首先创建一个获取结果的抽象类,让所有的运算类继承它并实现具体的算法;
    package com.zcbl.client.zcblsdk.factroymodel.simple;
    
    /**
     * Created by serenitynanian on 2018/2/9.
     * 创建一个获取结果的抽象类
     */
    
    public abstract class Operation {
    
        public double numA ;
        public double numB ;
    
        public abstract double getResult();
    
    }
    
    • 2.创建具体的算法类,所有的算法类都必须实现Operation;
    package com.zcbl.client.zcblsdk.factroymodel.simple;
    
    /**
     * Created by serenitynanian on 2018/2/9.
     * 创建一个获取结果的抽象类
     */
    
    public abstract class Operation {
    
        public double numA ;
        public double numB ;
    
        public abstract double getResult();
    
    }
    
    package com.zcbl.client.zcblsdk.factroymodel.simple;
    
    /**
     * Created by serenitynanian on 2018/2/9.
     * 具体的减法实现类
     */
    
    public class OperationSub extends Operation {
        @Override
        public double getResult() {
            double result = numA - numB;
            return result;
        }
    }
    
    
    package com.zcbl.client.zcblsdk.factroymodel.simple;
    
    /**
     * Created by serenitynanian on 2018/2/9.
     * 具体的除法实现类
     */
    
    public class OperationDiv extends Operation {
        @Override
        public double getResult() {
            if (numB == 0) {
    //            throw new Exception("除数不能是零");
                System.out.println("除数不能为零");
                return 0.0;
            }
            double result = numA/numB;
            return result;
        }
    }
    
    
    • 3.创建具体的算法简单工厂类
    package com.zcbl.client.zcblsdk.factroymodel.simple;
    
    /**
     * Created by serenitynanian on 2018/2/9.
     * 简单的工厂运算类
     */
    
    public class OperationFactory {
    
        public static Operation createOperation(String operation) {
            Operation ope = null ;
    
            switch (operation) {
                case "+":
                    ope = new OperationAdd();
                    break ;
                case "-":
                    ope = new OperationSub();
                    break ;
                case "*":
                    break ;
                case "/":
                    ope = new OperationDiv();
                    break ;
    
            }
            return ope;
        }
    }
    
    
    • 4.具体的调用如下:
    package com.zcbl.client.zcblsdk.factroymodel.simple;
    
    /**
     * Created by serenitynanian on 2018/2/9.
     */
    
    public class ClientTest {
    
        public static void main(String[] args) {
            Operation operation = OperationFactory.createOperation("+");
            operation.numA = 6 ;
            operation.numB = 2 ;
            double result = operation.getResult();
            System.out.println("----simple factory----result--->"+result);
        }
    }
    

    简单工厂模式的最大的优点在于工厂类中包含了必要的逻辑判断,根据客户端的选择条件动态实例化相关的类,对于客户端来说,去除了与具体产品的依赖;


    下面看下工厂模式
    工厂方法模式(Factory Method):定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法是一个类的实例延迟到其子类。


    • 1.定义一个用于创建对象的接口
    package com.zcbl.client.zcblsdk.factroymodel;
    
    import com.zcbl.client.zcblsdk.factroymodel.simple.Operation;
    
    /**
     * Created by serenitynanian on 2018/2/9.
     */
    
    public interface IFactory {
    
        Operation createOperation();
    }
    
    • 2.创建具体的运算子类;
    package com.zcbl.client.zcblsdk.factroymodel;
    
    import com.zcbl.client.zcblsdk.factroymodel.simple.Operation;
    import com.zcbl.client.zcblsdk.factroymodel.simple.OperationAdd;
    
    /**
     * Created by serenitynanian on 2018/2/9.
     */
    
    public class AddFactory implements IFactory{
    
        @Override
        public Operation createOperation() {
            return new OperationAdd();
        }
    }
    
    package com.zcbl.client.zcblsdk.factroymodel;
    
    import com.zcbl.client.zcblsdk.factroymodel.simple.Operation;
    import com.zcbl.client.zcblsdk.factroymodel.simple.OperationSub;
    
    /**
     * Created by serenitynanian on 2018/2/9.
     */
    
    public class SubFactory implements IFactory {
        @Override
        public Operation createOperation() {
            return new OperationSub();
        }
    }
    
    
    package com.zcbl.client.zcblsdk.factroymodel;
    
    import com.zcbl.client.zcblsdk.factroymodel.simple.Operation;
    import com.zcbl.client.zcblsdk.factroymodel.simple.OperationDiv;
    
    /**
     * Created by serenitynanian on 2018/2/9.
     */
    
    public class DivFactory implements IFactory {
        @Override
        public Operation createOperation() {
            return new OperationDiv();
        }
    }
    
    
    • 3.具体的调用如下:
    package com.zcbl.client.zcblsdk.factroymodel;
    
    import com.zcbl.client.zcblsdk.factroymodel.simple.Operation;
    
    /**
     * Created by serenitynanian on 2018/2/9.
     */
    
    public class FactoryMethodClientTest {
        public static void main(String[] args) {
            //如果需要创建不同的产品,使用不同的工厂类即可
            IFactory iFactory = new AddFactory();
            
            //以下都不变
            Operation operation = iFactory.createOperation();
            operation.numA = 9 ;
            operation.numB = 2 ;
            double res = operation.getResult();
            System.out.println("-------factory method----result---->"+res);
        }
    }
    

    总结:其实工厂方法模式实现时,客户端需要决定实例化哪一个工厂来实现运算类,选择判断的问题还是存在的,也就是说,工厂方法把简单工厂的内部逻辑判断移到了客户端代码来进行。你想要加功能,本来需要修改工厂类,而现在是修改客户端;这样整个工厂和产品体系没有修改,只是扩展需要的工厂类出来创建所需的实例,就完全符合了开放-封闭原则了;

    简单工厂模式和工厂方法模式,它们二者都是集中封装了对象的创建过程,使得要更换对象时,不需要做大的改动就可实现,降低了客户程序与产品对象的耦合。
    工厂方法模式是简单工厂模式的进一步抽象与推广。由于使用了多态性,工厂方法模式保持了简单工厂模式的优点,而且客服了它的缺点。但缺点是由于每加一个产品,就需要加一个产品工厂的类,增加了额外的开发量。

    相关文章

      网友评论

          本文标题:八、工厂模式

          本文链接:https://www.haomeiwen.com/subject/ijtqtftx.html