-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfactory.go
More file actions
127 lines (98 loc) · 2.54 KB
/
factory.go
File metadata and controls
127 lines (98 loc) · 2.54 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package ch8
import "fmt"
type Operation[T float64 | int64 | float32 | int] interface {
SetNumber1(number T)
SetNumber2(number T)
GetResult() T
}
type OperationBase[T float64 | int64 | float32 | int] struct {
Number1 T
Number2 T
}
func (o *OperationBase[T]) SetNumber1(number T) {
o.Number1 = number
}
func (o *OperationBase[T]) SetNumber2(number T) {
o.Number2 = number
}
// add
type OperationAdd[T float64 | int64 | float32 | int] struct {
OperationBase[T]
}
func (o *OperationAdd[T]) GetResult() T {
return o.Number1 + o.Number2
}
// sub
type OperationSub[T float64 | int64 | float32 | int] struct {
OperationBase[T]
}
func (o *OperationSub[T]) GetResult() T {
return o.Number1 - o.Number2
}
// mul
type OperationMul[T float64 | int64 | float32 | int] struct {
OperationBase[T]
}
func (o *OperationMul[T]) GetResult() T {
return o.Number1 * o.Number2
}
// div
type OperationDiv[T float64 | int64 | float32 | int] struct {
OperationBase[T]
}
func (o *OperationDiv[T]) GetResult() T {
if o.Number2 == 0 {
return 0
}
return o.Number1 / o.Number2
}
// pow
type OperationPow[T float64 | int64 | float32 | int] struct {
OperationBase[T]
}
func (o *OperationPow[T]) GetResult() T {
return o.Number1 * o.Number1
}
// 工厂模式
type IFactory[T float64 | int64 | float32 | int] interface {
CreateOperation() Operation[T]
}
type AddFactory[T float64 | int64 | float32 | int] struct {
}
func (a *AddFactory[T]) CreateOperation() Operation[T] {
return &OperationAdd[T]{}
}
type SubFactory[T float64 | int64 | float32 | int] struct {
}
func (a *SubFactory[T]) CreateOperation() Operation[T] {
return &OperationSub[T]{}
}
type MulFactory[T float64 | int64 | float32 | int] struct {
}
func (a *MulFactory[T]) CreateOperation() Operation[T] {
return &OperationMul[T]{}
}
type DivFactory[T float64 | int64 | float32 | int] struct {
}
func (a *DivFactory[T]) CreateOperation() Operation[T] {
return &OperationDiv[T]{}
}
type PowFactory[T float64 | int64 | float32 | int] struct {
}
func (a *PowFactory[T]) CreateOperation() Operation[T] {
return &OperationPow[T]{}
}
// client
func FactoryRun[T float64 | int64 | float32 | int](num1, num2 T) T {
factory := &AddFactory[T]{}
operation := factory.CreateOperation()
operation.SetNumber1(num1)
operation.SetNumber2(num2)
fmt.Printf("%v + %v = %v\n", num1, num2, operation.GetResult())
subfactory := &SubFactory[T]{}
operation = subfactory.CreateOperation()
operation.SetNumber1(num1)
operation.SetNumber2(num2)
fmt.Printf("%v - %v = %v\n", num1, num2, operation.GetResult())
return operation.GetResult()
}