簡(jiǎn)單工廠模式
歡迎關(guān)注我的公眾號(hào):zx94_11
基于一個(gè)包含do_say()
方法的Animal
的抽象類創(chuàng)建兩個(gè)類
- Cat
- Dog
from abc import ABCMeta, abstractmethod
class Animal(metaclass=ABCMeta):
@abstractmethod
def do_say(self):
pass
class Dog(Animal):
def do_say(self):
print("Bhow Bhow!!")
class Cat(Animal):
def do_say(self):
print("Meow Meow!!")
創(chuàng)建一個(gè)包含make_sound()
方法的工廠類ForestFactory
class ForestFactory(object):
def make_sound(self, object_type):
return eval(object_type)().do_say()
制造點(diǎn)聲音
if __name__ == '__main__':
ff = ForestFactory()
animal = input("Which animal should make_sound?[Dog or Cat]")
ff.make_sound(animal)
運(yùn)行結(jié)果
類關(guān)系圖
類關(guān)系圖
工廠方法模式
有兩個(gè)社交網(wǎng)站LinkedIn和Facebook历葛,它們的個(gè)人簡(jiǎn)介界面有各自不同的內(nèi)容
內(nèi)容抽象類Section
from abc import ABCMeta, abstractmethod
class Section(metaclass=ABCMeta):
@abstractmethod
def describe(self):
pass
不同的內(nèi)容
- PersonalSection
- AlbumSection
- PatenSection
- PublicationSection
class PersonalSection(Section):
def describe(self):
print("Personal Section")
class AlbumSection(Section):
def describe(self):
print("Album Section")
class PatenSection(Section):
def describe(self):
print("Patent Section")
class PublicationSection(Section):
def describe(self):
print("Publication Section")
公司抽象類Profile
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 linkedin(Profile):
def createProfile(self):
self.addSections(PersonalSection())
self.addSections(PatenSection())
self.addSections(PublicationSection())
class facebook(Profile):
def createProfile(self):
self.addSections(PersonalSection())
self.addSections(AlbumSection())
選擇一個(gè)社交網(wǎng)站
if __name__ == '__main__':
profile_type = input("Which Profile you`d like to create?[LinkedIn or FaceBook]")
profile = eval(profile_type.lower())()
print("Creating Profile..", type(profile).__name__)
print("Profile has sections --", profile.getSections())
測(cè)試結(jié)果
類關(guān)系圖
工廠方法模式
抽象工廠模式
一家提供印式和美式披薩的店(抽象類PizzFactory
)
class PizzFactory(metaclass=ABCMeta):
@abstractmethod
def createVegPizza(self):
pass
@abstractmethod
def createNonVegPizza(self):
pass
- IndianPizzaFactory
- USPizzaFactory
class IndianPizzaFactory(PizzFactory):
def createVegPizza(self):
return DeluxVeggiePizza()
def createNonVegPizza(self):
return ChickenPizza()
class USPizzaFactory(PizzFactory):
def createVegPizza(self):
return MexicanVegPizza()
def createNonVegPizza(self):
return HamPizza()
兩種不同的披薩(抽象類)
- 素食披薩
NonVegPizza
- 非素食披薩
VegPizza
class VegPizza(metaclass=ABCMeta):
@abstractmethod
def prepare(self):
pass
class NonVegPizza(metaclass=ABCMeta):
@abstractmethod
def serve(self, VegPizza):
pass
四種不同的披薩
- DeluxVeggiePizza
- ChickenPizza
- MexicanVegPizza
- HamPizza
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__)
到此就完成了一家披薩店肢预,有兩個(gè)國(guó)家口味,各有葷素的披薩。
整合為一個(gè)制造披薩的工廠
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)
測(cè)試
if __name__ == '__main__':
pizza = PizzaStore()
pizza.makePizzas()
測(cè)試結(jié)果
類關(guān)系圖
類關(guān)系圖
工廠方法和抽象工廠方法
工廠方法 | 抽象工廠方法 |
---|---|
向客戶端開發(fā)了一個(gè)創(chuàng)建對(duì)象的方法 | 包含一個(gè)或多個(gè)工廠方法來創(chuàng)建一個(gè)系列的相關(guān)對(duì)象 |
使用繼承和子類來決定要?jiǎng)?chuàng)建哪個(gè)對(duì)象 | 使用組合將創(chuàng)建對(duì)象的任務(wù)委托給其他類 |
用于創(chuàng)建一個(gè)產(chǎn)品 | 用于創(chuàng)建相關(guān)產(chǎn)品的系列 |