工厂模式的python实现
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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | #1.什么是工厂模式 #2.工厂模式的分类 ''' 1. 简单工厂模式 2. 工厂方法模式 3. 抽象工厂方法模式 ''' #3.简单工厂模式的python实现 from abc import ABCMeta, abstractmethod class Animal(metaclass = ABCMeta): @abstractmethod def do_say( self ): pass class Dog(Animal): def do_say( self ): print ( "wang wang!!" ) class Cat(Animal): def do_say( self ): print ( "miao miao!!" ) ## 定义工厂 class ForestFactory( object ): def make_sound( self , object_type): return eval (object_type)().do_say() ## client code if __name__ = = "__main__" : ff = ForestFactory() animal = input ( "Which animal should make_sound Dog or Cat?" ) ff.make_sound(animal) #4.工厂方法模式的python实现 from abc import ABCMeta, abstractmethod class Section(metaclass = ABCMeta): @abstractmethod def describe( self ): pass class PersonSection(Section): def describe( self ): print ( "personal section" ) class AlbumSection(Section): def describe( self ): print ( "Album section" ) class PatentSection(Section): def describe( self ): print ( "Patent section" ) class PublicationSection(Section): def describe( self ): print ( "Publication section" ) # 创建一个抽象类, 并提供一个工厂方法 class Profile(metaclass = ABCMeta): def __init__( self ): self .sections = [] self .createProfile() @abstractmethod def createProfile( self ): pass def getSections( self ): return self .sections def addsections( self , section): self .sections.append(section) class Zhihu(Profile): def createProfile( self ): self .addsections(PersonSection()) self .addsections(AlbumSection()) self .addsections(PublicationSection()) class Csdn(Profile): def createProfile( self ): self .addsections(PatentSection()) self .addsections(PersonSection()) if __name__ = = '__main__' : profile_type = input ( "which profile you'd like to create (Zhihu or Csdn)" ) profile = eval (profile_type)() print ( "create profile.." , type (profile).__name__) print ( "Profile has sections --" , profile.getSections()) #5.抽象工厂模式的python实现 from abc import ABCMeta, abstractmethod class PizzaFactory(metaclass = ABCMeta): @abstractmethod def createVegPizza( self ): pass @abstractmethod def createNonVegPizza( self ): pass class IndianPizzaFactory(PizzaFactory): def createVegPizza( self ): return DeluxVeggiePizza() def createNonVegPizza( self ): return ChickenPizza() class USPizzaFactory(PizzaFactory): def createVegPizza( self ): return MexicanVegPizza() def createNonVegPizza( self ): return HamPizza() class VegPizza(metaclass = ABCMeta): @abstractmethod def prepare( self , VegPizza): pass class NonVegPizza(metaclass = ABCMeta): @abstractmethod def serve( self , VegPizza): pass class DeluxVeggiePizza(VegPizza): def prepare( self ): print ( "Prepare " , type ( self ).__name__) class ChickenPizza(NonVegPizza): def serve( self , VegPizza): print ( type ( self ).__name__, " is served with Chicken on " , type (VegPizza).__name__) class MexicanVegPizza(VegPizza): def prepare( self ): print ( "Prepare " , type ( self ).__name__) class HamPizza(NonVegPizza): def serve( self , VegPizza): print ( type ( self ).__name__, " is served with Ham on " , type (VegPizza).__name__) class PizzaStore: def __init__( self ): pass def makePizzas( self ): for factory in [IndianPizzaFactory(), USPizzaFactory()]: self .factory = factory self .NonVegPizza = self .factory.createNonVegPizza() self .VegPizza = self .factory.createVegPizza() self .VegPizza.prepare() self .NonVegPizza.serve( self .VegPizza) pizza = PizzaStore() pizza.makePizzas() #6.工厂方法与抽象工厂方法的比较 # 工厂方法开发了一个创建对象的方法 # 抽象工厂方法开放了一个或者多个方法创建一个系列的相关对象 # 工厂方法使用继承和子类来决定要创建哪个对象 # 抽象共产方法使用组合将创建对象的任务委托给其他类 # 共产方法用于创建一个产品 # 抽象工厂方法用于创建相关产品的系列 # #7.工厂模式的优缺点 ''' 优点: 1.松耦合, 即对象的创建可以独立于类的实现 2.客户端无需了解创建对象的类的实现,但是依然可以创建对象 3.可以在工厂中添加其他类来创建其他类型的对象 4.工厂可以重用现有对象 ''' |
分类:
Design patterns
标签:
设计模式
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?