简单工厂模式
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!!") ## 工厂定义 class ForestFactory: def make_sound(self, object_type): return eval(object_type).do_say(self) ## 客户端 if __name__ == '__main__': ff = ForestFactory() animal = input("Which animal should make_sound Dog or Cat? ") ff.make_sound(animal)
工厂方法模式
from abc import ABCMeta, abstractmethod class Section(metaclass=ABCMeta): @abstractmethod def describe(self): pass class PersonalSection(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 linkedin(Profile): def createProfile(self): self.addSections(PersonalSection()) self.addSections(PatentSection()) self.addSections(PublicationSection()) class facebook(Profile): def createProfile(self): self.addSections(PersonalSection()) self.addSections(AlbumSection()) if __name__ == '__main__': profile_type = input("Which Profile you'd like to create?[LinkedIn or FaceBook] ") profile = eval(profile_type.lower())() print("Create Profile.. {}".format(type(profile).__name__)) print("Profile has sections -- \n{}".format(profile.getSections()))
抽象工厂模式
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 {}".format(type(self).__name__)) #制作鸡肉披萨 class ChickenPizza(NonVegPizza): def serve(self, VegPizza): print("鸡肉披萨 {0} is served with Chicken on {1} 豪华素食披萨".format(type(self).__name__, type(VegPizza).__name__)) #制作墨西哥素食披萨 class MexicanVegPizza(VegPizza): def prepare(self): print("墨西哥素食披萨 Prepare {}".format(type(self).__name__)) #制作火腿披萨 class HamPizza(NonVegPizza): def serve(self, VegPizza): print("火腿披萨 {0} is served with Ham on {1} 墨西哥素食披萨".format(type(self).__name__, type(VegPizza).__name__)) #披萨商店 class PizzaStore: def __init__(self): pass def makePizzas(self): for factory in [IndianPizzaFactory(), USPizzaFactory()]: self.factory = factory #豪华素食披萨 或 墨西哥素食披萨 self.VegPizza = self.factory.createVegPizza() self.VegPizza.prepare() #鸡肉披萨 或 火腿披萨 self.NonVegPizza = self.factory.createNonVegPizza() self.NonVegPizza.serve(self.VegPizza) #开始制作披萨 pizza = PizzaStore() pizza.makePizzas()