工厂模式的python实现
生活随笔
收集整理的這篇文章主要介紹了
工厂模式的python实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#1.什么是工廠模式#2.工廠模式的分類
'''1. 簡單工廠模式2. 工廠方法模式3. 抽象工廠方法模式
'''#3.簡單工廠模式的python實現
from abc import ABCMeta, abstractmethod
class Animal(metaclass=ABCMeta):@abstractmethoddef do_say(self):passclass 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):@abstractmethoddef describe(self):passclass 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() @abstractmethoddef createProfile(self):passdef getSections(self):return self.sectionsdef 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, abstractmethodclass PizzaFactory(metaclass=ABCMeta):@abstractmethoddef createVegPizza(self):pass@abstractmethoddef createNonVegPizza(self):passclass 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):@abstractmethoddef prepare(self, VegPizza):passclass NonVegPizza(metaclass=ABCMeta):@abstractmethoddef serve(self, VegPizza):passclass 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):passdef makePizzas(self):for factory in [IndianPizzaFactory(), USPizzaFactory()]:self.factory = factoryself.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.工廠可以重用現有對象
'''
轉載于:https://www.cnblogs.com/fireblackman/p/10543734.html
總結
以上是生活随笔為你收集整理的工厂模式的python实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Matplotlib 日期格式转换
- 下一篇: Python有了concurrent的话