forked from bvwells/go-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridge.go
111 lines (93 loc) · 2.63 KB
/
bridge.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package structural
import "fmt"
// TimeImp is the interface for different implementations of telling the time.
type TimeImp interface {
Tell()
}
// BasicTimeImp defines a TimeImp which tells the time in 24 hour format.
type BasicTimeImp struct {
hour int
minute int
}
// NewBasicTimeImp creates a new TimeImp.
func NewBasicTimeImp(hour, minute int) TimeImp {
return &BasicTimeImp{hour, minute}
}
// Tell tells the time in 24 hour format.
func (t *BasicTimeImp) Tell() {
fmt.Fprintf(outputWriter, "The time is %2.2d:%2.2d\n", t.hour, t.minute)
}
// CivilianTimeImp defines a TimeImp which tells the time in 12 hour format with meridem.
type CivilianTimeImp struct {
BasicTimeImp
meridiem string
}
// NewCivilianTimeImp creates a new TimeImp.
func NewCivilianTimeImp(hour, minute int, pm bool) TimeImp {
meridiem := "AM"
if pm {
meridiem = "PM"
}
time := &CivilianTimeImp{meridiem: meridiem}
time.hour = hour
time.minute = minute
return time
}
// Tell tells the time in 12 hour format.
func (t *CivilianTimeImp) Tell() {
fmt.Fprintf(outputWriter, "The time is %2.2d:%2.2d %s\n", t.hour, t.minute, t.meridiem)
}
// ZuluTimeImp defines a TimeImp which tells the time in Zulu zone format.
type ZuluTimeImp struct {
BasicTimeImp
zone string
}
// NewZuluTimeImp creates a new TimeImp.
func NewZuluTimeImp(hour, minute, zoneID int) TimeImp {
zone := ""
if zoneID == 5 {
zone = "Eastern Standard Time"
} else if zoneID == 6 {
zone = "Central Standard Time"
}
time := &ZuluTimeImp{zone: zone}
time.hour = hour
time.minute = minute
return time
}
// Tell tells the time in 24 hour format in Zulu time zone.
func (t *ZuluTimeImp) Tell() {
fmt.Fprintf(outputWriter, "The time is %2.2d:%2.2d %s\n", t.hour, t.minute, t.zone)
}
// Time is the base struct for Time containing the implementation.
type Time struct {
imp TimeImp
}
// NewTime creates a new time which uses a basic time for its implementation.
func NewTime(hour, minute int) *Time {
return &Time{imp: NewBasicTimeImp(hour, minute)}
}
// Tell tells the time with the given implementation.
func (t *Time) Tell() {
t.imp.Tell()
}
// CivilianTime is a time with a civilian time implementation.
type CivilianTime struct {
Time
}
// NewCivilianTime creates a new civilian time.
func NewCivilianTime(hour, minute int, pm bool) *Time {
time := &CivilianTime{}
time.imp = NewCivilianTimeImp(hour, minute, pm)
return &time.Time
}
// ZuluTime is a time with a Zulu time implementation.
type ZuluTime struct {
Time
}
// NewZuluTime creates a new Zulu time.
func NewZuluTime(hour, minute, zoneID int) *Time {
time := &ZuluTime{}
time.imp = NewZuluTimeImp(hour, minute, zoneID)
return &time.Time
}