1.2工厂模式

概述

理解

工厂设计模式(Factory Design Pattern)作为一种创建型设计模式,提供了一种创建对象的接口,但由子类决定要实例化的类是哪一个。工厂方法让类的实例化推迟到子类。

种类

工厂设计模式主要分为三种

  • 简单工厂模式(Simple Factory Pattern)
  • 工厂方法模式(Factory Method Pattern)
  • 抽象工厂模式(Abstract Factory Pattern)

三种工厂设计模式

(1)简单工厂模式(Simple Factory Pattern)

简单工厂模式不属于23种GOF设计模式之一,但它是最简单的工厂模式,常用于创建单一产品
简单工厂模式通过一个工厂类来创建不同类型的对象,客户端无需知道具体的创建细节。

简单工厂模式包含以下几个角色:

  • 抽象产品类(Abstract Product Class):定义产品的接口。抽象产品接口,定义产品的使用方法。

    1
    2
    3
    4
    // 抽象产品类
    public interface Product {
    void use();
    }
  • 具体产品类(Concrete Product Class):实现抽象产品接口的具体类。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    // 具体产品类A
    public class ProductA implements Product {
    @Override
    public void use() {
    System.out.println("Using Product A");
    }
    }

    // 具体产品类B
    public class ProductB implements Product {
    @Override
    public void use() {
    System.out.println("Using Product B");
    }
    }
  • 工厂类(Factory Class):负责创建对象的类。根据传入的类型创建不同的产品实例。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    // 工厂类
    public class SimpleFactory {

    public static Product createProduct(String type) {
    if ("A".equals(type)) {
    return new ProductA();
    } else if ("B".equals(type)) {
    return new ProductB();
    } else {
    throw new IllegalArgumentException("Unknown product type");
    }
    }

    }
  • 使用:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    public class Client {
    public static void main(String[] args) {
    Product productA = SimpleFactory.createProduct("A");
    productA.use();

    Product productB = SimpleFactory.createProduct("B");
    productB.use();
    }
    }

优缺点

优点:

  • 客户端无需知道具体的创建细节,只需通过工厂类获取产品。
  • 集中管理对象的创建逻辑,便于维护和扩展。

缺点:

  • 工厂类集中了所有产品的创建逻辑,违反了单一职责原则
  • 增加新产品时需要修改工厂类,违反了开闭原则(对扩展开放,对修改封闭。换句话说,当需要新增功能时,可以通过添加新代码来实现,而不必修改已有的代码。)

(2)工厂方法模式(Factory Method Pattern)

工厂方法模式定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个。工厂方法让类的实例化推迟到子类。

工厂方法模式包含以下几个角色:

  • 抽象产品类(Abstract Product Class):定义产品的接口。抽象产品接口,定义产品的使用方法。

    1
    2
    3
    4
    // 抽象产品类
    public interface Product {
    void use();
    }
  • 具体产品类(Concrete Product Class):实现抽象产品接口的具体类。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    // 具体产品类A
    public class ProductA implements Product {
    @Override
    public void use() {
    System.out.println("Using Product A");
    }
    }

    // 具体产品类B
    public class ProductB implements Product {
    @Override
    public void use() {
    System.out.println("Using Product B");
    }
    }
  • 抽象工厂类(Abstract Factory Class):定义创建产品的接口。

    1
    2
    3
    4
    // 抽象工厂类
    public interface Factory {
    Product createProduct();
    }
  • 具体工厂类(Concrete Factory Class):实现抽象工厂接口,创建具体产品。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    // 具体工厂类A
    public class FactoryA implements Factory {
    @Override
    public Product createProduct() {
    return new ProductA();
    }
    }

    // 具体工厂类B
    public class FactoryB implements Factory {
    @Override
    public Product createProduct() {
    return new ProductB();
    }
    }
  • 使用:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public class Client {
    public static void main(String[] args) {
    Factory factoryA = new FactoryA();
    Product productA = factoryA.createProduct();
    productA.use();

    Factory factoryB = new FactoryB();
    Product productB = factoryB.createProduct();
    productB.use();
    }
    }

