- 通过继承定义各个子类,并由工厂类定义一个创建对象的接口,决定通过哪个子类创建实例(将一个类延迟到其子类);
- 工厂模式在以下情况适用:
i. 当一个类不知道它所必须创建的对象的类时;
ii. 当一个类希望由它的子类来指定它所创建的对象时;
iii. 当类将创建对象的职责委托给多个子类中的某一个时;
简单工厂模式
- 用户不需要修改代码,而通过工厂类创建对象即可;
- 需要增加新的运算类同时还要修改工厂类,违反开放封闭原则。
class Shape(object): # 图形基类(必须被继承)
def draw(self):
raise NotImplementedError
class Circle(Shape):
def __init__(self):
self.shape_name = "Circle"
def draw(self):
print('draw circle')
class Rectangle(Shape):
def __init__(self):
self.shape_name = "Retangle"
def draw(self):
print('draw rectangle')
class ShapeFactory(object):
def create(self, shape):
res = None
if shape == 'Circle':
res = Circle()
elif shape == 'Rectangle':
res = Rectangle()
else:
pass
return res
factory = ShapeFactory()
obj = factory.create('Circle')
obj.draw()
工厂方法模式
- 工厂方法模式是简单工厂模式(只有一个工厂)的衍生,对每个产品都有一个工厂,因此解决了简单工厂模式的很多问题。
- 完全实现开放封闭原则,可扩展为更复杂的层次结构;
- 对简单工厂模式进行抽象:由抽象的Factory类负责制定规范,具体的生产工作由其子类去完成。
- 工厂类和产品类可以依次对应,即一个抽象工厂对应一个抽象产品,一个具体工厂负责一个具体产品的生产;
- 增加一个运算类,只需要增加运算类和相对应的工厂,不需要修改工厂类;
- 增加运算类会修改用户代码,工厂方法只是把简单工厂的内部逻辑判断移到了客户端进行。
class AbstractCourse(object):
def __init__(self, name, time_range, study_type, fee):
self.name = name
self.time_range = time_range
self.study_type = study_type
self.fee = fee
def enroll_test(self):
print("课程[%s]测试..." % self.name)
class AbstractSchool(object):
name = ''
def enroll(self, name, course):
pass
def info(self):
pass
class LinuxOPSCourse(AbstractCourse):
def enroll_test(self):
print("Linux入学测试")
class PythonCourse(AbstractCourse):
def enroll_test(self):
print("Python入学测试")
class HCSchool(AbstractSchool):
name = "大学城校区"
def create_course(self, course_type):
if course_type == 'python':
course = PythonCourse("Python开发", 7, '面授', 11000)
elif course_type == 'linux':
course = LinuxOPSCourse("Linux运维", 5, '面授', 12800)
return course
def enroll(self, name, course):
print("开始为新学员[%s]办入学手续... " % name)
print("帮学员[%s]注册课程[%s]..." % (name, course.name))
course.enroll_test()
def info(self):
print("------[%s]-----" % self.name)
class GFGSchool(AbstractSchool):
name = "桂花岗校区"
def create_course(self, course_type):
if course_type == 'py_ops':
course = PythonCourse("Python开发", 8, '在线', 6500)
elif course_type == 'linux':
course = LinuxOPSCourse("Linux运维", 6, '在线', 8000)
return course
def enroll(self, name, course):
print("开始为新学员[%s]办入学手续... " % name)
print("帮学员[%s]注册课程[%s]..." % (name, course.name))
def info(self):
print("------[%s]-----" % self.name)
school1 = HCSchool()
school2 = GFGSchool()
school1.info()
c1 = school1.create_course("linux")
school1.enroll("张三", c1)
school2.info()
c2 = school1.create_course("python")
school2.enroll("李四", c2)
抽象工厂模式
- 提供创建一个相关对象工厂的接口,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象;
- 当一个产品族中的多个对象被设计成一起工作时,能保证客户端始终只使用同一个产品族中的对象;
- 产品族扩展困难:要增加一个系列的某一产品,既要在抽象的 Creator 里加代码,又要在具体的里面加代码。
class Shape(object):
def draw(self):
raise NotImplementedError
class Color(object):
def fill(self):
raise NotImplementedError
class AbstractFactory(object):
def get_color(self, color):
raise NotImplementedError
def get_shape(self, shape_type):
raise NotImplementedError
class Rectangle(Shape):
def draw(self):
print("draw retangle")
class Square(Shape):
def draw(self):
print("draw square")
class Green(Color):
def fill(self):
print("fill green")
class Red(Color):
def fill(self):
print("fill red")
class ShapeFactory(AbstractFactory):
def get_shape(self, shape_type):
if shape_type == "Retangle":
return Rectangle()
elif shape_type == "Square":
return Square()
def get_color(self, color):
return
class ColorFactory(AbstractFactory):
def get_shape(self, shape_type):
return
def get_color(self, color):
if color == "Red":
return Red()
if color == "Green":
return Green()
class FactoryProducer(object):
@staticmethod
def get_factory(choice):
if choice == "Shape":
return ShapeFactory()
elif choice == "Color":
return ColorFactory()
factory = FactoryProducer.get_factory("Shape")
shape1 = factory.get_shape("Retangle")
shape1.draw()
shape2 = factory.get_shape("Square")
shape2.draw()
factory = FactoryProducer.get_factory("Color")
color1 = factory.get_color("Red")
color1.fill()
color2 = factory.get_color("Green")
color2.fill()
网友评论