在Python中,工厂模式(Factory Pattern)是一种常用的创建型设计模式,用于创建对象时不会将具体类的信息暴露给客户端,而是通过一个共同的接口来指向新创建的对象。工厂模式主要用来创建复杂对象,并隐藏实例化逻辑,使得客户端代码与具体类的实现解耦,从而提高系统的灵活性和可扩展性。
工厂模式主要有三种形式:简单工厂模式(Simple Factory Pattern)、工厂方法模式(Factory Method Pattern)和抽象工厂模式(Abstract Factory Pattern)。
1. 简单工厂模式(Simple Factory Pattern)
简单工厂模式通过一个工厂类来创建对象,这个工厂类根据传入的参数决定创建哪一种类的实例。
class ProductA:
def operation(self):
return "Product A"
class ProductB:
def operation(self):
return "Product B"
class Factory:
@staticmethod
def create_product(product_type):
if product_type == "A":
return ProductA()
elif product_type == "B":
return ProductB()
# 使用
product_a = Factory.create_product("A")
print(product_a.operation()) # 输出: Product A
product_b = Factory.create_product("B")
print(product_b.operation()) # 输出: Product B
2. 工厂方法模式(Factory Method Pattern)
工厂方法模式将对象的创建延迟到子类中进行,工厂类提供一个创建对象的接口,让子类决定实例化哪一个类。
class Product:
def operation(self):
pass
class ConcreteProductA(Product):
def operation(self):
return "Product A"
class ConcreteProductB(Product):
def operation(self):
return "Product B"
class Creator:
def factory_method(self):
raise NotImplementedError("子类必须实现这个方法")
def some_operation(self):
product = self.factory_method()
return product.operation()
class ConcreteCreatorA(Creator):
def factory_method(self):
return ConcreteProductA()
class ConcreteCreatorB(Creator):
def factory_method(self):
return ConcreteProductB()
# 使用
creator_a = ConcreteCreatorA()
print(creator_a.some_operation()) # 输出: Product A
creator_b = ConcreteCreatorB()
print(creator_b.some_operation()) # 输出: Product B
3. 抽象工厂模式(Abstract Factory Pattern)
抽象工厂模式提供一个接口,用于创建相关或依赖对象的家族,而不需要明确指定具体类。
class AbstractProductA:
def __init__(self):
pass
def use(self):
raise NotImplementedError
class AbstractProductB:
def __init__(self):
pass
def eat(self):
raise NotImplementedError
class ProductA1(AbstractProductA):
def use(self):
return "Product A1"
class ProductA2(AbstractProductA):
def use(self):
return "Product A2"
class ProductB1(AbstractProductB):
def eat(self):
return "Product B1"
class ProductB2(AbstractProductB):
def eat(self):
return "Product B2"
class AbstractFactory:
def create_product_a(self):
raise NotImplementedError
def create_product_b(self):
raise NotImplementedError
class ConcreteFactory1(AbstractFactory):
def create_product_a(self):
return ProductA1()
def create_product_b(self):
return ProductB1()
class ConcreteFactory2(AbstractFactory):
def create_product_a(self):
return ProductA2()
def create_product_b(self):
return ProductB2()
# 使用
factory1 = ConcreteFactory1()
product_a1 = factory1.create_product_a()
print(product_a1.use()) # 输出: Product A1
product_b1 = factory1.create_product_b()
print(product_b1.eat()) # 输出: Product B1
factory2 = ConcreteFactory2()
product_a2 = factory2.create_product_a()
print(product_a2.use()) # 输出: Product A2
product_b2 = factory2.create_product_b()
print(product_b2.eat()) # 输出: Product B2
每种工厂模式都有其适用场景,选择哪种模式取决于具体的设计需求和系统复杂度。
标签:product,return,Python,self,Product,模式,工厂,class,def From: https://blog.51cto.com/u_15266301/11984069