-
Notifications
You must be signed in to change notification settings - Fork 245
/
Copy pathtemplate_method.go
53 lines (43 loc) · 1.19 KB
/
template_method.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Package template_method is an example of the Template Method Pattern.
// In fact, this pattern is based on Abstract Class and Polymorphism.
// But there’s nothing like that in Go, so the composition will be applied.
package template_method
// QuotesInterface provides an interface for setting different quotes.
type QuotesInterface interface {
Open() string
Close() string
}
// Quotes implements a Template Method.
type Quotes struct {
QuotesInterface
}
// Quotes is the Template Method.
func (q *Quotes) Quotes(str string) string {
return q.Open() + str + q.Close()
}
// NewQuotes is the Quotes constructor.
func NewQuotes(qt QuotesInterface) *Quotes {
return &Quotes{qt}
}
// FrenchQuotes implements wrapping the string in French quotes.
type FrenchQuotes struct {
}
// Open sets opening quotes.
func (q *FrenchQuotes) Open() string {
return "«"
}
// Close sets closing quotes.
func (q *FrenchQuotes) Close() string {
return "»"
}
// GermanQuotes implements wrapping the string in German quotes.
type GermanQuotes struct {
}
// Open sets opening quotes.
func (q *GermanQuotes) Open() string {
return "„"
}
// Close sets closing quotes.
func (q *GermanQuotes) Close() string {
return "“"
}