日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

Golang實(shí)現(xiàn)組合模式和裝飾模式實(shí)例詳解_Golang

作者:夢(mèng)想畫(huà)家 ? 更新時(shí)間: 2022-12-24 編程語(yǔ)言

本文介紹組合模式和裝飾模式,golang實(shí)現(xiàn)兩種模式有共同之處,但在具體應(yīng)用場(chǎng)景有差異。通過(guò)對(duì)比兩個(gè)模式,可以加深理解。

組合模式

組合是一種結(jié)構(gòu)設(shè)計(jì)模式,它允許將對(duì)象組合成樹(shù)狀結(jié)構(gòu),并將其作為單一對(duì)象使用。對(duì)于需要構(gòu)建樹(shù)形結(jié)構(gòu)的大多數(shù)問(wèn)題,組合結(jié)構(gòu)成為常用的解決方案,它最大特性是能夠在整個(gè)樹(shù)結(jié)構(gòu)上遞歸運(yùn)行方法并對(duì)結(jié)果進(jìn)行匯總。

這里通過(guò)操作系統(tǒng)的文件系統(tǒng)來(lái)理解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)鍵字。下面通過(guò)實(shí)例進(jìn)行說(shuō)明。

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)模式,通過(guò)將對(duì)象放置在稱為裝飾器的特殊包裝對(duì)象中,允許動(dòng)態(tài)地向?qū)ο筇砑有滦袨椤J褂醚b飾器可以無(wú)數(shù)次包裝對(duì)象,因?yàn)槟繕?biāo)對(duì)象和裝飾器遵循相同的接口。結(jié)果對(duì)象將獲得所有包裝器的堆疊行為。下面通過(guò)實(shí)例進(jìn)行說(shuō)明:

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

原文鏈接:https://blog.csdn.net/neweastsun/article/details/128044786

欄目分類
最近更新