#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.工厂可以重用现有对象
‘‘‘
原文:https://www.cnblogs.com/fireblackman/p/10543734.html