Factory Pattern —— Creational Class

打印 上一主题 下一主题

主题 934|帖子 934|积分 2802

core

工厂模式(Factory Pattern)是一种创建型设计模式,用于创建对象而不暴露对象创建的逻辑。它将对象的实例化过程封装在一个工厂类中,客户端通过调用工厂类的方法来创建对象,从而实现了解耦和灵活性。
工厂模式的核心思想是将对象的创建与使用分离。客户端不直接实例化对象,而是通过调用工厂类的方法来获取对象实例。工厂类根据客户端的需求,决定实例化哪个具体对象,并将其返回给客户端。
三种工厂模式的识别

简单工厂模式、工厂方法模式和抽象工厂模式都属于创建型设计模式,用于封装对象的创建过程(相同点),但它们之间有明显的区别。
不同点:

  • 简单工厂模式(Simple Factory Pattern):
    简单工厂模式通过一个工厂类来创建产品对象,客户端通过调用工厂类的静态方法或实例方法来获取对象实例。
    工厂类负责根据客户端的需求,决定实例化哪个具体产品类。
    简单工厂模式的主要特点是工厂类集中了对象的创建逻辑,客户端通过工厂类来创建产品对象,而无需直接实例化具体产品类。但是,当需求变化时,需要修改工厂类的代码。
  • 工厂方法模式(Factory Method Pattern):
    工厂方法模式将对象的创建延迟到子类中,每个具体产品类都有对应的工厂类。
    抽象工厂类定义了创建产品对象的抽象方法,每个具体工厂类实现了抽象工厂类,负责创建具体产品的实例。
    客户端通过调用具体工厂类的方法来创建产品对象。客户端可以通过扩展抽象工厂类和具体工厂类来创建新的产品。
  • 抽象工厂模式(Abstract Factory Pattern):
    抽象工厂模式提供了一种创建一系列相关或相互依赖对象的接口,而无需指定具体类。
    抽象工厂模式包括抽象工厂类、具体工厂类、抽象产品类和具体产品类。
    抽象工厂类定义了一组创建产品对象的抽象方法,每个具体工厂类实现了抽象工厂类,负责创建一组相关的具体产品。
    客户端通过调用具体工厂类的方法来创建一组相关的产品对象。
总结:
简单工厂模式适用于创建单一类型的产品,通过一个工厂类来创建产品对象。
工厂方法模式适用于创建一类产品,每个具体产品都有对应的工厂类,通过扩展工厂类来创建新的产品。
抽象工厂模式适用于创建一组相关的产品,每个具体工厂类负责创建一组相关的产品对象。
选择使用哪种模式取决于具体需求和设计目标:

  • 简单工厂模式简单易用,但对于复杂的产品结构不够灵活;
  • 工厂方法模式适用于需要灵活扩展产品类的情况;
  • 抽象工厂模式适用于需要创建一组相关产品 并保持一致性的情况。(抽象工厂模式适用于一组相关的产品对象的创建,如果产品对象之间没有相关性或依赖关系,请考虑使用工厂方法模式。)
Simple Factory Pattern

下面是一个简单的示例代码,说明了简单工厂模式的基本结构:
  1. // 产品接口
  2. interface Product {
  3.     void operation();
  4. }
  5. // 具体产品A
  6. class ConcreteProductA implements Product {
  7.     public void operation() {
  8.         System.out.println("ConcreteProductA operation");
  9.     }
  10. }
  11. // 具体产品B
  12. class ConcreteProductB implements Product {
  13.     public void operation() {
  14.         System.out.println("ConcreteProductB operation");
  15.     }
  16. }
  17. // 工厂类
  18. class Factory {
  19.     public Product createProduct(String type) {
  20.         if (type.equals("A")) {
  21.             return new ConcreteProductA();
  22.         } else if (type.equals("B")) {
  23.             return new ConcreteProductB();
  24.         }
  25.         return null;
  26.     }
  27. }
  28. // 客户端代码
  29. public class Client {
  30.     public static void main(String[] args) {
  31.         Factory factory = new Factory();
  32.         
  33.         // 创建具体产品A
  34.         Product productA = factory.createProduct("A");
  35.         productA.operation();
  36.         
  37.         // 创建具体产品B
  38.         Product productB = factory.createProduct("B");
  39.         productB.operation();
  40.     }
  41. }
复制代码
在上面的示例中,Product 是产品接口,定义了产品的操作方法。ConcreteProductA 和 ConcreteProductB 是具体产品类,实现了产品接口,并定义了各自的操作方法。Factory 是工厂类,负责创建产品对象的方法。客户端通过调用工厂类的方法来创建具体产品的实例,并调用其操作方法。
通过使用工厂模式,客户端可以通过工厂类来创建具体产品的实例,而无需直接依赖具体产品类。这样可以实现代码的解耦和灵活性,使得系统更易于扩展和维护。
Factory Method Pattern

下面是一个简单的示例代码,说明了工厂方法模式的基本结构:
  1. // 抽象产品
  2. interface Product {
  3.     void operation();
  4. }
  5. // 具体产品A
  6. class ConcreteProductA implements Product {
  7.     public void operation() {
  8.         System.out.println("ConcreteProductA operation");
  9.     }
  10. }
  11. // 具体产品B
  12. class ConcreteProductB implements Product {
  13.     public void operation() {
  14.         System.out.println("ConcreteProductB operation");
  15.     }
  16. }
  17. // 抽象工厂
  18. interface Factory {
  19.     Product createProduct();
  20. }
  21. // 具体工厂A
  22. class ConcreteFactoryA implements Factory {
  23.     public Product createProduct() {
  24.         return new ConcreteProductA();
  25.     }
  26. }
  27. // 具体工厂B
  28. class ConcreteFactoryB implements Factory {
  29.     public Product createProduct() {
  30.         return new ConcreteProductB();
  31.     }
  32. }
  33. // 客户端代码
  34. public class Client {
  35.     public static void main(String[] args) {
  36.         Factory factoryA = new ConcreteFactoryA();
  37.         Product productA = factoryA.createProduct();
  38.         productA.operation();
  39.         Factory factoryB = new ConcreteFactoryB();
  40.         Product productB = factoryB.createProduct();
  41.         productB.operation();
  42.     }
  43. }
