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.
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
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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
