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

學無先后,達者為師

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

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

作者:夢想畫家 ? 更新時間: 2022-12-24 編程語言

本文介紹組合模式和裝飾模式,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

欄目分類
最近更新