Created
August 29, 2017 18:04
-
-
Save stevenferrer/e4c1eb973a930c2205039448cda6d3d9 to your computer and use it in GitHub Desktop.
Demonstrating the 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" | |
"strings" | |
) | |
type Decorator func(string) string | |
func Upper() Decorator { | |
return func(s string) string { | |
return strings.ToUpper(s) | |
} | |
} | |
func Prefix(prefix string) Decorator { | |
return func(s string) string { | |
return prefix + s | |
} | |
} | |
func main() { | |
decorators := []Decorator{ | |
Upper(), | |
Prefix("MyPrefix"), | |
} | |
s := "-TheString" | |
for _, dc := range decorators { | |
s = dc(s) | |
} | |
fmt.Println(s) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment