一、工厂模式概述
(一)定义
工厂模式是一种创建型设计模式。它的核心思想是定义一个创建对象的接口(可以是抽象类或者接口),让子类决定实例化哪一个类。工厂方法把实例化推迟到子类,这样就可以将对象的创建和使用分离,从而提高系统的灵活性和可扩展性。
(二)分类
- 简单工厂模式
- 这种模式不是严格的设计模式,但它为工厂模式奠定了基础。它由一个工厂类来决定创建哪一种产品类的实例。简单工厂模式的结构相对简单,它包含一个工厂类和多个产品类。
- 例如,假设我们要创建不同类型的图形对象,如圆形、矩形等。简单工厂模式下,有一个图形工厂类,根据传入的参数(如"circle"表示圆形,"rectangle"表示矩形)来创建对应的图形对象。
- 工厂方法模式
- 工厂方法模式是简单工厂模式的升级。它引入了抽象工厂类,这个抽象工厂类声明了一个工厂方法,用于创建产品对象。具体的子类工厂继承抽象工厂类,并实现工厂方法来创建具体的产品对象。
- 以汽车制造为例,有一个抽象的汽车工厂类,它有一个工厂方法用于创建汽车。然后有具体的工厂类,如宝马工厂和奔驰工厂,它们继承抽象工厂类。宝马工厂的工厂方法创建宝马汽车对象,奔驰工厂的工厂方法创建奔驰汽车对象。
- 抽象工厂模式
- 抽象工厂模式是最复杂的工厂模式。它提供了一个接口,用于创建相关或依赖对象的家族,而不需要明确指定具体类。这种模式适用于产品族和产品等级结构都比较复杂的情况。
- 比如,一个电子产品公司生产手机和电脑两种产品,而且有高端和低端两种产品等级。抽象工厂模式下,有一个抽象工厂接口,用于创建手机和电脑。然后有高端产品工厂和低端产品工厂,高端产品工厂创建高端手机和高端电脑,低端产品工厂创建低端手机和低端电脑。
二、Python中实现工厂模式
(一)简单工厂模式
python
# 定义产品接口
class Shape:
def draw(self):
pass
# 创建具体产品类
class Circle(Shape):
def draw(self):
print("Inside Circle::draw() method.")
class Rectangle(Shape):
def draw(self):
print("Inside Rectangle::draw() method.")
# 创建工厂类
class ShapeFactory:
def get_shape(self, shape_type):
if shape_type == None:
return None
if shape_type == "circle":
return Circle()
elif shape_type == "rectangle":
return Rectangle()
return None
# 使用
shape_factory = ShapeFactory()
shape1 = shape_factory.get_shape("circle")
shape1.draw()
shape2 = shape_factory.get_shape("rectangle")
shape2.draw()
在这个例子中,ShapeFactory
类根据传入的shape_type
参数来决定创建Circle
还是Rectangle
对象。
(二)工厂方法模式
python
# 定义产品接口
class Shape:
def draw(self):
pass
# 创建具体产品类
class Circle(Shape):
def draw(self):
print("Inside Circle::draw() method.")
class Rectangle(Shape):
def draw(self):
print("Inside Rectangle::draw() method.")
# 定义抽象工厂类
class ShapeFactory:
def get_shape(self):
pass
# 创建具体工厂类
class CircleFactory(ShapeFactory):
def get_shape(self):
return Circle()
class RectangleFactory(ShapeFactory):
def get_shape(self):
return Rectangle()
# 使用
circle_factory = CircleFactory()
circle = circle_factory.get_shape()
circle.draw()
rectangle_factory = RectangleFactory()
rectangle = rectangle_factory.get_shape()
rectangle.draw()
这里ShapeFactory
是抽象工厂类,CircleFactory
和RectangleFactory
是具体的工厂类,它们分别实现了get_shape
方法来创建对应的Circle
和Rectangle
对象。
(三)抽象工厂模式
python
# 定义产品接口
class Shape:
def draw(self):
pass
class Color:
def fill(self):
pass
# 创建具体产品类
class Circle(Shape):
def draw(self):
print("Inside Circle::draw() method.")
class Rectangle(Shape):
def draw(self):
print("Inside Rectangle::draw() method.")
class Red(Color):
def fill(self):
print("Inside Red::fill() method.")
class Blue(Color):
def fill(self):
print("Inside Blue::fill() method.")
# 定义抽象工厂类
class AbstractFactory:
def get_shape(self):
pass
def get_color(self):
pass
# 创建具体工厂类
class ShapeFactory(AbstractFactory):
def get_shape(self):
return Circle()
def get_color(self):
return None
class ColorFactory(AbstractFactory):
def get_shape(self):
return None
def get_color(self):
return Red()
# 使用
shape_factory = ShapeFactory()
shape = shape_factory.get_shape()
shape.draw()
color_factory = ColorFactory()
color = color_factory.get_color()
color.fill()
在这个例子中,AbstractFactory
是抽象工厂类,ShapeFactory
和ColorFactory
是具体工厂类。ShapeFactory
创建形状对象,ColorFactory
创建颜色对象。这种模式可以很方便地扩展新的产品族和产品等级结构。