Skip to content

Instantly share code, notes, and snippets.

@Guaderxx
Created December 15, 2023 05:55
Show Gist options
  • Save Guaderxx/d8176075eeaf7a4075a0cfff1a2ea4af to your computer and use it in GitHub Desktop.
Save Guaderxx/d8176075eeaf7a4075a0cfff1a2ea4af to your computer and use it in GitHub Desktop.
decorator pattern
package main
import "fmt"
type Printer interface {
Print() string
}
type SimplePrinter struct {}
func (sp *SimplePrinter) Print() string {
return "Hello"
}
type PrinterFunc func() string
func BoldDecorator(p Printer) Printer {
return PrinterFunc(func() string {
return "<b>" + p.Print() + "</b>"
})
}
func (pf PrinterFunc) Print() string {
return pf()
}
func main() {
simplePrinter := &SimplePrinter{}
boldPrinter := BoldDecorator(simplePrinter)
fmt.Println(simplePrinter.Print()) // output: Hello
fmt.Println(boldPrinter.Print()) // output: <b>Hello</b>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment