• 安卓常见设计模式3.2------工厂模式,工厂方法模式,抽象工厂模式对比(Kotlin版)


    1. 都是创建型模式

    工厂模式(Factory Pattern)、工厂方法模式(Factory Method Pattern)和抽象工厂模式(Abstract Factory Pattern)都是创建型设计模式,它们之间有以下区别:

    1. 工厂模式(Factory Pattern):

      工厂模式是一种创建对象的模式,它通过一个工厂类来封装对象的创建过程。客户端只需要与工厂类交互,而无需直接实例化具体的产品对象。
      工厂模式适用于创建单个产品对象的场景,它通过工厂类的静态方法或实例方法来创建具体产品对象。

    2. 工厂方法模式(Factory Method Pattern):

      工厂方法模式是一种创建单个对象的模式,它定义了一个工厂方法接口,在具体的工厂类中实现该方法来创建具体的产品对象。每个具体工厂类只负责创建一种类型的产品对象
      工厂方法模式适用于需要根据不同情况创建不同类型的产品对象的场景。

    3. 抽象工厂模式(Abstract Factory Pattern):

      抽象工厂模式是一种创建一系列相关或相互依赖的对象的模式,它提供了一个抽象工厂接口,该接口声明了一组创建产品对象的方法。具体的工厂类实现这些方法来创建具体的产品对象。
      抽象工厂模式适用于创建一系列相关产品的场景,每个具体工厂类负责创建特定产品族的对象。

    2. 代码举例

    // 工厂模式示例:

    // 抽象产品类
    abstract class Product {
        abstract fun operation()
    }
    
    // 具体产品类A
    class ConcreteProductA : Product() {
        override fun operation() {
            println("ConcreteProductA operation")
        }
    }
    
    // 具体产品类B
    class ConcreteProductB : Product() {
        override fun operation() {
            println("ConcreteProductB operation")
        }
    }
    
    // 工厂类
    class Factory {
        fun createProduct(type: String): Product {
            return when (type) {
                "A" -> ConcreteProductA()
                "B" -> ConcreteProductB()
                else -> throw IllegalArgumentException("Invalid product type")
            }
        }
    }
    
    // 客户端代码
    fun main() {
        val factory = Factory()
        val productA = factory.createProduct("A")
        productA.operation()
    
        val productB = factory.createProduct("B")
        productB.operation()
    }
    
    • 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
    • 37
    • 38
    • 39

    // 工厂方法模式示例:

    // 抽象产品类
    abstract class Product {
        abstract fun operation()
    }
    
    // 具体产品类A
    class ConcreteProductA : Product() {
        override fun operation() {
            println("ConcreteProductA operation")
        }
    }
    
    // 具体产品类B
    class ConcreteProductB : Product() {
        override fun operation() {
            println("ConcreteProductB operation")
        }
    }
    
    // 抽象工厂类
    abstract class Factory {
        abstract fun createProduct(): Product
    
        fun performOperation() {
            val product = createProduct()
            product.operation()
        }
    }
    
    // 具体工厂类A
    class ConcreteFactoryA : Factory() {
        override fun createProduct(): Product {
            return ConcreteProductA()
        }
    }
    
    // 具体工厂类B
    class ConcreteFactoryB : Factory() {
        override fun createProduct(): Product {
            return ConcreteProductB()
        }
    }
    
    // 客户端代码
    fun main() {
        val factoryA: Factory = ConcreteFactoryA()
        factoryA.performOperation()
    
        val factoryB: Factory = ConcreteFactoryB()
        factoryB.performOperation()
    }
    
    • 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
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    // 抽象工厂模式示例:

    // 抽象产品接口A
    interface ProductA {
        fun operationA()
    }
    
    // 具体产品类A1
    class ConcreteProductA1 : ProductA {
        override fun operationA() {
            println("ConcreteProductA1 operationA")
        }
    }
    
    // 具体产品类A2
    class ConcreteProductA2 : ProductA {
        override fun operationA() {
            println("ConcreteProductA2 operationA")
        }
    }
    
    // 抽象产品接口B
    interface ProductB {
        fun operationB()
    }
    
    // 具体产品类B1
    class ConcreteProductB1 : ProductB {
        override fun operationB() {
            println("ConcreteProductB1 operationB")
        }
    }
    
    // 具体产品类B2
    class ConcreteProductB2 : ProductB {
        override fun operationB() {
            println("ConcreteProductB2 operationB")
        }
    }
    
    // 抽象工厂接口
    interface AbstractFactory {
        fun createProductA(): ProductA
        fun createProductB(): ProductB
    }
    
    // 具体工厂类1
    class ConcreteFactory1 : AbstractFactory {
        override fun createProductA(): ProductA {
            return ConcreteProductA1()
        }
    
        override fun createProductB(): ProductB {
            return ConcreteProductB1()
        }
    }
    
    // 具体工厂类2
    class ConcreteFactory2 : AbstractFactory {
        override fun createProductA(): ProductA {
            return ConcreteProductA2()
        }
    
        override fun createProductB(): ProductB {
            return ConcreteProductB2()
        }
    }
    
    // 客户端代码
    fun main() {
        val factory1: AbstractFactory = ConcreteFactory1()
        val productA1: ProductA = factory1.createProductA()
        val productB1: ProductB = factory1.createProductB()
        productA1.operationA()
        productB1.operationB()
    
        val factory2: AbstractFactory = ConcreteFactory2()
        val productA2: ProductA = factory2.createProductA()
        val productB2: ProductB = factory2.createProductB()
        productA2.operationA()
        productB2.operationB()
    }
    
    • 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
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80

    Thank you for your reading, best regards!

  • 相关阅读:
    申威芯片UOS中opencv DNN推理
    操作系统01_进程管理_---软考高级系统架构师006
    软考高级系统架构设计师系列之:可靠性设计
    Vscode配置C/C++编程环境@配置C和CPP的运行和调试环境@配置过程的相关问题@中文文件名乱码@build和debug方案组合配置
    网络安全渗透测试工具之skipfish
    JVM 执行引擎部分 (编译器、解释器)
    源码解析day06 (PriorityQueue)
    Unity - Normal mapping - Reoriented normal mapping - 重定向法线、混合法线
    linux命令查看谁在使用服务器的GPU
    vuejs - - - - - 递归组件的实现
  • 原文地址:https://blog.csdn.net/qq_42751010/article/details/134268268