標籤:而不是 created 構造 weight 並且 wan end delegate 責任
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
> 在基於類的編程中,Factory 方法模式是一種建立模式,它使用Factory 方法來處理建立對象的問題,而無需指定將建立的對象的確切類別。
這是通過調用一個Factory 方法建立對象來完成的,這個方法要麼在介面中指定,要麼由子類實現,要麼在基類中實現,並且可以被衍生類別重寫,而不是通過調用建構函式。
> 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.
Use the Factory Method pattern when
使用Factory 方法模式:
* 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
* 類將責任委派給幾個助手子類之一,並且您想本地化哪些助手子類是委託的知識
java設計模式-原廠模式