日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

工厂模式的python实现

發布時間:2024/10/12 python 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 工厂模式的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实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。