设计模式——抽象工厂模式

文章目录

抽象工厂模式(Abstract Factory Pattern)简介

抽象工厂模式是一种创建型设计模式,它提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。抽象工厂模式通过为对象的创建提供一个抽象层,使得客户端可以使用抽象接口来创建一组相关的产品,而不需要知道具体的实现细节。

抽象工厂模式的组成部分

  1. 抽象工厂(AbstractFactory):声明创建一系列相关对象的操作。
  2. 具体工厂(ConcreteFactory):实现抽象工厂中的创建操作,生成具体的产品对象。
  3. 抽象产品(AbstractProduct):为每种产品声明一个接口。
  4. 具体产品(ConcreteProduct):实现抽象产品接口,定义具体产品的行为。
  5. 客户端(Client):使用抽象工厂和抽象产品的接口,依赖于这些接口而不是具体类。

抽象工厂模式的结构

python 复制代码
from abc import ABC, abstractmethod

# 抽象产品
class AbstractProductA(ABC):
    @abstractmethod
    def do_something(self):
        pass

class AbstractProductB(ABC):
    @abstractmethod
    def perform_action(self):
        pass

# 具体产品
class ConcreteProductA1(AbstractProductA):
    def do_something(self):
        return "Product A1"

class ConcreteProductA2(AbstractProductA):
    def do_something(self):
        return "Product A2"

class ConcreteProductB1(AbstractProductB):
    def perform_action(self):
        return "Product B1"

class ConcreteProductB2(AbstractProductB):
    def perform_action(self):
        return "Product B2"

# 抽象工厂
class AbstractFactory(ABC):
    @abstractmethod
    def create_product_a(self) -> AbstractProductA:
        pass

    @abstractmethod
    def create_product_b(self) -> AbstractProductB:
        pass

# 具体工厂
class ConcreteFactory1(AbstractFactory):
    def create_product_a(self) -> AbstractProductA:
        return ConcreteProductA1()

    def create_product_b(self) -> AbstractProductB:
        return ConcreteProductB1()

class ConcreteFactory2(AbstractFactory):
    def create_product_a(self) -> AbstractProductA:
        return ConcreteProductA2()

    def create_product_b(self) -> AbstractProductB:
        return ConcreteProductB2()

# 客户端代码
def client_code(factory: AbstractFactory):
    product_a = factory.create_product_a()
    product_b = factory.create_product_b()
    print(product_a.do_something())
    print(product_b.perform_action())

print("Client: Testing client code with the first factory type:")
client_code(ConcreteFactory1())

print("\nClient: Testing the same client code with the second factory type:")
client_code(ConcreteFactory2())

类AbstractFactory的作用

  • 抽象工厂(AbstractFactory):定义创建一系列相关对象的接口。抽象工厂模式的核心在于这个抽象工厂类,它为一组相关的产品提供了创建接口,使得客户端可以通过这个接口来创建具体的产品,而不需要知道具体的实现类。

具体子类的作用

  • 具体工厂(ConcreteFactory):实现抽象工厂中的创建操作,生成具体的产品对象。每个具体工厂类对应一个特定的产品家族,它们实现了抽象工厂定义的创建方法,从而创建属于这个家族的具体产品。

抽象工厂模式的实例

1. GUI库

假设我们有一个跨平台的GUI库,需要生成不同风格的按钮和文本框,例如Windows风格和Mac风格。

python 复制代码
from abc import ABC, abstractmethod

# 抽象产品
class Button(ABC):
    @abstractmethod
    def click(self):
        pass

class TextBox(ABC):
    @abstractmethod
    def render(self):
        pass

# 具体产品
class WindowsButton(Button):
    def click(self):
        return "Windows Button clicked"

class MacButton(Button):
    def click(self):
        return "Mac Button clicked"

class WindowsTextBox(TextBox):
    def render(self):
        return "Rendering Windows TextBox"

class MacTextBox(TextBox):
    def render(self):
        return "Rendering Mac TextBox"

# 抽象工厂
class GUIFactory(ABC):
    @abstractmethod
    def create_button(self) -> Button:
        pass

    @abstractmethod
    def create_textbox(self) -> TextBox:
        pass

