设计模式-01.工厂方法

这是我尝试写的第一篇文章,以软件开发的设计模式开始,记录一下自己的理解与心得,方便以后回过头来查看。以工厂方法模式开始:

什么是工厂方法?

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

结构图

classDiagram Product <|.. ConcreteProduct class Product{ <<interface>> } class ConcreteProduct{ <<具体的产品,实现Product接口>> } Creator <|-- ConcreteCreator ConcreteCreator ..> ConcreteProduct class Creator{ +FactoryMethod() 声明工厂方法,返回一个 Product 类型的对象 } class ConcreteCreator{ +FactoryMethod() 重定义工厂方法返回一个 ConcreteProduct 类型的对象 }

实例

实现一个计算器,拥有加减乘除功能:

1.抽象一个protocol(Operation), 即:对应结构图中的Product接口。

swift 复制代码
protocol Operation {

    var numberA: Double { set get }

    var numberB: Double { setget }

   
    func calculate() -> Double

}

2.抽象一个protocol(OperationFactory),即:对应结构图中的Creator。协议里面有一个创建Operation类型的对象。

swift 复制代码
protocol OperationFactory {

    func creatOperation() -> Operation

}

3.定义具体的加减乘除的具体Operation对象,都需要实现Operation协议。

swift 复制代码
struct OperationAdd: Operation {

    var numberA: Double = 0.0

    var numberB: Double = 0.0

    func calculate() -> Double {
        return numberA + numberB
    }

}

struct OperationSub: Operation {

    var numberA: Double = 0.0

    var numberB: Double = 0.0

    func calculate() -> Double {
        return numberA - numberB
    }
}

struct OperationMul: Operation {

    var numberA: Double = 0.0

    var numberB: Double = 0.0

    func calculate() -> Double {
        return numberA * numberB
    }

}

struct OperationDiv: Operation {

    var numberA: Double = 0.0

    var numberB: Double = 0.0

    func calculate() -> Double {
        if numberB != 0 {
            return numberA / numberB
        }
        return 0
    }
}

4.定义具体工厂类。

swift 复制代码
class OperationAddFactory: OperationFactory {

    func creatOperation() -> Operation {
        return OperationAdd()
    }
}

class OperationSubFactory: OperationFactory {
    func creatOperation() -> Operation {
        return OperationSub()
    }
}

class OperationMulFactory: OperationFactory {
    func creatOperation() -> Operation {
        return OperationMul()
    }
}

class OperationDivFactory: OperationFactory {
    func creatOperation() -> Operation {
        return OperationDiv()
    }
}

/// 客户端调用
 let addFactory = OperationAddFactory()
 var addOperation = addFactory.creatOperation()
 addOperation.numberA = 1
 addOperation.numberB = 2
 addOperation.calculate()

 let subFactory = OperationSubFactory()
 var subOperation = subFactory.creatOperation()
 subOperation.numberA = 1
 subOperation.numberB = 2
 subOperation.calculate()

 let mulFactory = OperationMulFactory()
 var mulOperation = mulFactory.creatOperation()
 mulOperation.numberA = 1
 mulOperation.numberB = 2
 mulOperation.calculate()

 let divFactory = OperationDivFactory()
 var divOperation = divFactory.creatOperation()
 divOperation.numberA = 1
 divOperation.numberB = 2
 divOperation.calculate()

工厂模式总结

  • 工厂方法模式实现时,客户端需要决定实例化哪一个工厂来实现具体的运算类,与简单工厂相比较,其实选择判断问题还是存在,也就是说,工厂方法把简单工厂的内部逻辑判断移到了客户端代码进行。
  • 符合开闭原则。假如现在有需求需要增加一个开根号的运算,那么只需要增加一个该运算的具体工厂类和运算类。
  • 工厂方法模式需要引入许多新的子类,代码可能会因此变得更复杂。
相关推荐
geovindu4 小时前
rust: Chain of Responsibility Pattern
开发语言·后端·设计模式·rust·责任链模式·行为模式
拆房老料4 小时前
【第四部分:Agentic 智能体设计模式】15.为什么 Agent 也需要设计模式
人工智能·设计模式·ai·ai-native
geovindu5 小时前
rust: Chain of Responsibility Pattern (续)
开发语言·后端·设计模式·rust·责任链模式·行为模式
Lost of 程序猿6 小时前
责任链模式实战:审批流里“层层把关“的艺术
后端·设计模式·asp.net·责任链模式
希洛兔1 天前
状态模式详解:让对象随内部状态自动改变行为
java·设计模式·状态模式
博风1 天前
7、设计模式应用举例:策略模式:加密、解密文件
设计模式·策略模式
4SAPI1 天前
Provider Adapter设计模式:如何让AI应用支持多个模型
网络·人工智能·设计模式
geovindu1 天前
rust: pool Pattern
开发语言·后端·设计模式·rust·对象池简写模式
高晶1 天前
ARM 嵌入式 Linux 开发:从体系结构到 LED 点亮实战
设计模式·程序员
VimorphTech1 天前
接入两套 TTS 接口后,业务代码如何避免堆满 if/else?
设计模式