SoFunction
Updated on 2025-03-05

Detailed explanation of examples of Golang implementing combination mode and decorative mode

This article introduces the combination mode and the decorative mode. Golang implements the two modes in common, but there are differences in specific application scenarios. By comparing the two modes, we can deepen our understanding.

Combination mode

Composition is a structural design pattern that allows objects to be combined into a tree structure and used as a single object. For most problems that require building a tree structure, combined structures have become a common solution. Its biggest feature is the ability to recursively run methods on the entire tree structure and summarize the results.

Here we understand the Composite mode through the operating system's file system. There are two types of objects in the file system: files and folders. In some cases files and folders should be treated the same way. This is where Composite mode comes in handy.

Suppose you need to search for specific keywords in the file system. This search is applicable to both files and folders. For a file, it will only view the contents of the file; for a folder, it will iterate through all files in that folder to find the keyword. The following is an example.

Define the node type:

package main

type Component interface {
    search(string)
}

Define file type node and implement search method:

package main

import "fmt"

type File struct {
    name string
}

func (f *File) search(keyword string) {
    ("Searching for keyword %s in file %s\n", keyword, )
}

func (f *File) getName() string {
    return 
}

Define folder type nodes and implement search methods:

package main

import "fmt"

type Folder struct {
    components []Component
    name       string
}

func (f *Folder) search(keyword string) {
    ("Serching recursively for keyword %s in folder %s\n", keyword, )
    for _, composite := range  {
        (keyword)
    }
}

func (f *Folder) add(c Component) {
     = append(, c)
}

Combination test

Define files for combination testing:

package main

func main() {
    file1 := &File{name: "File1"}
    file2 := &File{name: "File2"}
    file3 := &File{name: "File3"}

    folder1 := &Folder{
        name: "Folder1",
    }

    (file1)

    folder2 := &Folder{
        name: "Folder2",
    }
    (file2)
    (file3)
    (folder1)

    ("rose")
}

Output result:

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

Decorative mode

Decorative mode is also a structural mode that allows new behavior to be added dynamically to the object by placing it in a special wrapper object called a decorator. Using a decorator can wrap an object countless times, because the target object and the decorator follow the same interface. The resulting object will get stacking behavior for all wrappers. The following is an example:

Define the pizza type, including the getPrice method:

package main

type IPizza interface {
    getPrice() int
}

Define vegetarian pizza and implement the getPrice method:

package main

type VeggeMania struct {
}

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

Define the tomato pizza and decorate the getPrice method again:

package main

type TomatoTopping struct {
    pizza IPizza
}

func (c *TomatoTopping) getPrice() int {
    pizzaPrice := ()
    return pizzaPrice + 7
}

Define cheese pizza and decorate the getPrice method again:

package main

type CheeseTopping struct {
    pizza IPizza
}

func (c *CheeseTopping) getPrice() int {
    pizzaPrice := ()
    return pizzaPrice + 10
}

The following defines the specific implementation and displays the application of the decorative mode:

package main

import "fmt"

func main() {

    // Define pizza    pizza := &VeggeMania{}

    // Add cheese    pizzaWithCheese := &CheeseTopping{
        pizza: pizza,
    }

    // Add tomatoes    pizzaWithCheeseAndTomato := &TomatoTopping{
        pizza: pizzaWithCheese,
    }

    ("Price of veggeMania with tomato and cheese topping is %d\n", ())
}

Output result:

Price of veggeMania with tomato and cheese topping is 32

This is the article about Golang implementing combination modes and decoration modes. For more related go combination modes and decoration modes, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!