优缺点

优点:

  • 符合开闭原则,增加新产品时只需增加新的具体工厂类和产品类,无需修改现有代码。
  • 符合单一职责原则,每个具体工厂类只负责创建一种产品。

缺点:

  • 增加了类的数量,增加了系统的复杂度。
  • 客户端需要知道具体的工厂类,增加了客户端的复杂度。

(3)抽象工厂模式(Abstract Factory Pattern)

抽象工厂模式提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。抽象工厂模式适用于创建一组相关的产品族

抽象工厂模式包含以下几个角色:

  • 抽象产品类(Abstract Product Class):定义产品的接口。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    // 抽象产品类A
    public interface ProductA {
    void useA();
    }

    // 抽象产品类B
    public interface ProductB {
    void useB();
    }
  • 具体产品类(Concrete Product Class):实现抽象产品接口的具体类。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    // 具体产品类A1
    public class ProductA1 implements ProductA {
    @Override
    public void useA() {
    System.out.println("Using Product A1");
    }
    }

    // 具体产品类A2
    public class ProductA2 implements ProductA {
    @Override
    public void useA() {
    System.out.println("Using Product A2");
    }
    }

    // 具体产品类B1
    public class ProductB1 implements ProductB {
    @Override
    public void useB() {
    System.out.println("Using Product B1");
    }
    }

    // 具体产品类B2
    public class ProductB2 implements ProductB {
    @Override
    public void useB() {
    System.out.println("Using Product B2");
    }
    }
  • 抽象工厂类(Abstract Factory Class):定义创建一组产品的接口。

    1
    2
    3
    4
    5
    // 抽象工厂类
    public interface AbstractFactory {
    ProductA createProductA();
    ProductB createProductB();
    }
  • 具体工厂类(Concrete Factory Class):实现抽象工厂接口,创建一组具体产品。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    // 具体工厂类1
    public class ConcreteFactory1 implements AbstractFactory {
    @Override
    public ProductA createProductA() {
    return new ProductA1();
    }

    @Override
    public ProductB createProductB() {
    return new ProductB1();
    }
    }

    // 具体工厂类2
    public class ConcreteFactory2 implements AbstractFactory {
    @Override
    public ProductA createProductA() {
    return new ProductA2();
    }

    @Override
    public ProductB createProductB() {
    return new ProductB2();
    }
    }
  • 使用

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    public class Client {
    public static void main(String[] args) {
    AbstractFactory factory1 = new ConcreteFactory1();
    ProductA productA1 = factory1.createProductA();
    ProductB productB1 = factory1.createProductB();
    productA1.useA();
    productB1.useB();

    AbstractFactory factory2 = new ConcreteFactory2();
    ProductA productA2 = factory2.createProductA();
    ProductB productB2 = factory2.createProductB();
    productA2.useA();
    productB2.useB();
    }
    }

4.5 优缺点

优点:

  • 符合开闭原则,增加新产品族时只需增加新的具体工厂类和产品类,无需修改现有代码。
  • 符合单一职责原则,每个具体工厂类只负责创建一组相关的产品。

缺点:

  • 增加了类的数量,增加了系统的复杂度。
  • 客户端需要知道具体的工厂类,增加了客户端的复杂度。

五、应用场景

简单工厂模式的应用场景

  • 创建对象的逻辑简单,且不经常变化。
  • 客户端不需要知道具体的创建细节。

工厂方法模式的应用场景

  • 创建对象的逻辑复杂,且需要灵活扩展。
  • 符合开闭原则,便于维护和扩展。

抽象工厂模式的应用场景

  • 需要创建一组相关的产品族
  • 符合开闭原则,便于维护和扩展。
Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

扫一扫,分享到微信

微信分享二维码
  • Copyrights © 2023-2025 Annie
  • Visitors: | Views:

嘿嘿 请我吃小蛋糕吧~

支付宝
微信