Golang实现组合模式和装饰模式
admin
2024-02-15 10:52:05

本文介绍组合模式和装饰模式,golang实现两种模式有共同之处,但在具体应用场景有差异。通过对比两个模式,可以加深理解。

组合模式

组合是一种结构设计模式,它允许将对象组合成树状结构,并将其作为单一对象使用。对于需要构建树形结构的大多数问题,组合结构成为常用的解决方案,它最大特性是能够在整个树结构上递归运行方法并对结果进行汇总。

这里通过操作系统的文件系统来理解Composite模式。在文件系统中有两种类型的对象: 文件和文件夹。有些情况下文件和文件夹应该以相同的方式对待。这就是Composite模式派上用场的地方。

假设您需要在文件系统中对特定的关键字进行搜索。此搜索操作同时适用于文件和文件夹。对于一个文件,它只会查看文件的内容;对于一个文件夹,它将遍历该文件夹的所有文件以找到该关键字。下面通过实例进行说明。

component.go

定义节点类型:

package maintype Component interface {search(string)
}

file.go

定义文件类型节点,实现search方法:

package mainimport "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

定义文件夹类型节点,也实现search方法:

package mainimport "fmt"type Folder struct {components []Componentname       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 mainfunc 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

装饰模式

装饰模式也是一种结构模式,通过将对象放置在称为装饰器的特殊包装对象中,允许动态地向对象添加新行为。使用装饰器可以无数次包装对象,因为目标对象和装饰器遵循相同的接口。结果对象将获得所有包装器的堆叠行为。下面通过实例进行说明:

pizza.go

定义披萨类型,包括getPrice方法:

package maintype IPizza interface {getPrice() int
}

veggieMania.go

定义素食披萨,并实现getPrice方法:

package maintype VeggeMania struct {
}func (p *VeggeMania) getPrice() int {return 15
}

tomatoTopping.go

定义番茄匹萨,再次对getPrice方法进行装饰:

package maintype TomatoTopping struct {pizza IPizza
}func (c *TomatoTopping) getPrice() int {pizzaPrice := c.pizza.getPrice()return pizzaPrice + 7
}

cheeseTopping.go

定义奶酪匹萨,同时再次对getPrice方法进行装饰:

package maintype CheeseTopping struct {pizza IPizza
}func (c *CheeseTopping) getPrice() int {pizzaPrice := c.pizza.getPrice()return pizzaPrice + 10
}

main.go

下面定义具体实现,展示装饰模式的应用:

package mainimport "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

相关内容

热门资讯

山水皆有情!丽江两山一江的千年... 去过丽江、走过虎跳峡的人,大概率都听过哈巴雪山和金沙江的传说。很多人站在峡谷之下,望着奔腾不息的金沙...
活力中国调研行丨“殷”你而来 ... 9月12日,游客在殷墟博物馆“甲骨文展厅”内参观。位于河南省安阳市的殷墟博物馆新馆是首个全景式展现商...
浦江之夜赴美好相逢,第37届上... 9月12日晚,黄浦江畔灯火璀璨,第37届上海旅游节大巡游在外滩中山东一路上演。不少观众早早便来到外滩...
美丽中国丨秋韵紫鹊界 梯田入画... 这是9月11日拍摄的紫鹊界梯田(无人机照片)。 金秋九月,湖南省娄底市新化县紫鹊界梯田稻谷渐次成熟。...
中国之巅奏响《共同的节日》,千... 千吨阻尼器,“迎世赛、游上海旅游节”。9月13日,由上海市文化和旅游局指导推出上海旅游·会客│巅峰之...