# 具体工厂
class WindowsFactory(GUIFactory):
    def create_button(self) -> Button:
        return WindowsButton()

    def create_textbox(self) -> TextBox:
        return WindowsTextBox()

class MacFactory(GUIFactory):
    def create_button(self) -> Button:
        return MacButton()

    def create_textbox(self) -> TextBox:
        return MacTextBox()

# 客户端代码
def client_code(factory: GUIFactory):
    button = factory.create_button()
    textbox = factory.create_textbox()
    print(button.click())
    print(textbox.render())

print("Client: Testing client code with Windows factory:")
client_code(WindowsFactory())

print("\nClient: Testing client code with Mac factory:")
client_code(MacFactory())
2. 主题和风格管理

假设我们有一个文本编辑器,可以支持不同的主题和风格,例如浅色主题和深色主题,每种主题包括不同风格的字体和背景。

python 复制代码
from abc import ABC, abstractmethod

# 抽象产品
class Font(ABC):
    @abstractmethod
    def get_font(self):
        pass

class Background(ABC):
    @abstractmethod
    def get_background(self):
        pass

# 具体产品
class LightFont(Font):
    def get_font(self):
        return "Light theme font"

class DarkFont(Font):
    def get_font(self):
        return "Dark theme font"

class LightBackground(Background):
    def get_background(self):
        return "Light theme background"

class DarkBackground(Background):
    def get_background(self):
        return "Dark theme background"

# 抽象工厂
class ThemeFactory(ABC):
    @abstractmethod
    def create_font(self) -> Font:
        pass

    @abstractmethod
    def create_background(self) -> Background:
        pass

# 具体工厂
class LightThemeFactory(ThemeFactory):
    def create_font(self) -> Font:
        return LightFont()

    def create_background(self) -> Background:
        return LightBackground()

class DarkThemeFactory(ThemeFactory):
    def create_font(self) -> Font:
        return DarkFont()

    def create_background(self) -> Background:
        return DarkBackground()

# 客户端代码
def client_code(factory: ThemeFactory):
    font = factory.create_font()
    background = factory.create_background()
    print(font.get_font())
    print(background.get_background())

print("Client: Testing client code with Light theme factory:")
client_code(LightThemeFactory())

print("\nClient: Testing client code with Dark theme factory:")
client_code(DarkThemeFactory())

总结

抽象工厂模式通过为一系列相关对象提供创建接口,使得客户端代码可以独立于这些对象的具体实现。抽象工厂定义了创建产品的方法,而具体工厂实现这些方法来创建具体产品。通过使用抽象工厂模式,系统可以方便地扩展和维护,因为添加新的产品家族只需要定义新的具体工厂,而不需要修改客户端代码。

相关推荐
CF14年老兵1 分钟前
🐍 Python黑魔法手册:让你的代码从能跑到飞起的奇技淫巧
后端·python·trae
天天进步201514 分钟前
Python实战--基于Django的企业资源管理系统
开发语言·python·django
万邦科技Lafite1 小时前
利用淘宝开放API接口监控商品状态,掌握第一信息
大数据·python·电商开放平台·开放api接口·淘宝开放平台
Hy行者勇哥3 小时前
Python 与 VS Code 结合操作指南
开发语言·python
大力水手(Popeye)3 小时前
Pytorch——tensor
人工智能·pytorch·python
饕餮争锋4 小时前
设计模式笔记_行为型_访问者模式
笔记·设计模式·访问者模式
飞翔的佩奇7 小时前
【完整源码+数据集+部署教程】表盘指针检测系统源码和数据集:改进yolo11-CA-HSFPN
python·yolo·计算机视觉·数据集·yolo11·表盘指针检测
larance7 小时前
SQLAlchemy 的异步操作来批量保存对象列表
数据库·python
搏博8 小时前
基于Python3.10.6与jieba库的中文分词模型接口在Windows Server 2022上的实现与部署教程
windows·python·自然语言处理·flask·中文分词
lxmyzzs9 小时前
pyqt5无法显示opencv绘制文本和掩码信息
python·qt·opencv