设计模式:工厂 vs 工厂方法 vs 抽象工厂 [英] Design Patterns: Factory vs Factory method vs Abstract Factory

查看:27
本文介绍了设计模式:工厂 vs 工厂方法 vs 抽象工厂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从一个网站上阅读设计模式

I was reading design patterns from a website

在那里我读到了工厂、工厂方法和抽象工厂,但它们太混乱了,定义不清楚.根据定义

There I read about Factory, Factory method and Abstract factory but they are so confusing, am not clear on the definition. According to definitions

Factory - 在不向客户端公开实例化逻辑的情况下创建对象,并通过公共接口引用新创建的对象.是工厂方法的简化版

Factory - Creates objects without exposing the instantiation logic to the client and Refers to the newly created object through a common interface. Is a simplified version of Factory Method

Factory Method - 定义一个用于创建对象的接口,但让子类决定实例化哪个类并通过一个公共接口引用新创建的对象.

Factory Method - Defines an interface for creating objects, but let subclasses to decide which class to instantiate and Refers to the newly created object through a common interface.

抽象工厂 - 提供用于创建相关对象系列的接口,无需明确指定它们的类.

Abstract Factory - Offers the interface for creating a family of related objects, without explicitly specifying their classes.

我还查看了其他关于抽象工厂与工厂方法的 stackoverflow 线程,但那里绘制的 UML 图让我的理解更糟.

I also looked the other stackoverflow threads regarding Abstract Factory vs Factory Method but the UML diagrams drawn there make my understanding even worse.

谁能告诉我

  1. 这三种模式有何不同?
  2. 何时使用哪个?
  3. 如果可能,还有与这些模式相关的 Java 示例吗?

推荐答案

所有三个 Factory 类型都做同样的事情:它们是一个智能构造函数".

All three Factory types do the same thing: They are a "smart constructor".

假设您希望能够创建两种水果:Apple 和 Orange.

Let's say you want to be able to create two kinds of Fruit: Apple and Orange.

class FruitFactory {

  public Apple makeApple() {
    // Code for creating an Apple here.
  }

  public Orange makeOrange() {
    // Code for creating an orange here.
  }

}

用例:构造 Apple 或 Orange 有点太复杂,无法在构造函数中处理.

Use case: Constructing an Apple or an Orange is a bit too complex to handle in the constructor for either.

abstract class FruitPicker {

  protected abstract Fruit makeFruit();

  public void pickFruit() {
    private final Fruit f = makeFruit(); // The fruit we will work on..
    <bla bla bla>
  }
}

...然后您可以通过在子类中实现工厂方法来重用 FruitPicker.pickFruit() 中的通用功能:

...then you can reuse the common functionality in FruitPicker.pickFruit() by implementing a factory method in subclasses:

class OrangePicker extends FruitPicker {

  @Override
  protected Fruit makeFruit() {
    return new Orange();
  }
}

抽象工厂

抽象工厂通常用于依赖注入/策略之类的事情,当您希望能够创建需要相同类型"的整个对象系列并具有一些公共基类时.这是一个与水果相关的模糊示例.这里的用例是我们要确保我们不会意外地在 Apple 上使用 OrangePicker.只要我们从同一家工厂拿到我们的 Fruit 和 Picker,它们就会匹配.

Abstract Factory

Abstract factory is normally used for things like dependency injection/strategy, when you want to be able to create a whole family of objects that need to be of "the same kind", and have some common base classes. Here's a vaguely fruit-related example. The use case here is that we want to make sure that we don't accidentally use an OrangePicker on an Apple. As long as we get our Fruit and Picker from the same factory, they will match.

interface PlantFactory {
  
  Plant makePlant();

  Picker makePicker(); 

}

public class AppleFactory implements PlantFactory {
  Plant makePlant() {
    return new Apple();
  }

  Picker makePicker() {
    return new ApplePicker();
  }
}

public class OrangeFactory implements PlantFactory {
  Plant makePlant() {
    return new Orange();
  }

  Picker makePicker() {
    return new OrangePicker();
  }
}

这篇关于设计模式:工厂 vs 工厂方法 vs 抽象工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