簡單工廠模式
簡單工廠模式(Simple Factory Pattern) :定義一個工廠類,它可以根據參數的不同返回不同類的實例,被創建的實例通常都具有共同的父類。因為在簡單工廠模式中用于創建實例的方法是靜態(static)方法,因此簡單工廠模式又被稱為靜態工廠方法(Static Factory Method)模式,它屬于類創建型模式。
簡單工廠需要:
- 工廠結構體
- 產品接口
- 產品結構體
示例說明
創建一個餃子店工廠結構體,和餃子類的接口。該工廠的其中一個方法用來生產不同口味的餃子,如韭菜的豬肉餡的。
type DumplingsShop struct{
Generate(t string) *Dumplings
}
type Dumplingsinterface interface {
create()
}
創建肉餡和韭菜餡的餃子結構體,并且實現對應接口的方法。
type DumplingsMeat struct{}
func (* DumplingsMeat)create(){
fmt.Println("DumplingsMeat create")
}
type DumplingsChives struct{}
func (* DumplingsChives)create(){
fmt.Println("DumplingsMeat create")
}
func(* DumplingsShop)Create(type string)*Dumplings{
switch type {
case "meat":
return new(DumplingsMeat)
case "chives":
return new(DumplingsChives)
default:
return nil
}
}
工廠實例化調用
var type string
dumplingFactory := DumplingsShop{}
type = "meat"
meat := dumplingFactory.Create(type)//返回肉餡餃子對象
meat.create()
type = "chives"
chives := dumplingFactory.Create(type) //返回韭菜餡餃子對象
chives.create()
簡單工廠模式優缺點
優點: 工廠類是整個工廠模式的核心,我們只需要傳入給定的信息,就可以創建所需實例,在多人協作的時候,無需知道對象之間的內部依賴,可以直接創建,有利于整個軟件體系結構的優化。
缺點: 工廠類中包含了所有實例的創建邏輯,一旦這個工廠類出現問題,所有實例都會受到影響,并且,工廠類中生產的產品都基于一個共同的接口,一旦要添加不同種類的產品,這就會增加工廠類的復雜度,將不同種類的產品混合在一起,違背了單一職責,系統的靈活性和可維護性都會降低,并且當新增產品的時候,必須要修改工廠類,違背了『系統對擴展開放,對修改關閉』的原則。
工廠方法模式
工廠方法模式(英語:Factory method pattern) 是一種實現了“工廠”概念的面向對象設計模式。就像其他創建型模式一樣,它也是處理在不指定對象具體類型的情況下創建對象的問題。工廠方法模式的實質是“定義一個創建對象的接口,但讓實現這個接口的類來決定實例化哪個類。工廠方法讓類的實例化推遲到子類中進行。”,實際應用中工廠方法模式針對的是一個產品等級結構。
工廠方法需要:
- 工廠接口
- 工廠結構體
- 產品接口
- 產品結構體
示例說明
創建一個餃子店工廠接口,和餃子類的接口。該工廠用來生產不同口味的餃子,如韭菜的豬肉餡的。
type DumplingsShopinterface interface{
Generate(t string) *Dumplings
}
type Dumplingsinterface interface {
create()
}
創建北京和西安對應餡的餃子結構體,并且實現對應接口的方法。
type BeijingDumplingsMeat struct{}
func (* BeijingDumplingsMeat)create(){
fmt.Println("BeijingDumplingsMeat create")
}
type BeijingDumplingsChives struct{}
...
type XianDumplingsMeat struct{}
...
type XianDumplingsChives struct{}
...
創建北京和西安工廠
type BeijingDumplings struct{}
type XianDumplings struct{}
func(* BeijingDumplings)Generate(t string) *Dumplings{
switch t {
case "chives" :
return new(BeijingDumplingsChives)
case "meat" :
return new(BeijingDumplingsMeat)
default:
return nil
}
}
func(* XianDumplings)Generate(t string) *Dumplings{
switch t{}
case "chives" :
return new(XianDumplingsChives)
case "meat" :
return new(XianDumplingsMeat)
default:
return nil
}
工廠實例化調用
var DumplingsShopFactory DumplingsShopinterface
DumplingsShopFactory := new(BeijingDumplings)
b = DumplingsShopFactory.Generate("meat") // 傳入肉餡的參數,會返回北京市的肉餡餃子
b.create()
DumplingsShopFactory := new(XianDumplings)
b = DumplingsShopFactory.Generate("meat") // 同樣傳入肉餡的參數,會返回北京市的肉餡餃子
b.create()
工廠方法模式的優缺點
優點: 符合“開閉”原則,具有很強的的擴展性、彈性和可維護性。修改時只需要添加對應的工廠類即可使用了依賴倒置原則,依賴抽象而不是具體,使用(客戶)和實現(具體類)松耦合。客戶只需要知道所需產品的具體工廠,而無須知道具體工廠的創建產品的過程,甚至不需要知道具體產品的類名。
缺點: 每增加一個產品時,都需要一個具體類和一個具體創建者,使得類的個數成倍增加,導致系統類數目過多,復雜性增加對簡單工廠,增加功能修改的是工廠類;對工廠方法,增加功能修改的是產品類。
抽象工廠模式
抽象工廠模式(英語:Abstract factory pattern) 是一種軟件開發設計模式。抽象工廠模式提供了一種方式,可以將一組具有同一主題的單獨的工廠封裝起來。在正常使用中,客戶端程序需要創建抽象工廠的具體實現,然后使用抽象工廠作為接口來創建這一主題的具體對象。客戶端程序不需要知道(或關心)它從這些內部的工廠方法中獲得對象的具體類型,因為客戶端程序僅使用這些對象的通用接口。抽象工廠模式將一組對象的實現細節與他們的一般使用分離開來。實際應用中針對的多個產品等級結構。
示例說明
創建一個肉餡餃子店工廠接口和韭菜餡餃子店工廠接口,以及餃子類的接口。
type DumplingsShopinterface interface{
GenerateMeatDumpling() *Dumplingsinterface
GenerateChivesDumpling() *Dumplingsinterface
}
type Dumplingsinterface interface {
create()
}
實現北京肉餡餃子、北京韭菜餃子、西安肉餡餃子、西安韭菜餃子4中實例對象。
type BeijingDumplingsMeat struct{}
func (* BeijingDumplingsMeat)create(){
fmt.Println("BeijingDumplingsMeat create")
}
type BeijingDumplingsChives struct{}
...
type XianDumplingsMeat struct{}
...
type XianDumplingsChives struct{}
...
創建北京和西安工廠
type BeijingDumplingsFactory struct{}
type XianDumplingsFactory struct{}
func(* BeijingDumplingsFactory)GenerateMeatDumpling() *Dumplings{
return new(BeijingDumplingsMeat)
}
}
func(* BeijingDumplingsFactory)GenerateChivesDumpling() *Dumplings{
return new(BeijingDumplingsChives)
}
}
func(* XianDumplingsFactory)GenerateMeatDumpling() *Dumplings{
return new(XianDumplingsMeat)
}
}
func(* XianDumplingsFactory)GenerateChivesDumpling() *Dumplings{
return new(XianDumplingsChives)
}
}
工廠示例調用
var DumplingsShopFactory DumplingsShopinterface
DumplingsShopFactory := new(BeijingDumplingsFactory)
b = DumplingsShopFactory.GenerateMeatDumpling() // 傳入肉餡的參數,會返回北京市的肉餡餃子
b.create()
...
抽象工廠模式的優缺點
優點: 抽象工廠模式除了具有工廠方法模式的優點外,最主要的優點就是可以在類的內部對產品族進行約束。所謂的產品族,一般或多或少的都存在一定的關聯,抽象工廠模式就可以在類內部對產品族的關聯關系進行定義和描述,而不必專門引入一個新的類來進行管理。
缺點: 產品族的擴展將是一件十分費力的事情,假如產品族中需要增加一個新的產品,則幾乎所有的工廠類都需要進行修改。所以使用抽象工廠模式時,對產品等級結構的劃分是非常重要的