Skip to content

Commit f7c063b

Browse files
committed
readme
1 parent 84a4502 commit f7c063b

File tree

9 files changed

+788
-9
lines changed

9 files changed

+788
-9
lines changed

Facade/README.md

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,66 @@
2121
外觀模式是一結構型模式,
2222
中介者模式是一行為性模式
2323

24-
![UML](https://github.com/kimi0230/DesignPatternGolang/blob/master/UML/Facade.png?raw=true)
24+
![UML](https://github.com/kimi0230/DesignPatternGolang/blob/master/UML/Facade.png?raw=true)
25+
26+
### Example
27+
https://github.com/kimi0230/DesignPatternGolang/tree/master/Facade
28+
29+
```go
30+
package facade
31+
32+
import "fmt"
33+
34+
// 外觀類別, 她需要了解所有的子系統的方法或屬性, 並進行組合, 以備外界調用
35+
type Facade struct {
36+
One SubSystemOne
37+
Two SubSystemTwo
38+
Three SubSystemThree
39+
Four SubSystemFour
40+
}
41+
42+
func NewFacade() *Facade {
43+
return &Facade{SubSystemOne{}, SubSystemTwo{}, SubSystemThree{}, SubSystemFour{}}
44+
}
45+
46+
func (f *Facade) MethodA() {
47+
fmt.Println("方法組 A")
48+
f.One.MethodOne()
49+
f.Three.MethodThree()
50+
}
51+
52+
func (f *Facade) MethodB() {
53+
fmt.Println("方法組 B")
54+
f.Two.MethodTwo()
55+
f.Four.MethodFour()
56+
}
57+
58+
type SubSystemOne struct {
59+
}
60+
61+
func (s *SubSystemOne) MethodOne() {
62+
fmt.Println("子系統方法一")
63+
}
64+
65+
type SubSystemTwo struct {
66+
}
67+
68+
func (s *SubSystemTwo) MethodTwo() {
69+
fmt.Println("子系統方法二")
70+
}
71+
72+
type SubSystemThree struct {
73+
}
74+
75+
func (s *SubSystemThree) MethodThree() {
76+
fmt.Println("子系統方法三")
77+
}
78+
79+
type SubSystemFour struct {
80+
}
81+
82+
func (s *SubSystemFour) MethodFour() {
83+
fmt.Println("子系統方法四")
84+
}
85+
86+
```

FactoryMethod/README.md

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,129 @@
1010

1111
> Ex: 計算機
1212
13-
![UML](https://github.com/kimi0230/DesignPatternGolang/blob/master/UML/Factory.png?raw=true)
13+
![UML](https://github.com/kimi0230/DesignPatternGolang/blob/master/UML/Factory.png?raw=true)
14+
15+
### Example
16+
https://github.com/kimi0230/DesignPatternGolang/tree/master/FactoryMethod
17+
18+
```go
19+
/*
20+
Factory Method 工廠方法模式: 由次類別決定要建立的具象類別為何者
21+
定義一個用於創建對象的接口,讓子類決定實例化哪一個類
22+
是一種管理物件創建的模式,隨著輸入的參數不同,簡單工廠會回傳不同的物件
23+
使用者取得物件的時候只要傳入正確的參數,不需要去理解這個物件
24+
是一個使一個類的實例化延遲到其子類
25+
此範例為 簡單工廠
26+
*/
27+
package factorymethod
28+
29+
type OperationFunc interface {
30+
SetNumA(float32)
31+
SetNumB(float32)
32+
GetNumA() float32
33+
GetNumB() float32
34+
GetResult() (float32, bool)
35+
}
36+
37+
type Operation struct {
38+
numberA float32
39+
numberB float32
40+
}
41+
42+
func (o *Operation) SetNumA(num float32) {
43+
if o == nil {
44+
return
45+
}
46+
o.numberA = num
47+
}
48+
49+
func (o *Operation) SetNumB(num float32) {
50+
if o == nil {
51+
return
52+
}
53+
o.numberB = num
54+
}
55+
func (o *Operation) GetNumA() float32 {
56+
if o == nil {
57+
return 0
58+
}
59+
return o.numberA
60+
}
61+
func (o *Operation) GetNumB() float32 {
62+
if o == nil {
63+
return 0
64+
}
65+
return o.numberB
66+
}
67+
68+
// 加法
69+
type OperationAdd struct {
70+
Operation
71+
}
72+
73+
func (o *OperationAdd) GetResult() (float32, bool) {
74+
if o == nil {
75+
return 0, false
76+
}
77+
return o.numberA + o.numberB, true
78+
}
79+
80+
//
81+
type OperationSub struct {
82+
Operation
83+
}
84+
85+
func (o *OperationSub) GetResult() (float32, bool) {
86+
if o == nil {
87+
return 0, false
88+
}
89+
return o.numberA - o.numberB, true
90+
}
91+
92+
//
93+
type OperationMul struct {
94+
Operation
95+
}
96+
97+
func (o *OperationMul) GetResult() (float32, bool) {
98+
if o == nil {
99+
return 0, false
100+
}
101+
return o.numberA * o.numberB, true
102+
}
103+
104+
//
105+
type OperationDiv struct {
106+
Operation
107+
}
108+
109+
func (o *OperationDiv) GetResult() (float32, bool) {
110+
if o == nil {
111+
return 0, false
112+
}
113+
if o.numberB == 0 {
114+
return 0, false
115+
}
116+
return o.numberA / o.numberB, true
117+
}
118+
119+
// 建立工廠
120+
type OperationFactory struct {
121+
oper string
122+
}
123+
124+
func (of *OperationFactory) createoperation(op string) OperationFunc {
125+
switch op {
126+
case "+":
127+
return &OperationAdd{}
128+
case "-":
129+
return &OperationSub{}
130+
case "*":
131+
return &OperationMul{}
132+
case "/":
133+
return &OperationDiv{}
134+
}
135+
return nil
136+
}
137+
138+
```

FactoryMethod/SimplevsFactory/README.md

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,84 @@
1010
工廠方法把簡單工廠的內部邏輯判斷移到了用戶端程式碼來進行
1111
如果要加功能, 本來是改工廠類型, 現在是修改用戶端
1212
是簡單工廠模式的進一步抽象.
13-
缺點是每加一個產品就需要加一個產品工廠的類別, 增加額外的開發量
13+
缺點是每加一個產品就需要加一個產品工廠的類別, 增加額外的開發量
14+
15+
### Example
16+
https://github.com/kimi0230/DesignPatternGolang/tree/master/FactoryMethod/SimplevsFactory
17+
18+
```go
19+
/*
20+
簡單工廠 vs. 工廠方法
21+
簡單工廠
22+
最大優點在於工廠類別中包含了必要的邏輯判斷, 根據用戶端的選擇條件動態實體化相關的類別
23+
對用戶端來說, 去除了與具體產品的依賴
24+
但是違背了 開放封閉原則
25+
工廠模式
26+
用戶端需決定實體化哪一個工廠來實現運算類別, 選擇判斷的問題還是存在
27+
工廠方法把簡單工廠的內部邏輯判斷移到了用戶端程式碼來進行
28+
如果要加功能, 本來是改工廠類型, 現在是修改用戶端
29+
是簡單工廠模式的進一步抽象.
30+
缺點是每加一個產品就需要加一個產品工廠的類別, 增加額外的開發量
31+
*/
32+
package simplevsfactory
33+
34+
import "fmt"
35+
36+
type NigtingaleInterface interface {
37+
Sweep()
38+
Wash()
39+
BuyRice()
40+
}
41+
type Nigtingale struct {
42+
}
43+
44+
func (n *Nigtingale) Sweep() {
45+
fmt.Println("掃地")
46+
}
47+
func (n *Nigtingale) Wash() {
48+
fmt.Println("洗衣")
49+
}
50+
func (n *Nigtingale) BuyRice() {
51+
fmt.Println("買米")
52+
}
53+
54+
type Undergraduate struct {
55+
Nigtingale
56+
}
57+
58+
type Volunteer struct {
59+
Nigtingale
60+
}
61+
62+
type SimpleFactory struct {
63+
}
64+
65+
func (s *SimpleFactory) CreateNightingale(factoryType string) NigtingaleInterface {
66+
var result NigtingaleInterface
67+
switch factoryType {
68+
case "學男丁格爾的大學生":
69+
result = new(Undergraduate)
70+
return result
71+
case "社區義工":
72+
result = new(Volunteer)
73+
return result
74+
}
75+
return nil
76+
}
77+
78+
type IFactory interface {
79+
CreateNightingale()
80+
}
81+
82+
type UndergraduateFactory struct{}
83+
84+
func (u *UndergraduateFactory) CreateNightingale() NigtingaleInterface {
85+
return new(Undergraduate)
86+
}
87+
88+
type VolunteerFactory struct{}
89+
90+
func (u *VolunteerFactory) CreateNightingale() NigtingaleInterface {
91+
return new(Volunteer)
92+
}
93+
```

Flyweight/README.md

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,62 @@
77
像是圍棋棋子物件可以減少到只有兩個實體.
88
開發一個可供多人註冊的部落格網站
99

10-
![UML](https://github.com/kimi0230/DesignPatternGolang/blob/master/UML/Flyweight.png?raw=true)
10+
![UML](https://github.com/kimi0230/DesignPatternGolang/blob/master/UML/Flyweight.png?raw=true)
11+
12+
### Example
13+
https://github.com/kimi0230/DesignPatternGolang/tree/master/Flyweight
14+
15+
```go
16+
package flyweight
17+
18+
import "fmt"
19+
20+
type Flyweight interface {
21+
Operation(int)
22+
}
23+
24+
type ConcreteFlyweight struct {
25+
name string
26+
}
27+
28+
func (c *ConcreteFlyweight) Operation(extrinsicState int) {
29+
if c == nil {
30+
return
31+
}
32+
fmt.Println("具體 Flyweight", extrinsicState)
33+
}
34+
35+
type UnsharedConcreteFlyweight struct {
36+
name string
37+
}
38+
39+
func (c *UnsharedConcreteFlyweight) Operation(extrinsicState int) {
40+
if c == nil {
41+
return
42+
}
43+
fmt.Println("不共用的具體 Flyweight", extrinsicState)
44+
}
45+
46+
type FlyweightFactory struct {
47+
flyweights map[string]Flyweight
48+
}
49+
50+
func NewFlyweightFactory() *FlyweightFactory {
51+
fwfactory := FlyweightFactory{make(map[string]Flyweight)}
52+
fwfactory.flyweights["X"] = &ConcreteFlyweight{"X"}
53+
fwfactory.flyweights["Y"] = &ConcreteFlyweight{"Y"}
54+
fwfactory.flyweights["Z"] = &ConcreteFlyweight{"Z"}
55+
return &fwfactory
56+
}
57+
58+
func (f *FlyweightFactory) Flyweight(name string) Flyweight {
59+
if f == nil {
60+
return nil
61+
}
62+
if _, ok := f.flyweights[name]; !ok {
63+
f.flyweights[name] = &ConcreteFlyweight{name}
64+
}
65+
return f.flyweights[name]
66+
}
67+
68+
```

0 commit comments

Comments
 (0)