• 大话设计模式——2.简单工厂模式(Simple Factory Pattern)


    定义:又称静态工厂方法,可以根据参数的不同返回不同类的实例,专门定义一个类(工厂类)来负责创建其他类的实例可通过类名直接调用,被创建的实例通常具有共同的父类。属于创建型模式
    UML图
    在这里插入图片描述
    例子:
    计算器中的加减乘除,可将不同的运算看成不同的对象,通过工厂类进行构建,传入你想创建的对象的运算符号。
    基类:运算对象,确定运算参数和方法

    public class Operation {
    
        private int numberA;
    
        private int numberB;
    
        /**
         * 计算数据结果,通用方法
         */
        public void count() {
        }
    
        public Operation() {
        }
    
        public Operation(int numberA, int numberB) {
            this.numberA = numberA;
            this.numberB = numberB;
        }
    
        public int getNumberA() {
            return numberA;
        }
    
        public void setNumberA(int numberA) {
            this.numberA = numberA;
        }
    
        public int getNumberB() {
            return numberB;
        }
    
        public void setNumberB(int numberB) {
            this.numberB = numberB;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    派生类:
    加法运算对象继承基类

    public class AddOperation extends Operation {
    
        @Override
        public void count() {
            System.out.println(this.getNumberA() + "+" + this.getNumberB() + "=" + (this.getNumberA() + this.getNumberB()));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    后续运算对象创建类似。

    工厂对象创建:提供一个统一的静态方法,创建运算对象

    public class OperationFactory {
    
        /**
         * 构建运算对象
         *
         * @param calSign
         * @return
         */
        public static Operation createOperation(String calSign) {
            Operation opr = null;
            switch (calSign){
                case "+":
                    opr = new AddOperation();
                    break;
                case "-":
                    opr = new SubOperation();
                    break;
                case "*":
                    opr = new MutOperation();
                    break;
                default:
                    System.out.println("暂未设置该类型运算");
            }
            return opr;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    执行:

    public static void main(String[] args) {
            Operation opr;
            try {
                // 加法运算
                opr = OperationFactory.createOperation("+");
                opr.setNumberA(10);
                opr.setNumberB(12);
                opr.count();
    
                // 减法运算
                opr = OperationFactory.createOperation("-");
                opr.setNumberA(10);
                opr.setNumberB(12);
                opr.count();
    
                // 乘法运算
                opr = OperationFactory.createOperation("*");
                opr.setNumberA(10);
                opr.setNumberB(12);
                opr.count();
    
                // 除法运算
                opr = OperationFactory.createOperation("/");
                opr.setNumberA(10);
                opr.setNumberB(12);
                opr.count();
            } catch (NullPointerException ignored) {
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    在这里插入图片描述

    总结:
    优点:

    • 降低对象与业务之间的耦合性,隐藏对象的创建逻辑,使得对象与相关业务的修改较为容易。

    缺点:

    • 违背开放-封闭原则,每新增一个业务便要修改工厂对象和创建新的对象
    • 增加了系统的复杂度和理解难度,不便于维护和扩展
  • 相关阅读:
    RequestContextHolder
    python爬虫代理ip关于设置proxies的问题
    HTML-界面设计字体背景颜色-下拉列表框-margin-top失效-Div换行
    SpringBoot整合ElasticSearch应用
    22 OpenCV 直方图计算
    22-08-02 西安 尚医通(02)Vscode、ES6、nodejs、npm、Bable转码器、js模块化、webpack
    【LeetCode】Day177-统计一致字符串的数目
    leetcode(力扣) 198. 打家劫舍 (入门动态规划)
    Linux安装redis数据库
    图形系统开发实战课程:进阶篇(上)——6.图形交互操作:拾取
  • 原文地址:https://blog.csdn.net/a1498665771/article/details/136195584