-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathflyweight.go
More file actions
109 lines (88 loc) · 2.27 KB
/
flyweight.go
File metadata and controls
109 lines (88 loc) · 2.27 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
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
//main package has examples shown
// in Go Data Structures and algorithms book
package main
// importing fmt package
import (
"fmt"
)
//DataTransferObjectFactory struct
type DataTransferObjectFactory struct {
pool map[string]DataTransferObject
}
//DataTransferObjectFactory class method getDataTransferObject
func (factory DataTransferObjectFactory) getDataTransferObject(dtoType string) DataTransferObject {
var dto = factory.pool[dtoType]
if dto == nil {
fmt.Println("new DTO of dtoType: " + dtoType)
switch dtoType {
case "customer":
factory.pool[dtoType] = Customer{id: "1"}
case "employee":
factory.pool[dtoType] = Employee{id: "2"}
case "manager":
factory.pool[dtoType] = Manager{id: "3"}
case "address":
factory.pool[dtoType] = Address{id: "4"}
}
dto = factory.pool[dtoType]
}
return dto
}
// DataTransferObject interface
type DataTransferObject interface {
getId() string
}
//Customer struct
type Customer struct {
id string //sequence generator
name string
ssn string
}
// Customer class method getId
func (customer Customer) getId() string {
//fmt.Println("getting customer Id")
return customer.id
}
//Employee struct
type Employee struct {
id string
name string
}
//Employee class method getId
func (employee Employee) getId() string {
return employee.id
}
//Manager struct
type Manager struct {
id string
name string
dept string
}
//Manager class method getId
func (manager Manager) getId() string {
return manager.id
}
//Address struct
type Address struct {
id string
streetLine1 string
streetLine2 string
state string
city string
}
//Address class method getId
func (address Address) getId() string {
return address.id
}
//main method
func main() {
var factory = DataTransferObjectFactory{make(map[string]DataTransferObject)}
var customer DataTransferObject = factory.getDataTransferObject("customer")
fmt.Println("Customer ", customer.getId())
var employee DataTransferObject = factory.getDataTransferObject("employee")
fmt.Println("Employee ", employee.getId())
var manager DataTransferObject = factory.getDataTransferObject("manager")
fmt.Println("Manager", manager.getId())
var address DataTransferObject = factory.getDataTransferObject("address")
fmt.Println("Address", address.getId())
}