目的
Decouple an abstraction from its implementation so that the two can vary independently.
(將物件組織成樹狀結構、「部分-全體」層級關係,讓外界以一致性的方式對待個別物件和整體物件。[1])
在電腦中最常看到的樹狀結構就是檔案系統中的檔案與資料夾,資料夾像是檔案,可以執行、重新命名、排列⋯⋯。然而資料夾裡面卻可以還有許多檔案或者資料夾,執行資料夾則是打開資料夾。對於檔案系統…
(將實作體系與抽象體系分離開來,讓兩者能各自更動更自演進。[1])
一個抽象體通常會對應數種實作方式,或者是有多種類型,例如 View 會有各式各樣的 View,包括 ScrollView, TableView 等等。每種 View 在不同作業系統上會有不一樣的實作方式。所以如果希望可以跨…
Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.
(將類別的界面轉換成外界所預期的另一種介面,讓原先囿於介面不相容問題而無法協力合作的類別能夠兜在一起用。[1])
Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
(制訂可用原型個體生成的物件類型,爾後只需複製此原型即可生成新物件。[1])
Separate the construction of a complex object from its representation so that the same construction process can create different representations.
(從複雜物件的佈局中抽取出生成程序,以便用同一個生成程序製造各種不同的物件佈局。[1])
Ensure a class only has one instance, and provide a global point of access to it.
(確保類別只會有一個物體實體存在,並提供單一存取窗口。[1])
Singleton 是個非常簡單好懂的 pattern,上述的目的便非常清楚的述說其功用
Design Patterns 的三大工廠:Simple Factory、Factory Method 與 Abstract Factory 這三大工廠都是製造產品的地方,究竟有何差別?
這便是這次所要討論的主題
在此之前,我們先來復習這三個工廠吧
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
(以同一個介面來建立一整族相關或相依的物件,不需點明個物件真正所屬的具象類別。 [1])
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. (定義可資生成物件的介面,但讓子類別去決定該具現出哪一種類別的物件。此模式讓類別將具現化程序交付給子類別去處置。 [1])
在 Design Patterns 中,與 Factory 相關的 Patterns,總共有三個:
這三個彼此有緊密的關係,之後會對這三個做綜合介紹