• 设计模式-抽象工厂模式(Abstract Factory Pattern)结构|原理|优缺点|场景|示例


                                        设计模式(分类)        设计模式(六大原则)   

        创建型(5种)        工厂方法         抽象工厂模式        单例模式        建造者模式        原型模式

        结构型(7种)        适配器模式        装饰器模式        代理模式        ​​​​​​外观模式      桥接模式        组合模式       享元模式

        行为型(11种)       策略模式        模板方法模式        观察者模式        迭代器模式        责任链模式        命令模式

                                       备忘录模式          状态模式          访问者模式        中介者模式    


    抽象工厂模式(Abstract Factory Pattern)是一种创建型设计模式,它提供了一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。这种模式的核心目的是将一组相关产品的创建过程封装起来,使得客户端可以使用一个统一的接口来创建这些产品家族中的任何产品,而无需关心具体的实现细节。抽象工厂模式特别适用于处理具有产品族的产品结构,其中产品族是指位于不同等级结构中的一组产品,它们之间可能存在一定的依赖或约束关系。

    模式结构

    抽象工厂模式通常包含以下角色:

    1. 抽象工厂(Abstract Factory):定义了一个创建产品族的接口,声明了一系列创建不同产品的方法。这些方法返回的是相应产品接口的引用。

    2. 具体工厂(Concrete Factory):实现了抽象工厂接口,负责生成一个或多个具体产品族中的产品对象。

    3. 抽象产品(Abstract Product):定义了产品族中每种产品的公共接口,它是所有具体产品类的基类或接口。

    4. 具体产品(Concrete Product):实现了抽象产品接口,是产品族中每种产品的具体实现。

    工作原理

    • 客户端:通过调用具体工厂的接口来创建所需的产品对象,无需直接实例化具体产品类。
    • 具体工厂:实现了抽象工厂接口中定义的创建产品的方法,根据工厂的类型返回相应具体产品族中的产品对象。
    • 抽象产品:为产品对象定义了通用的接口,使得客户端可以使用抽象产品类型的引用来处理各种具体产品对象,无需关心具体产品类型。
    • 具体产品:实现了抽象产品的接口,提供了具体产品的功能实现。

    优缺点

    优点
    • 封装性:将一组相关产品的创建过程封装在一起,客户端无需了解具体产品的创建细节。
    • 一致性:提供一个统一的接口来创建相关或依赖的对象,保持产品族内部的一致性。
    • 灵活性与扩展性:当需要增加新的产品族时,只需增加一个新的具体工厂和相关产品类即可,不影响已有代码,符合“开闭原则”。
    • 隔离变化:通过抽象工厂隔离了高层模块与具体产品的依赖关系,使得高层模块无需关心产品族内部的变化。
    缺点
    • 增加复杂性:对于简单系统,引入抽象工厂可能会增加不必要的复杂性。
    • 产品族扩展困难:如果产品族中添加新产品,不仅需要修改抽象工厂和具体工厂,还可能影响到使用抽象工厂的客户端代码。
    • 类的层级结构复杂:随着产品种类的增加,可能会导致类的层级结构变得复杂。

    适用场景

    • 产品族内有多组相关产品:当一个系统需要创建一系列相关或依赖的产品对象时,且这些产品之间存在一定的约束或兼容性要求,抽象工厂模式非常适合。
    • 需要隔离高层模块与具体产品的依赖:如果希望高层模块不直接依赖具体产品的创建过程,可以通过抽象工厂来封装创建细节。
    • 系统需要支持多种产品系列:如果有多种产品系列(如不同操作系统上的图形界面组件),且希望在不修改客户端代码的情况下引入新的产品系列,可以使用抽象工厂模式。

    代码示例(以Java为例)

    1. // 抽象产品(操作系统相关的UI组件接口)
    2. interface Button {
    3. void display();
    4. }
    5. interface TextField {
    6. void display();
    7. }
    8. // 具体产品(Windows平台的UI组件)
    9. class WindowsButton implements Button {
    10. @Override
    11. public void display() {
    12. System.out.println("Displaying Windows button");
    13. }
    14. }
    15. class WindowsTextField implements TextField {
    16. @Override
    17. public void display() {
    18. System.out.println("Displaying Windows text field");
    19. }
    20. }
    21. // 具体产品(MacOS平台的UI组件)
    22. class MacOSButton implements Button {
    23. @Override
    24. public void display() {
    25. System.out.println("Displaying MacOS button");
    26. }
    27. }
    28. class MacOSTextField implements TextField {
    29. @Override
    30. public void display() {
    31. System.out.println("Displaying MacOS text field");
    32. }
    33. }
    34. // 抽象工厂
    35. interface UIFactory {
    36. Button createButton();
    37. TextField createTextField();
    38. }
    39. // 具体工厂(Windows平台UI工厂)
    40. class WindowsUIFactory implements UIFactory {
    41. @Override
    42. public Button createButton() {
    43. return new WindowsButton();
    44. }
    45. @Override
    46. public TextField createTextField() {
    47. return new WindowsTextField();
    48. }
    49. }
    50. // 具体工厂(MacOS平台UI工厂)
    51. class MacOSUIFactory implements UIFactory {
    52. @Override
    53. public Button createButton() {
    54. return new MacOSButton();
    55. }
    56. @Override
    57. public TextField createTextField() {
    58. return new MacOSTextField();
    59. }
    60. }
    61. // 客户端代码
    62. public class Main {
    63. public static void main(String[] args) {
    64. UIFactory windowsFactory = new WindowsUIFactory();
    65. UIFactory macosFactory = new MacOSUIFactory();
    66. Button windowsButton = windowsFactory.createButton();
    67. TextField windowsTextField = windowsFactory.createTextField();
    68. windowsButton.display();
    69. windowsTextField.display();
    70. Button macosButton = macosFactory.createButton();
    71. TextField macosTextField = macosFactory.createTextField();
    72. macosButton.display();
    73. macosTextField.display();
    74. }
    75. }

     在这个Java示例中:

    • ButtonTextField接口作为抽象产品,定义了UI组件的基本行为。
    • WindowsButtonWindowsTextFieldMacOSButtonMacOSTextField类是具体产品,它们分别实现了对应平台的UI组件功能。
    • UIFactory接口作为抽象工厂,定义了创建按钮和文本字段的方法。
    • WindowsUIFactoryMacOSUIFactory类是具体工厂,它们实现了UIFactory接口,分别创建对应平台的UI组件。
    • Main类的main方法中,客户端代码通过创建不同平台的工厂对象,然后调用工厂方法来创建所需UI组件,无需关心组件的具体实现细节。通过切换不同的工厂对象,可以轻松地在不同平台的UI风格之间切换。

    这个Java示例展示了抽象工厂模式的应用,通过抽象工厂创建相关产品族中的对象,使得客户端代码与具体产品的实现细节解耦,增强了系统的灵活性和可扩展性。当需要支持新的操作系统平台时,只需添加新的具体工厂和UI组件类即可,无需修改已有的客户端代码。

    代码示例(以Python为例)

    1. # 抽象产品(操作系统相关的UI组件)
    2. class Button:
    3. def display(self):
    4. raise NotImplementedError("Subclasses must implement this abstract method")
    5. class TextField:
    6. def display(self):
    7. raise NotImplementedError("Subclasses must implement this abstract method")
    8. # 具体产品(Windows平台的UI组件)
    9. class WindowsButton(Button):
    10. def display(self):
    11. return "Displaying Windows button"
    12. class WindowsTextField(TextField):
    13. def display(self):
    14. return "Displaying Windows text field"
    15. # 具体产品(MacOS平台的UI组件)
    16. class MacOSButton(Button):
    17. def display(self):
    18. return "Displaying MacOS button"
    19. class MacOSTextField(TextField):
    20. def display(self):
    21. return "Displaying MacOS text field"
    22. # 抽象工厂
    23. class UIFactory:
    24. def create_button(self):
    25. raise NotImplementedError("Subclasses must implement this abstract method")
    26. def create_text_field(self):
    27. raise NotImplementedError("Subclasses must implement this abstract method")
    28. # 具体工厂(Windows平台UI工厂)
    29. class WindowsUIFactory(UIFactory):
    30. def create_button(self):
    31. return WindowsButton()
    32. def create_text_field(self):
    33. return WindowsTextField()
    34. # 具体工厂(MacOS平台UI工厂)
    35. class MacOSUIFactory(UIFactory):
    36. def create_button(self):
    37. return MacOSButton()
    38. def create_text_field(self):
    39. return MacOSTextField()
    40. # 客户端代码
    41. def main():
    42. windows_factory = WindowsUIFactory()
    43. macos_factory = MacOSUIFactory()
    44. button = windows_factory.create_button()
    45. text_field = windows_factory.create_text_field()
    46. print(button.display())
    47. print(text_field.display())
    48. button = macos_factory.create_button()
    49. text_field = macos_factory.create_text_field()
    50. print(button.display())
    51. print(text_field.display())
    52. if __name__ == "__main__":
    53. main()

    在这个Python示例中:

    • ButtonTextField类作为抽象产品,定义了UI组件的基本接口。
    • WindowsButtonWindowsTextFieldMacOSButtonMacOSTextField类是具体产品,它们分别实现了对应平台的UI组件功能。
    • UIFactory类作为抽象工厂,定义了创建按钮和文本字段的方法。
    • WindowsUIFactoryMacOSUIFactory类是具体工厂,它们实现了UIFactory接口,分别创建对应平台的UI组件。
    • 客户端代码中,通过创建不同平台的工厂对象,然后调用工厂方法来创建所需UI组件,无需关心组件的具体实现细节。通过切换不同的工厂对象,可以轻松地在不同平台的UI风格之间切换。

    这个例子展示了抽象工厂模式在Python中的应用,通过抽象工厂创建相关产品族中的对象,使得客户端代码与具体产品的实现细节解耦,增强了系统的灵活性和可扩展性。当需要支持新的操作系统平台时,只需添加新的具体工厂和UI组件类即可,无需修改已有的客户端代码。 

  • 相关阅读:
    使用 Redis 作为缓存的 Spring Boot 应用
    SpringBoot事务详解
    项目二--01:基于nginx、keepalived的高可用集群之负载均衡
    18. Spring类型转换之ConversionService
    NVIDIA 7th SkyHackathon(八)使用 Flask 与 Vue 开发 Web
    点云从入门到精通技术详解100篇-基于 Kinect v2 相机的柑橘点云分割与配准(续)
    web前端零基础入门5
    JAVA毕业设计扶贫产品销售计算机源码+lw文档+系统+调试部署+数据库
    JAVA毕业设计web家教信息服务平台设计与实现计算机源码+lw文档+系统+调试部署+数据库
    单商户商城系统功能拆解20—售后订单
  • 原文地址:https://blog.csdn.net/piaomiao_/article/details/137908266