复制代码
在上面的示例中,Product 是抽象产品接口,定义了产品的操作方法。ConcreteProductA 和 ConcreteProductB 是具体产品类,实现了产品接口,并定义了各自的操作方法。Factory 是抽象工厂接口,定义了创建产品对象的抽象方法。ConcreteFactoryA 和 ConcreteFactoryB 是具体工厂类,实现了抽象工厂接口,负责创建具体产品的实例。
客户端通过调用具体工厂类的方法来创建具体产品的实例,并调用其操作方法。通过使用工厂方法模式,客户端可以通过工厂类来创建产品对象,而无需直接依赖具体产品类。这样可以实现代码的解耦和灵活性,使得系统更易于扩展和维护。
Abstract Factory Pattern

一个常见的实际应用抽象工厂模式的例子是图形用户界面(GUI)库。假设我们正在开发一个跨平台的GUI库,需要支持不同操作系统(如Windows、macOS和Linux)下的窗口和按钮。
在这种情况下,可以使用抽象工厂模式来创建一组相关的产品对象,包括窗口和按钮。首先,定义抽象产品接口:
  1. // 抽象窗口产品
  2. interface Window {
  3.     void render();
  4. }
  5. // 抽象按钮产品
  6. interface Button {
  7.     void click();
  8. }
复制代码
然后,实现具体的窗口和按钮产品类,每个操作系统对应一个具体产品类:
  1. // Windows窗口产品
  2. class WindowsWindow implements Window {
  3.     public void render() {
  4.         System.out.println("Render Windows window");
  5.     }
  6. }
  7. // Windows按钮产品
  8. class WindowsButton implements Button {
  9.     public void click() {
  10.         System.out.println("Click Windows button");
  11.     }
  12. }
  13. //----------------------------------------------------------
  14. // macOS窗口产品
  15. class MacOSWindow implements Window {
  16.     public void render() {
  17.         System.out.println("Render macOS window");
  18.     }
  19. }
  20. // macOS按钮产品
  21. class MacOSButton implements Button {
  22.     public void click() {
  23.         System.out.println("Click macOS button");
  24.     }
  25. }
  26. //----------------------------------------------------------
  27. // Linux窗口产品
  28. class LinuxWindow implements Window {
  29.     public void render() {
  30.         System.out.println("Render Linux window");
  31.     }
  32. }
  33. // Linux按钮产品
  34. class LinuxButton implements Button {
  35.     public void click() {
  36.         System.out.println("Click Linux button");
  37.     }
  38. }
复制代码
接下来,定义抽象工厂接口来创建窗口和按钮:
  1. // 抽象GUI工厂
  2. interface GUIFactory {
  3.     Window createWindow();
  4.     Button createButton();
  5. }
复制代码
然后,实现具体的GUI工厂类,每个操作系统对应一个具体工厂类:
  1. // Windows GUI工厂
  2. class WindowsGUIFactory implements GUIFactory {
  3.     public Window createWindow() {
  4.         return new WindowsWindow();
  5.     }
  6.     public Button createButton() {
  7.         return new WindowsButton();
  8.     }
  9. }
  10. // macOS GUI工厂
  11. class MacOSGUIFactory implements GUIFactory {
  12.     public Window createWindow() {
  13.         return new MacOSWindow();
  14.     }
  15.     public Button createButton() {
  16.         return new MacOSButton();
  17.     }
  18. }
  19. // Linux GUI工厂
  20. class LinuxGUIFactory implements GUIFactory {
  21.     public Window createWindow() {
  22.         return new LinuxWindow();
  23.     }
  24.     public Button createButton() {
  25.         return new LinuxButton();
  26.     }
  27. }
复制代码
现在,客户端可以通过抽象工厂来创建特定操作系统下的窗口和按钮,并使用它们:
  1. public class Client {
  2.     public static void main(String[] args) {
  3.         // 创建Windows风格的GUI
  4.         GUIFactory windowsFactory = new WindowsGUIFactory();
  5.         Window windowsWindow = windowsFactory.createWindow();
  6.         Button windowsButton = windowsFactory.createButton();
  7.         windowsWindow.render();
  8.         windowsButton.click();
  9.         // 创建macOS风格的GUI
  10.         GUIFactory macosFactory = new MacOSGUIFactory();
  11.         Window macosWindow = macosFactory.createWindow();
  12.         Button macosButton = macosFactory.createButton();
  13.         macosWindow.render();
  14.         macosButton.click();
  15.         // 创建Linux风格的GUI
  16.         GUIFactory linuxFactory = new LinuxGUIFactory();
  17.         Window linuxWindow = linuxFactory.createWindow();
  18.         Button linuxButton = linuxFactory.createButton();
  19.         linuxWindow.render();
  20.         linuxButton.click();
  21.     }
  22. }
复制代码
通过使用抽象工厂模式,我们可以轻松地扩展GUI库以支持新的操作系统,只需实现新的具体产品和具体工厂类即可,而不需要修改现有的客户端代码。这样,不同操作系统下的窗口和按钮将保持一致性,并且客户端无需关心具体产品的创建过程,只需使用抽象工厂接口来创建产品对象。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

我可以不吃啊

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表