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

相关内容

热门资讯

同样卖奶茶,为何古茗赚了15亿... 出品/联商专栏 撰文/老刀 编辑/蔡建桢 今年上半年,蜜雪、古茗、霸王茶姬、茶百道、沪上阿姨全部盈利...
现烤散装走俏、新口味亮相、健康... 中秋佳节临近,成都月饼市场已全面开启“上新季”。记者连日来走访成都多家商超看到,月饼专区占据卖场核心...
散装月饼靠性价比当上“人气王” 华润超市货架上的散装月饼。 深圳报业集团记者 欧阳莹 摄 深圳晚报讯 (深圳报业集团记者 欧阳莹) ...
原创 9... 九月初秋,燥气渐起,容易咽喉干痒、口鼻缺水、干咳烦闷。多喝清润汤水,滋养肺阴,补足身体津液,不寒不腻...
原创 生... 山药是秋季深藏不露的养生食材,也是为数不多 生用补肾、熟用健脾 的平价好物。生山药滋阴补肾、固护肾精...