Golang實(shí)現(xiàn)組合模式和裝飾模式實(shí)例詳解
本文介紹組合模式和裝飾模式,golang實(shí)現(xiàn)兩種模式有共同之處,但在具體應(yīng)用場(chǎng)景有差異。通過對(duì)比兩個(gè)模式,可以加深理解。
組合模式
組合是一種結(jié)構(gòu)設(shè)計(jì)模式,它允許將對(duì)象組合成樹狀結(jié)構(gòu),并將其作為單一對(duì)象使用。對(duì)于需要構(gòu)建樹形結(jié)構(gòu)的大多數(shù)問題,組合結(jié)構(gòu)成為常用的解決方案,它最大特性是能夠在整個(gè)樹結(jié)構(gòu)上遞歸運(yùn)行方法并對(duì)結(jié)果進(jìn)行匯總。
這里通過操作系統(tǒng)的文件系統(tǒng)來理解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)鍵字。下面通過實(shí)例進(jìn)行說明。
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)模式,通過將對(duì)象放置在稱為裝飾器的特殊包裝對(duì)象中,允許動(dòng)態(tài)地向?qū)ο筇砑有滦袨?。使用裝飾器可以無數(shù)次包裝對(duì)象,因?yàn)槟繕?biāo)對(duì)象和裝飾器遵循相同的接口。結(jié)果對(duì)象將獲得所有包裝器的堆疊行為。下面通過實(shí)例進(jìn)行說明:
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)文章
Golang中g(shù)oto跳轉(zhuǎn)語句的實(shí)現(xiàn)
本文主要介紹了Golang中g(shù)oto跳轉(zhuǎn)語句的實(shí)現(xiàn),包括標(biāo)簽的定義、跳轉(zhuǎn)語句的使用、作用域限制、避免濫用的原因以及歷史遺留代碼中的使用情況,感興趣的可以了解一下2025-03-03
go自動(dòng)下載所有的依賴包go module使用詳解
這篇文章主要介紹了go自動(dòng)下載所有的依賴包go module使用詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
golang并發(fā)之使用sync.Pool優(yōu)化性能
在Go提供如何實(shí)現(xiàn)對(duì)象的緩存池功能,常用一種實(shí)現(xiàn)方式是sync.Pool,?其旨在緩存已分配但未使用的項(xiàng)目以供以后重用,從而減輕垃圾收集器(GC)的壓力,下面我們就來看看具體操作吧2023-10-10

