Skip to content

Commit 9283fa2

Browse files
authored
Merge pull request #20 from bvwells/template_method
Implement template method #16
2 parents 9475e2d + 9f2c23d commit 9283fa2

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ Name | Description | Status
7070
[`Observer`](./behavioral/observer.go) | Define a one-to-many dependency between objects where a state change in one object results in all its dependents being notified and updated automatically. | Implemented
7171
[`State`](./behavioral/state.go) | Allow an object to alter its behavior when its internal state changes. The object will appear to change its class. |
7272
[`Strategy`](./behavioral/strategy.go) | Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. | Implemented
73-
[`Template Method`](./behavioral/template_method.go) | Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure. |
73+
[`Template Method`](./behavioral/template_method.go) | Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure. | Implemented
7474
[`Visitor`](./behavioral/visitor.go) | Represent an operation to be performed on the elements of an object structure. Visitor lets a new operation be defined without changing the classes of the elements on which it operates. | Implemented
7575

7676
## Go Versions Supported

behavioral/template_method.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,76 @@
11
package behavioral
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
// WorkerInterface defines an interface for a worker.
8+
type WorkerInterface interface {
9+
GetUp()
10+
EatBreakfast()
11+
GoToWork()
12+
Work()
13+
ReturnHome()
14+
Relax()
15+
Sleep()
16+
}
17+
18+
// Worker defines the worker.
19+
type Worker struct {
20+
WorkerInterface
21+
}
22+
23+
// NewWorker returns a new Worker.
24+
func NewWorker(w WorkerInterface) *Worker {
25+
return &Worker{w}
26+
}
27+
28+
// DailyRoutine is the template method for printing the workers daily routine.
29+
func (w *Worker) DailyRoutine() {
30+
w.GetUp()
31+
w.EatBreakfast()
32+
w.GoToWork()
33+
w.Work()
34+
w.ReturnHome()
35+
w.Relax()
36+
w.Sleep()
37+
}
38+
39+
// PostMan is a worker.
40+
type PostMan struct {
41+
}
42+
43+
// GetUp prints what the postman does to get up.
44+
func (w *PostMan) GetUp() {
45+
fmt.Fprintf(outputWriter, "Getting up\n")
46+
}
47+
48+
// EatBreakfast prints what the postman does to eat breakfast.
49+
func (w *PostMan) EatBreakfast() {
50+
fmt.Fprintf(outputWriter, "Eating pop tarts\n")
51+
}
52+
53+
// GoToWork prints what the postman does to get to work.
54+
func (w *PostMan) GoToWork() {
55+
fmt.Fprintf(outputWriter, "Cycle to work\n")
56+
}
57+
58+
// Work prints what the postman does to work.
59+
func (w *PostMan) Work() {
60+
fmt.Fprintf(outputWriter, "Post letters\n")
61+
}
62+
63+
// ReturnHome prints what the postman does to get home.
64+
func (w *PostMan) ReturnHome() {
65+
fmt.Fprintf(outputWriter, "Cycle home\n")
66+
}
67+
68+
// Relax prints what the postman does to relax.
69+
func (w *PostMan) Relax() {
70+
fmt.Fprintf(outputWriter, "Collect stamps\n")
71+
}
72+
73+
// Sleep prints what the postman does to sleep.
74+
func (w *PostMan) Sleep() {
75+
fmt.Fprintf(outputWriter, "Zzzzzzz\n")
76+
}

behavioral/template_method_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package behavioral
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestTemplateMethod(t *testing.T) {
11+
12+
bufferOutputWriter := outputWriter
13+
outputWriter = new(bytes.Buffer)
14+
defer func() { outputWriter = bufferOutputWriter }()
15+
16+
worker := NewWorker(&PostMan{})
17+
worker.DailyRoutine()
18+
19+
assert.Equal(t, "Getting up\n"+
20+
"Eating pop tarts\n"+
21+
"Cycle to work\n"+
22+
"Post letters\n"+
23+
"Cycle home\n"+
24+
"Collect stamps\n"+
25+
"Zzzzzzz\n", outputWriter.(*bytes.Buffer).String())
26+
}

0 commit comments

Comments
 (0)