java設計模式-原廠模式

來源:互聯網
上載者:User

標籤:而不是   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設計模式-原廠模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.