工厂模式是一种创建型设计模式。它的核心思想是定义一个创建对象的接口(可以是抽象类或者接口),让子类决定实例化哪一个类。工厂方法把实例化推迟到子类,这样就可以将对象的创建和使用分离,从而提高系统的灵活性和可扩展性。
python 体验AI代码助手 代码解读复制代码# 定义产品接口
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 体验AI代码助手 代码解读复制代码# 定义产品接口
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 体验AI代码助手 代码解读复制代码# 定义产品接口
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
创建颜色对象。这种模式可以很方便地扩展新的产品族和产品等级结构。
策略模式(Strategy Pattern) :定义一系列算法(或操作),将每个算法封装起来,并使它们可以互换。Ironic 的每个 interface(如 DeployInterface)就是一个策略,具体的 driver 实现(如 AgentDeploy、PXEDeploy)就是不同的策略实现。
ruby 体验AI代码助手 代码解读复制代码import abc
class DeployInterface(metaclass=abc.ABCMeta):
@abc.abstractmethod
def deploy(self, task):
pass
@abc.abstractmethod
def tear_down(self, task):
pass
class PXEDeploy(DeployInterface):
def deploy(self, task):
print("Deploying node with PXE")
def tear_down(self, task):
print("Tearing down PXE deployment")
class AgentDeploy(DeployInterface):
def deploy(self, task):
print("Deploying node with Agent")
def tear_down(self, task):
print("Tearing down Agent deployment")
def run_deploy(deploy_interface: DeployInterface, task):
deploy_interface.deploy(task)
deploy_interface.tear_down(task)
# 假设根据配置或硬件类型动态选择
deploy_impl = PXEDeploy() # 或 AgentDeploy()
run_deploy(deploy_impl, task={})
# 输出
Deploying node with PXE
Tearing down PXE deployment
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。