Mastering the Decorator Pattern in Go: Add Features Without Changing Code

This article explains the Decorator design pattern, shows how to implement it in Go with interfaces and structs, provides concrete code for encryption and formatting decorators, demonstrates composition, and includes a UML diagram to illustrate the pattern’s structure.

Ops Development & AI Practice
Ops Development & AI Practice
Ops Development & AI Practice
Mastering the Decorator Pattern in Go: Add Features Without Changing Code

Decorator Pattern Overview

The Decorator pattern is a structural design pattern that enables adding new behavior to objects without modifying their existing structure. It works by wrapping the original object with a decorator that performs additional actions before or after delegating to the original object's methods, providing a flexible alternative to inheritance.

Go Implementation

Define a TextProcessor interface that declares a Process method.

type TextProcessor interface {
    Process(text string) string
}

Implement a basic processor that returns the input unchanged.

type BaseProcessor struct{}

func (p *BaseProcessor) Process(text string) string {
    return text
}

Create an encryption decorator that wraps another TextProcessor and pretends to encrypt the result.

type EncryptDecorator struct {
    processor TextProcessor
}

func (d *EncryptDecorator) Process(text string) string {
    // Assume encryption happens here
    encryptedText := "encrypted(" + d.processor.Process(text) + ")"
    return encryptedText
}

func NewEncryptDecorator(processor TextProcessor) TextProcessor {
    return &EncryptDecorator{processor: processor}
}

Create a formatting decorator that adds a formatting step.

type FormatDecorator struct {
    processor TextProcessor
}

func (d *FormatDecorator) Process(text string) string {
    // Assume formatting happens here
    formattedText := "formatted(" + d.processor.Process(text) + ")"
    return formattedText
}

func NewFormatDecorator(processor TextProcessor) TextProcessor {
    return &FormatDecorator{processor: processor}
}

Compose the decorators to build a processing pipeline.

func main() {
    processor := &BaseProcessor{}
    encryptedProcessor := NewEncryptDecorator(processor)
    formattedProcessor := NewFormatDecorator(encryptedProcessor)

    text := "Hello, World!"
    result := formattedProcessor.Process(text)
    fmt.Println(result) // Output: formatted(encrypted(Hello, World!))
}

UML Class Diagram

Decorator pattern UML diagram
Decorator pattern UML diagram

The diagram shows the TextProcessor interface, the BaseProcessor concrete component, the abstract decorator role (represented by the shared processor field), and the concrete EncryptDecorator and FormatDecorator classes.

Key Points

Decorators add behavior without altering existing code.

In Go, interfaces combined with struct composition replace inheritance.

Multiple decorators can be chained, each delegating to the wrapped processor.

Adding a new operation only requires implementing a new decorator type.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Design PatternsSoftware ArchitectureprogrammingGoDecorator Pattern
Ops Development & AI Practice
Written by

Ops Development & AI Practice

DevSecOps engineer sharing experiences and insights on AI, Web3, and Claude code development. Aims to help solve technical challenges, improve development efficiency, and grow through community interaction. Feel free to comment and discuss.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.