Python: Abstract Factory Pattern
AbstractFactory.py
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | ## 抽象工厂模式 Abstract Factory Pattern from __future__ import annotations from abc import ABC, abstractmethod class AbstractFactory(ABC): """ The Abstract Factory interface declares a set of methods that return different abstract products. These products are called a family and are related by a high-level theme or concept. Products of one family are usually able to collaborate among themselves. A family of products may have several variants, but the products of one variant are incompatible with products of another. """ @abstractmethod def create_product_a( self ) - > AbstractProductA: pass @abstractmethod def create_product_b( self ) - > AbstractProductB: pass class ConcreteFactory1(AbstractFactory): """ Concrete Factories produce a family of products that belong to a single variant. The factory guarantees that resulting products are compatible. Note that signatures of the Concrete Factory's methods return an abstract product, while inside the method a concrete product is instantiated. """ def create_product_a( self ) - > AbstractProductA: return ConcreteProductA1() def create_product_b( self ) - > AbstractProductB: return ConcreteProductB1() class ConcreteFactory2(AbstractFactory): """ Each Concrete Factory has a corresponding product variant. """ def create_product_a( self ) - > AbstractProductA: return ConcreteProductA2() def create_product_b( self ) - > AbstractProductB: return ConcreteProductB2() class AbstractProductA(ABC): """ Each distinct product of a product family should have a base interface. All variants of the product must implement this interface. """ @abstractmethod def useful_function_a( self ) - > str : pass """ Concrete Products are created by corresponding Concrete Factories. """ class ConcreteProductA1(AbstractProductA): def useful_function_a( self ) - > str : return "The result of the product A1." class ConcreteProductA2(AbstractProductA): def useful_function_a( self ) - > str : return "The result of the product A2." class AbstractProductB(ABC): """ Here's the the base interface of another product. All products can interact with each other, but proper interaction is possible only between products of the same concrete variant. """ @abstractmethod def useful_function_b( self ) - > None : """ Product B is able to do its own thing... """ pass @abstractmethod def another_useful_function_b( self , collaborator: AbstractProductA) - > None : """ ...but it also can collaborate with the ProductA. The Abstract Factory makes sure that all products it creates are of the same variant and thus, compatible. """ pass """ Concrete Products are created by corresponding Concrete Factories. """ class ConcreteProductB1(AbstractProductB): def useful_function_b( self ) - > str : return "The result of the product B1." """ The variant, Product B1, is only able to work correctly with the variant, Product A1. Nevertheless, it accepts any instance of AbstractProductA as an argument. """ def another_useful_function_b( self , collaborator: AbstractProductA) - > str : result = collaborator.useful_function_a() return f "The result of the B1 collaborating with the ({result})" class ConcreteProductB2(AbstractProductB): def useful_function_b( self ) - > str : return "The result of the product B2." def another_useful_function_b( self , collaborator: AbstractProductA): """ The variant, Product B2, is only able to work correctly with the variant, Product A2. Nevertheless, it accepts any instance of AbstractProductA as an argument. """ result = collaborator.useful_function_a() return f "The result of the B2 collaborating with the ({result})" def client_code(factory: AbstractFactory) - > None : """ The client code works with factories and products only through abstract types: AbstractFactory and AbstractProduct. This lets you pass any factory or product subclass to the client code without breaking it. """ product_a = factory.create_product_a() product_b = factory.create_product_b() print (f "{product_b.useful_function_b()}" ) print (f "{product_b.another_useful_function_b(product_a)}" , end = "") |
调用:
main.py
1 2 3 4 5 6 7 8 9 10 11 12 13 | import AbstractFactory if __name__ = = '__main__' : ## 抽象工厂模式 Abstract Factory Pattern print ( "Client: Testing client code with the first factory type:" ) AbstractFactory.client_code(AbstractFactory.ConcreteFactory1()) print ( "\n" ) print ( "Client: Testing the same client code with the second factory type:" ) AbstractFactory.client_code(AbstractFactory.ConcreteFactory2()); |
输出:
1 2 3 4 5 6 7 8 | Client: Testing client code with the first factory type : The result of the product B1. The result of the B1 collaborating with the (The result of the product A1.) Client: Testing the same client code with the second factory type : The result of the product B2. The result of the B2 collaborating with the (The result of the product A2.) Process finished with exit code 0 |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
分类:
Python
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!