Repository navigation
Expand file tree
/
Copy pathFlyweight.go
More file actions
75 lines (71 loc) · 1.34 KB
/
Copy pathFlyweight.go
File metadata and controls
75 lines (71 loc) · 1.34 KB
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
package main
import "time"
const (
TEAM_A = iota
TEAM_B
TEAM_C
)
type Team struct {
ID uint64
Name string
Shield []byte
Players []Player
HistoricalData []HistoricalData
}
type Player struct {
Name string
Surname string
PreviousTeam uint64
Photo []byte
}
type HistoricalData struct {
Year uint8
LeagueResults []Match
}
type Match struct {
Date time.Time
VisitorID uint64
LocalID uint64
LocalScore byte
VisitorScore byte
LocalShoots uint16
VisitorShoots uint16
}
type TeamFlyweightFactory struct {
createdTeams map[int]*Team
}
func (t *TeamFlyweightFactory) GetTeam(teamID int) *Team {
if t.createdTeams == nil {
t.createdTeams = make(map[int]*Team)
}
if t.createdTeams[teamID] != nil {
return t.createdTeams[teamID]
}
team := getTeamFactory(teamID)
t.createdTeams[teamID] = &team
return t.createdTeams[teamID]
}
func getTeamFactory(team int) Team {
switch team {
case TEAM_C:
return Team{ID: 3, Name: "TEAM_C"}
case TEAM_B:
return Team{
ID: 2,
Name: "TEAM_B",
}
default:
return Team{
ID: 1,
Name: "TEAM_A",
}
}
}
func (t *TeamFlyweightFactory) GetNumberOfObjects() int {
return len(t.createdTeams)
}
func NewTeamFactory() TeamFlyweightFactory {
return TeamFlyweightFactory{
createdTeams: make(map[int]*Team),
}
}