Golang實(shí)現(xiàn)組合模式和裝飾模式實(shí)例詳解
本文介紹組合模式和裝飾模式,golang實(shí)現(xiàn)兩種模式有共同之處,但在具體應(yīng)用場(chǎng)景有差異。通過(guò)對(duì)比兩個(gè)模式,可以加深理解。
組合模式
組合是一種結(jié)構(gòu)設(shè)計(jì)模式,它允許將對(duì)象組合成樹狀結(jié)構(gòu),并將其作為單一對(duì)象使用。對(duì)于需要構(gòu)建樹形結(jié)構(gòu)的大多數(shù)問(wèn)題,組合結(jié)構(gòu)成為常用的解決方案,它最大特性是能夠在整個(gè)樹結(jié)構(gòu)上遞歸運(yùn)行方法并對(duì)結(jié)果進(jìn)行匯總。
這里通過(guò)操作系統(tǒng)的文件系統(tǒng)來(lái)理解Composite模式。在文件系統(tǒng)中有兩種類型的對(duì)象: 文件和文件夾。有些情況下文件和文件夾應(yīng)該以相同的方式對(duì)待。這就是Composite模式派上用場(chǎng)的地方。
假設(shè)您需要在文件系統(tǒng)中對(duì)特定的關(guān)鍵字進(jìn)行搜索。此搜索操作同時(shí)適用于文件和文件夾。對(duì)于一個(gè)文件,它只會(huì)查看文件的內(nèi)容;對(duì)于一個(gè)文件夾,它將遍歷該文件夾的所有文件以找到該關(guān)鍵字。下面通過(guò)實(shí)例進(jìn)行說(shuō)明。
component.go
定義節(jié)點(diǎn)類型:
package main type Component interface { search(string) }
file.go
定義文件類型節(jié)點(diǎn),實(shí)現(xiàn)search方法:
package main import "fmt" type File struct { name string } func (f *File) search(keyword string) { fmt.Printf("Searching for keyword %s in file %s\n", keyword, f.name) } func (f *File) getName() string { return f.name }
folder.go
定義文件夾類型節(jié)點(diǎn),也實(shí)現(xiàn)search方法:
package main import "fmt" type Folder struct { components []Component name string } func (f *Folder) search(keyword string) { fmt.Printf("Serching recursively for keyword %s in folder %s\n", keyword, f.name) for _, composite := range f.components { composite.search(keyword) } } func (f *Folder) add(c Component) { f.components = append(f.components, c) }
組合測(cè)試
定義main.go文件進(jìn)行組合測(cè)試:
package main func main() { file1 := &File{name: "File1"} file2 := &File{name: "File2"} file3 := &File{name: "File3"} folder1 := &Folder{ name: "Folder1", } folder1.add(file1) folder2 := &Folder{ name: "Folder2", } folder2.add(file2) folder2.add(file3) folder2.add(folder1) folder2.search("rose") }
輸出結(jié)果:
Serching recursively for keyword rose in folder Folder2
Searching for keyword rose in file File2
Searching for keyword rose in file File3
Serching recursively for keyword rose in folder Folder1
Searching for keyword rose in file File1
裝飾模式
裝飾模式也是一種結(jié)構(gòu)模式,通過(guò)將對(duì)象放置在稱為裝飾器的特殊包裝對(duì)象中,允許動(dòng)態(tài)地向?qū)ο筇砑有滦袨椤J褂醚b飾器可以無(wú)數(shù)次包裝對(duì)象,因?yàn)槟繕?biāo)對(duì)象和裝飾器遵循相同的接口。結(jié)果對(duì)象將獲得所有包裝器的堆疊行為。下面通過(guò)實(shí)例進(jìn)行說(shuō)明:
pizza.go
定義披薩類型,包括getPrice方法:
package main type IPizza interface { getPrice() int }
veggieMania.go
定義素食披薩,并實(shí)現(xiàn)getPrice方法:
package main type VeggeMania struct { } func (p *VeggeMania) getPrice() int { return 15 }
tomatoTopping.go
定義番茄匹薩,再次對(duì)getPrice方法進(jìn)行裝飾:
package main type TomatoTopping struct { pizza IPizza } func (c *TomatoTopping) getPrice() int { pizzaPrice := c.pizza.getPrice() return pizzaPrice + 7 }
cheeseTopping.go
定義奶酪匹薩,同時(shí)再次對(duì)getPrice方法進(jìn)行裝飾:
package main type CheeseTopping struct { pizza IPizza } func (c *CheeseTopping) getPrice() int { pizzaPrice := c.pizza.getPrice() return pizzaPrice + 10 }
main.go
下面定義具體實(shí)現(xiàn),展示裝飾模式的應(yīng)用:
package main import "fmt" func main() { // 定義匹薩 pizza := &VeggeMania{} // 增加奶酪 pizzaWithCheese := &CheeseTopping{ pizza: pizza, } // 增加番茄 pizzaWithCheeseAndTomato := &TomatoTopping{ pizza: pizzaWithCheese, } fmt.Printf("Price of veggeMania with tomato and cheese topping is %d\n", pizzaWithCheeseAndTomato.getPrice()) }
輸出結(jié)果:
Price of veggeMania with tomato and cheese topping is 32
到此這篇關(guān)于Golang實(shí)現(xiàn)組合模式和裝飾模式的文章就介紹到這了,更多相關(guān)go組合模式和裝飾模式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
windows下使用GoLand生成proto文件的方法步驟
本文主要介紹了windows下使用GoLand生成proto文件的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06Go語(yǔ)言執(zhí)行cmd命令庫(kù)的方法實(shí)現(xiàn)
go語(yǔ)言用來(lái)執(zhí)行一個(gè)系統(tǒng)的命令相對(duì)python來(lái)說(shuō)還是有點(diǎn)復(fù)雜的,執(zhí)行命令是一個(gè)非常常見(jiàn)的需求,本文主要介紹了Go語(yǔ)言執(zhí)行cmd命令庫(kù)的方法實(shí)現(xiàn),感興趣的可以了解一下2023-09-09golang判斷結(jié)構(gòu)體為空的問(wèn)題
這篇文章主要介紹了golang判斷結(jié)構(gòu)體為空的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02go實(shí)現(xiàn)for range迭代時(shí)修改值的操作
這篇文章主要介紹了go實(shí)現(xiàn)for range迭代時(shí)修改值的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04Golang實(shí)現(xiàn)超時(shí)退出的三種方式
這篇文章主要介紹了Golang三種方式實(shí)現(xiàn)超時(shí)退出,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03Go語(yǔ)言kube-scheduler深度剖析與開發(fā)之pod調(diào)度
這篇文章主要為大家介紹了Go語(yǔ)言kube-scheduler深度剖析與開發(fā),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04Go語(yǔ)言中int、float、string類型之間相互的轉(zhuǎn)換
golang是強(qiáng)類型語(yǔ)言,在應(yīng)用過(guò)程中類型轉(zhuǎn)換基本都會(huì)用到,下面這篇文章主要給大家介紹了關(guān)于Go語(yǔ)言中int、float、string類型相互之間的轉(zhuǎn)換,需要的朋友可以參考下2022-01-01