设计模式4——工厂方法模式(factory-method)

一、工厂方法模式说明

layout title folder permalink categories tags

pattern

Factory Method

factory-method

/patterns/factory-method/

Creational

Java

Difficulty-Beginner

Gang Of Four

布局 标题 文件夹 永久链接 分类 tags

设计模式

工厂方法模式

factory-method

/patterns/factory-method/

构建类

Java

Difficulty-Beginner

Gang Of Four

Also known as

也被称为

Virtual Constructor

虚拟构造器

Intent

含义

Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

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

Explanation

解释

Real world example

真实世界案例

Blacksmith manufactures weapons. Elves require Elvish weapons and orcs require Orcish weapons. Depending on the customer at hand the right type of blacksmith is summoned.

铁匠制造武器。精灵需要精灵的武器,兽人需要兽人的武器。取决于铁匠被谁召见

In plain words

通俗地讲:

It provides a way to delegate the instantiation logic to child classes.

它提供了一种将实例化逻辑委派给子类的方式

Wikipedia says

维基百科说明

In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created. This is done by creating objects by calling a factory method—either specified in an interface and implemented by child classes, or implemented in a base class and optionally overridden by derived classes—rather than by calling a constructor.

在基于类的编程中,工厂方法模式是一种构建模式,使用工厂方法处理创建对象的相关问题,它不用给将要创建的对象指定确切的类。通过叫做工厂方法的方式创建对象可以实现,要么在接口中指定并被子类实现,要么在一个基础类中实现并在衍生类中选择性重写而不是调用构造器。

Programmatic Example

编程案例

Taking our blacksmith example above. First of all we have a blacksmith interface and some implementations for it

以上述的铁匠案例来看。首先,我们要有一个铁匠接口和它的一些实现。

public interface Blacksmith {
  Weapon manufactureWeapon(WeaponType weaponType);
}

public class ElfBlacksmith implements Blacksmith {
  public Weapon manufactureWeapon(WeaponType weaponType) {
    return new ElfWeapon(weaponType);
  }
}

public class OrcBlacksmith implements Blacksmith {
  public Weapon manufactureWeapon(WeaponType weaponType) {
    return new OrcWeapon(weaponType);
  }
}

Now as the customers come the correct type of blacksmith is summoned and requested weapons are manufactured

现在随着顾客来确定召唤准确类别的铁匠,之后被要求的武器就可以生产了。

Blacksmith blacksmith = new ElfBlacksmith();
blacksmith.manufactureWeapon(WeaponType.SPEAR);
blacksmith.manufactureWeapon(WeaponType.AXE);
// Elvish weapons are created

Applicability

应用

Use the Factory Method pattern when

使用工厂方法模式,当:

  • a class can't anticipate the class of objects it must create
  • a class wants its subclasses to specify the objects it creates
  • classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate
  • 一个类不能预料到它必须创建哪种类的对象。
  • 一个类想要它的子类来指定它要创建的对象
  • 类委派他们的职责给数个助理类中的一个,而你想要局部化究竟是哪个助理类被委派的信息。

Presentations

展示

Real world examples

真实世界案例

Credits

参考书

二、工厂方法模式讲解

1,一行一行读代码

上面讲了这个模式的概念,一看说明这么少就知道,这个模式相对简单,来看一下它的调用过程:

    // Lets go to war with Orc weapons
    App app = new App(new OrcBlacksmith());
    app.manufactureWeapons();

我理解这里的工厂是OrcBlacksmith,当APP想要制造对象(制造武器)时,先指定具体工厂,然后manufactureWeapons()通过调用这个具体的工厂类实现生产武器。

仔细看了两遍工厂方法模式和抽象工厂模式,他们基本都是一样的,要创建对象,通过一个中间的抽象类(接口)隐藏具体工厂实现,然后调用具体工厂的实现方法来创建对象。

按照之前博客的观点:

两者的区别:
工厂方法模式只有一个抽象产品类,而抽象工厂模式有多个。   
工厂方法模式的具体工厂类只能创建一个具体产品类的实例,而抽象工厂模式可以创建多个。

总之我的理解是抽象工厂创建的对象是以主题(theme)为基础,创建了一系列的产品。

猜你喜欢

转载自blog.csdn.net/qq_22059611/article/details/85386780