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

文章目录

抽象工厂模式(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())

总结

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

相关推荐
nbsaas-boot1 小时前
Java 正则表达式白皮书:语法详解、工程实践与常用表达式库
开发语言·python·mysql
仗剑_走天涯1 小时前
基于pytorch.nn模块实现线性模型
人工智能·pytorch·python·深度学习
chao_7891 小时前
二分查找篇——搜索旋转排序数组【LeetCode】两次二分查找
开发语言·数据结构·python·算法·leetcode
chao_7896 小时前
二分查找篇——搜索旋转排序数组【LeetCode】一次二分查找
数据结构·python·算法·leetcode·二分查找
烛阴6 小时前
Python装饰器解除:如何让被装饰的函数重获自由?
前端·python
noravinsc6 小时前
django 一个表中包括id和parentid,如何通过parentid找到全部父爷id
python·django·sqlite
ajassi20006 小时前
开源 python 应用 开发(三)python语法介绍
linux·python·开源·自动化
沉默媛7 小时前
如何安装python以及jupyter notebook
开发语言·python·jupyter
ffcf7 小时前
设计模式—专栏简介
设计模式
Deng9452013148 小时前
基于Python的旅游数据可视化应用
python·numpy·pandas·旅游·数据可视化技术