-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflyweight.go
133 lines (108 loc) · 2.92 KB
/
flyweight.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import "fmt"
/*
The Flyweight pattern is a structural design pattern
that eliminates the need to store all data in each object,
allowing you to load more objects into a limited amount of memory
by sharing the same state shared by multiple objects.
Flyweight refers to object only stores internal states, its construction function should only be called once
imagine game CS,players with same identification share same clothes
*/
var (
TerroristType = "terrorist"
DefenderType = "defender"
TerroristDressType = "terrorist dress"
DefenderDressType = "defender dress"
dressFactoryInstance = &dressFactory{
dressMap: make(map[string]fDress),
}
)
//flyweight interface
type fDress interface {
getColor() string
}
//concrete flyweight object
type defenderDress struct {
color string
}
func (d *defenderDress) getColor() string {
return d.color
}
func newDefenderDress() *defenderDress {
return &defenderDress{color: "green"}
}
//concrete flyweight object
type terroristDress struct {
color string
}
func (t *terroristDress) getColor() string {
return t.color
}
func newTerroristDress() *terroristDress {
return &terroristDress{color: "red"}
}
type dressFactory struct {
dressMap map[string]fDress
}
func (d *dressFactory) getDressByType(dressType string) (fDress, error) {
if dress, ok := d.dressMap[dressType]; ok {
return dress, nil
}
if dressType == TerroristDressType {
d.dressMap[dressType] = newTerroristDress()
return d.dressMap[dressType], nil
}
if dressType == DefenderDressType {
d.dressMap[dressType] = newDefenderDress()
return d.dressMap[dressType], nil
}
return nil, fmt.Errorf("wrong dress type passed")
}
type fPlayer struct {
dress fDress
pType string
latitude int
longitude int
}
func newFPlayer(playerType, dressType string) *fPlayer {
if dress, err := dressFactoryInstance.getDressByType(dressType); err != nil {
return nil
} else {
return &fPlayer{
dress: dress,
pType: playerType,
}
}
}
func (f *fPlayer) fPlayerOperations() {}
type fGame struct {
terrorists []*fPlayer
counterTerrorists []*fPlayer
}
func newGame() *fGame {
return &fGame{
terrorists: make([]*fPlayer, 1),
counterTerrorists: make([]*fPlayer, 1),
}
}
func (c *fGame) addTerrorist(dressType string) {
player := newFPlayer(TerroristType, dressType)
c.terrorists = append(c.terrorists, player)
return
}
func (c *fGame) addDefender(dressType string) {
player := newFPlayer(DefenderType, dressType)
c.counterTerrorists = append(c.counterTerrorists, player)
return
}
func RunFlyweight() {
game := newGame()
game.addTerrorist(TerroristDressType)
game.addTerrorist(TerroristDressType)
game.addDefender(DefenderDressType)
game.addDefender(DefenderType)
fmt.Printf("total number of dresses = %d \n", len(dressFactoryInstance.dressMap))
for t, d := range dressFactoryInstance.dressMap {
fmt.Printf("DressColorType: %s\nDressColor: %s\n", t, d.getColor())
}
}