1. 程式人生 > >設計模式4——工廠方法模式(factory-method)

設計模式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)為基礎,建立了一系列的產品。