Skip to content

Commit 8c5740a

Browse files
author
qwik zgheib mossad bashit
committed
feat: #24 - go
1 parent f929f70 commit 8c5740a

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type Greeter interface {
8+
Greet(name string) string
9+
}
10+
11+
type SimpleGreeter struct{}
12+
13+
func (g SimpleGreeter) Greet(name string) string {
14+
return fmt.Sprintf("Hello, %s!", name)
15+
}
16+
17+
type ExcitedDecorator struct {
18+
Greeter Greeter
19+
}
20+
21+
func (e ExcitedDecorator) Greet(name string) string {
22+
return e.Greeter.Greet(name) + " How are you doing today?"
23+
}
24+
25+
type AddGreetingCountDecorator struct {
26+
Greeter Greeter
27+
CallCount int
28+
}
29+
30+
func (c *AddGreetingCountDecorator) Greet(name string) string {
31+
c.CallCount++
32+
return c.Greeter.Greet(name)
33+
}
34+
35+
func (c *AddGreetingCountDecorator) GetCallCount() int {
36+
return c.CallCount
37+
}
38+
39+
func main() {
40+
greeter := SimpleGreeter{}
41+
42+
excitedGreeter := ExcitedDecorator{Greeter: greeter}
43+
44+
countingGreeter := &AddGreetingCountDecorator{Greeter: excitedGreeter}
45+
46+
fmt.Println(countingGreeter.Greet("qwik"))
47+
fmt.Println(countingGreeter.Greet("zgheib"))
48+
fmt.Println(countingGreeter.Greet("mossad"))
49+
50+
fmt.Printf("Greet method has been called %d times\n", countingGreeter.GetCallCount())
51+
}

0 commit comments

Comments
 (0)