美文网首页产品
设计模式之工厂模式

设计模式之工厂模式

作者: Tinyspot | 来源:发表于2023-02-10 20:35 被阅读0次

    基础

    工厂模式,让创建实例的过程封装到工厂类中,避免耦合

    工厂类中创建对象的方法一般都是 create 开头,但有的也命名为 getInstance()、createInstance()、newInstance(),有的甚至命名为 valueOf()(比如 Java String 类的 valueOf() 函数)等等

    1.1 简单工厂

    简单工厂模式(Simple Factory Pattern)又叫作静态工厂方法模式(Static Factory Method Pattern)

    简单工厂模式的最大优点在于工厂类中包含必要的逻辑判断,根据客户端的选择条件动态实例化相关的类,对于客户端来说,去除了与具体产品的依赖
    增加操作:
    不但对扩展开放了,对修改也开放了违背的是开放-封闭原则

    1.2 工厂方法

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

    2. 示例:四则运算

    public static double calculate(String operator, double num1, double num2) {
        double result = 0;
        switch (operator) {
            case "+":
                result = num1 + num2;
                break;
            case "-":
                result = num1 - num2;
                break;
            case "*":
                result = num1 * num2;
                break;
            case "/":
                if (num2 == 0) {
                    throw new ArithmeticException("num2 is zero");
                }
                result = num1 / num2;
                break;
        }
        return result;
    }
    

    2.1 优化一:简单工厂模式

    image.png

    简单运算工厂类 OperationFactory 负责实例化具体的运算对象

    public class OperationFactory {
        public static Operation createOperation(String operator) {
            Operation operation = null;
            switch (operator) {
                case "+":
                    operation = new Addition();
                    break;
                case "-":
                    operation = new Subtract();
                    break;
                case "*":
                    operation = new Multiply();
                    break;
                case "/":
                    operation = new Division();
                    break;
            }
            return operation;
        }
    }
    

    抽象运算类和它的子类

    public abstract class Operation {
        public abstract double calculate(double num1, double num2);
    }
    
    public class Addition extends Operation {
        @Override
        public double calculate(double num1, double num2) {
            return num1 + num2;
        }
    }
    public class Subtract extends Operation {
        @Override
        public double calculate(double num1, double num2) {
            return num1 - num2;
        }
    }
    public class Multiply extends Operation {
        @Override
        public double calculate(double num1, double num2) {
            return num1 * num2;
        }
    }
    public class Division extends Operation {
        @Override
        public double calculate(double num1, double num2) {
            if (num2 == 0) {
                throw new ArithmeticException("num2 is zero");
            }
            return num1 / num2;
        }
    }
    

    测试

    @Test
    public void test() {
        Operation operation = OperationFactory.createOperation("+");
        double result = operation.calculate(10, 1);
        System.out.println(result);
    }
    

    2.2 优化二:工厂方法模式

    image.png

    工厂接口

    public interface IFactory {
        Operation createOperation(String operator);
    }
    

    基础运算工厂和高级运算工厂

    public class BasicFactory implements IFactory {
        @Override
        public Operation createOperation(String operator) {
            Operation operation = null;
            switch (operator) {
                case "+":
                    operation = new Addition();
                    break;
                case "-":
                    operation = new Subtract();
                    break;
                case "*":
                    operation = new Multiply();
                    break;
                case "/":
                    operation = new Division();
                    break;
            }
            return operation;
        }
    }
    
    public class AdvancedFactory implements IFactory {
        @Override
        public Operation createOperation(String operator) {
            Operation operation = null;
            switch (operator) {
                case "pow":
                    operation = new Pow();
                    break;
                case "log":
                    operation = new Log();
                    break;
            }
            return operation;
        }
    }
    
    public class Pow extends Operation {
        @Override
        public double calculate(double num1, double num2) {
            return Math.pow(num1, num2);
        }
    }
    public class Log extends Operation {
        /**
         * 取对数:以 num1 为底的 num2 的对数
         */
        @Override
        public double calculate(double num1, double num2) {
            return Math.log(num2) / Math.log(num1);
        }
    }
    

    运算工厂类

    public class OperationFactory {
    
        public static Operation createOperation(String operator) {
            Operation operation = null;
    
            IFactory factory = null;
            switch (operator) {
                case "+":
                case "-":
                case "*":
                case "/":
                    factory = new BasicFactory();
                    break;
                case "pow":
                case "log":
                    factory = new AdvancedFactory();
                    break;
            }
            // 多态
            operation = factory.createOperation(operator);
            return operation;
        }
    }
    

    新的 OperationFactory 类已经不存在运算子类实例化的代码。也就是说,在这个代码里,全部是接口与具体工厂类,并不存在具体的实现,与原来的OperationFactory 类对比,实例化的过程延迟到了工厂子类中

    测试

    @Test
    public void test() {
        Operation operation = OperationFactory.createOperation("pow");
        double result = operation.calculate(2, 4);
        System.out.println(result);
    }
    

    相关文章

      网友评论

        本文标题:设计模式之工厂模式

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