網(wǎng)站首頁 編程語言 正文
本文介紹組合模式和裝飾模式,golang實現(xiàn)兩種模式有共同之處,但在具體應用場景有差異。通過對比兩個模式,可以加深理解。
組合模式
組合是一種結構設計模式,它允許將對象組合成樹狀結構,并將其作為單一對象使用。對于需要構建樹形結構的大多數(shù)問題,組合結構成為常用的解決方案,它最大特性是能夠在整個樹結構上遞歸運行方法并對結果進行匯總。
這里通過操作系統(tǒng)的文件系統(tǒng)來理解Composite模式。在文件系統(tǒng)中有兩種類型的對象: 文件和文件夾。有些情況下文件和文件夾應該以相同的方式對待。這就是Composite模式派上用場的地方。
假設您需要在文件系統(tǒng)中對特定的關鍵字進行搜索。此搜索操作同時適用于文件和文件夾。對于一個文件,它只會查看文件的內容;對于一個文件夾,它將遍歷該文件夾的所有文件以找到該關鍵字。下面通過實例進行說明。
component.go
定義節(jié)點類型:
package main type Component interface { search(string) }
file.go
定義文件類型節(jié)點,實現(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é)點,也實現(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) }
組合測試
定義main.go文件進行組合測試:
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") }
輸出結果:
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
裝飾模式
裝飾模式也是一種結構模式,通過將對象放置在稱為裝飾器的特殊包裝對象中,允許動態(tài)地向對象添加新行為。使用裝飾器可以無數(shù)次包裝對象,因為目標對象和裝飾器遵循相同的接口。結果對象將獲得所有包裝器的堆疊行為。下面通過實例進行說明:
pizza.go
定義披薩類型,包括getPrice方法:
package main type IPizza interface { getPrice() int }
veggieMania.go
定義素食披薩,并實現(xiàn)getPrice方法:
package main type VeggeMania struct { } func (p *VeggeMania) getPrice() int { return 15 }
tomatoTopping.go
定義番茄匹薩,再次對getPrice方法進行裝飾:
package main type TomatoTopping struct { pizza IPizza } func (c *TomatoTopping) getPrice() int { pizzaPrice := c.pizza.getPrice() return pizzaPrice + 7 }
cheeseTopping.go
定義奶酪匹薩,同時再次對getPrice方法進行裝飾:
package main type CheeseTopping struct { pizza IPizza } func (c *CheeseTopping) getPrice() int { pizzaPrice := c.pizza.getPrice() return pizzaPrice + 10 }
main.go
下面定義具體實現(xiàn),展示裝飾模式的應用:
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()) }
輸出結果:
Price of veggeMania with tomato and cheese topping is 32
原文鏈接:https://blog.csdn.net/neweastsun/article/details/128044786
相關推薦
- 2022-06-30 python實現(xiàn)水印圖片功能_python
- 2023-01-31 golang優(yōu)先級隊列的實現(xiàn)全過程_Golang
- 2022-10-01 Python?from?import導包ModuleNotFoundError?No?module?
- 2022-05-17 ribbon和nacos獲取服務列表不一致問題
- 2023-01-03 Android序列化實現(xiàn)接口Serializable與Parcelable詳解_Android
- 2022-04-01 k8s集群中移除節(jié)點后重新加入
- 2022-04-11 C#定時任務框架Quartz.NET介紹與用法_C#教程
- 2022-01-23 SpringBoot時區(qū)問題解決,徹底解決時差問題
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支