Golang实现组合模式和装饰模式实例详解

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Golang实现组合模式和装饰模式实例详解

梦想画家   2022-11-27 我要评论

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

组合模式

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

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

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

component.go

定义节点类型:

package main

type Component interface {
    search(string)
}

file.go

定义文件类型节点,实现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

定义文件夹类型节点,也实现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

装饰模式

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

pizza.go

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

package main

type IPizza interface {
    getPrice() int
}

veggieMania.go

定义素食披萨,并实现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

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

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们