golang中的工厂模式

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
* 简单工厂模式

package main

import (
"fmt"
)

type Operater interface {
Operate(int, int) int
}

type AddOperate struct {
}

func (this *AddOperate) Operate(rhs int, lhs int) int {
return rhs + lhs
}

type MultipleOperate struct {
}

func (this *MultipleOperate) Operate(rhs int, lhs int) int {
return rhs * lhs
}

type OperateFactory struct {
}

func NewOperateFactory() *OperateFactory {
return &OperateFactory{}
}

func (this *OperateFactory) CreateOperate(operatename string) Operater {
switch operatename {
case "+":
return &AddOperate{}
case "*":
return &MultipleOperate{}
default:
panic("无效运算符号")
return nil
}
}

func main() {
Operator := NewOperateFactory().CreateOperate("+")
fmt.Printf("add result is %d\n", Operator.Operate(1, 2))
}
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
* 工厂方法
package main

import (
"fmt"
)

type Operation struct {
a float64
b float64
}

type OperationI interface {
GetResult() float64
SetA(float64)
SetB(float64)
}

func (op *Operation) SetA(a float64) {
op.a = a
}

func (op *Operation) SetB(b float64) {
op.b = b
}

type AddOperation struct {
Operation
}

func (this *AddOperation) GetResult() float64 {
return this.a + this.b
}

type SubOperation struct {
Operation
}

func (this *SubOperation) GetResult() float64 {
return this.a - this.b
}

type MulOperation struct {
Operation
}

func (this *MulOperation) GetResult() float64 {
return this.a * this.b
}

type DivOperation struct {
Operation
}

func (this *DivOperation) GetResult() float64 {
return this.a / this.b
}

type IFactory interface {
CreateOperation() Operation
}

type AddFactory struct {
}

func (this *AddFactory) CreateOperation() OperationI {
return &(AddOperation{})
}

type SubFactory struct {
}

func (this *SubFactory) CreateOperation() OperationI {
return &(SubOperation{})
}

type MulFactory struct {
}

func (this *MulFactory) CreateOperation() OperationI {
return &(MulOperation{})
}

type DivFactory struct {
}

func (this *DivFactory) CreateOperation() OperationI {
return &(DivOperation{})
}

func main() {
fac := &(AddFactory{})
oper := fac.CreateOperation()
oper.SetA(1)
oper.SetB(2)
fmt.Println(oper.GetResult())
}
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
* 抽象工厂方法
package main

import "fmt"

type GirlFriend struct {
nationality string
eyesColor string
language string
}

type AbstractFactory interface {
CreateMyLove() GirlFriend
}

type IndianGirlFriendFactory struct {
}

type KoreanGirlFriendFactory struct {
}

func (a IndianGirlFriendFactory) CreateMyLove() GirlFriend {
return GirlFriend{"Indian", "Black", "Hindi"}
}

func (a KoreanGirlFriendFactory) CreateMyLove() GirlFriend {
return GirlFriend{"Korean", "Brown", "Korean"}
}

func getGirlFriend(typeGf string) GirlFriend {

var gffact AbstractFactory
switch typeGf {
case "Indian":
gffact = IndianGirlFriendFactory{}
return gffact.CreateMyLove()
case "Korean":
gffact = KoreanGirlFriendFactory{}
return gffact.CreateMyLove()
}
return GirlFriend{}
}

func main() {

a := getGirlFriend("Indian")

fmt.Println(a.eyesColor)
}

-------------The End-------------

本文标题:golang中的工厂模式

文章作者:cloud sjhan

发布时间:2018年08月27日 - 18:08

最后更新:2018年08月27日 - 19:08

原始链接:https://cloudsjhan.github.io/2018/08/27/golang中的工厂模式-md/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

cloud sjhan wechat
subscribe to my blog by scanning my public wechat account
坚持原创技术分享,您的支持将鼓励我继续创作!
0%
;