Created
December 15, 2023 05:55
-
-
Save Guaderxx/d8176075eeaf7a4075a0cfff1a2ea4af to your computer and use it in GitHub Desktop.
decorator pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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