Skip to content

Commit a58eee0

Browse files
committed
readme
1 parent f7c063b commit a58eee0

File tree

7 files changed

+644
-7
lines changed

7 files changed

+644
-7
lines changed

Prototype/README.md

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,115 @@
44
用原型實例指定建立物件的種類, 並且透過拷貝這些原型建立新的物件
55
從一個物件在建立另一個可訂製的物件,而且不需知道任何建立的細節
66

7-
![UML](https://github.com/kimi0230/DesignPatternGolang/blob/master/UML/Prototype.png?raw=true)
7+
![UML](https://github.com/kimi0230/DesignPatternGolang/blob/master/UML/Prototype.png?raw=true)
8+
9+
### Example
10+
https://github.com/kimi0230/DesignPatternGolang/tree/master/Prototype
11+
12+
```go
13+
/*
14+
Prototype 原型模式:用原型實例指定建立物件的種類, 並且透過拷貝這些原型建立新的物件
15+
從一個物件在建立另一個可訂製的物件,而且不需知道任何建立的細節
16+
*/
17+
18+
package prototype
19+
20+
import "fmt"
21+
22+
type WorkExperience struct {
23+
workDate string
24+
company string
25+
}
26+
27+
func (w *WorkExperience) GetWorkDate() string {
28+
return w.workDate
29+
}
30+
31+
func (w *WorkExperience) SetWorkDate(val string) {
32+
w.workDate = val
33+
}
34+
35+
func (w *WorkExperience) GetCompany() string {
36+
return w.company
37+
}
38+
39+
func (w *WorkExperience) SetCompany(val string) {
40+
w.company = val
41+
}
42+
43+
func (w *WorkExperience) Clone() *WorkExperience {
44+
if w == nil {
45+
return nil
46+
}
47+
// 淺複製, 複製結構, 但不複製值
48+
shallowObj := w
49+
fmt.Printf("&shallowObj: %p, orgObj: %p , shallowObj: %+v \n", &shallowObj, w, shallowObj)
50+
return shallowObj
51+
}
52+
53+
type Resume struct {
54+
name string
55+
sex string
56+
age string
57+
work WorkExperience
58+
}
59+
60+
// NewResume 建構值
61+
func NewResume(n string) *Resume {
62+
work := WorkExperience{}
63+
return &Resume{name: n, work: work}
64+
}
65+
66+
// 提供clone 方法呼叫的私有建構是, 以便複製工作經歷的資料
67+
func (r *Resume) cloneWork(w WorkExperience) {
68+
r.work = *w.Clone()
69+
}
70+
71+
// SetPersonalInfo 設定個人資訊
72+
func (r *Resume) SetPersonalInfo(sex, age string) {
73+
if r == nil {
74+
return
75+
}
76+
r.age = age
77+
r.sex = sex
78+
}
79+
80+
// SetWorkExperience 設定工作經歷
81+
func (r *Resume) SetWorkExperience(workDate, company string) {
82+
if r == nil {
83+
return
84+
}
85+
r.work.company = company
86+
r.work.workDate = workDate
87+
}
88+
89+
// Display 顯示履歷
90+
func (r *Resume) Display() {
91+
if r == nil {
92+
return
93+
}
94+
fmt.Println(r.name, r.sex, r.age)
95+
fmt.Println("工作經歷: ", r.work.workDate, r.work.company)
96+
}
97+
98+
// Clone 深複製履歷
99+
// 淺複製: 被複製物件的所有變數都含有與原來的物件相同的值, 而所有的對其物件的參考都能指向
100+
// 深複製: 把參考物件的變數指向複製過的新物件, 而不是原有他被參考的物件
101+
func (r *Resume) Clone() *Resume {
102+
// 深複製
103+
// var obj = new(Resume)
104+
// obj.work = *(r.work).Clone()
105+
// obj.age = r.age
106+
// obj.name = r.name
107+
// obj.sex = r.sex
108+
// return obj
109+
110+
// 淺複製 : 會導致來源的值也被更改
111+
shallowObj := r
112+
shallowObj.age = r.age
113+
shallowObj.name = r.name
114+
shallowObj.sex = r.sex
115+
shallowObj.work = *(r.work).Clone()
116+
return shallowObj
117+
}
118+
```

Proxy/README.md

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,99 @@
1919
代理是一種原來物件的代表, 其他需要與這個物件打交道的操作都是和這個代表交涉
2020
轉接器則不需要虛構出一個代表者, 只需要為應付特定使用目的, 將原來的類別進行一些組合
2121

22-
![UML](https://github.com/kimi0230/DesignPatternGolang/blob/master/UML/Proxy.png?raw=true)
22+
![UML](https://github.com/kimi0230/DesignPatternGolang/blob/master/UML/Proxy.png?raw=true)
23+
24+
### Example
25+
https://github.com/kimi0230/DesignPatternGolang/tree/master/Proxy
26+
27+
```go
28+
/*
29+
Proxy 代理模式: 將物件包裝起來, 以控制對此物件的存取
30+
為其他物件提供一種代理, 以控制對這個物件的存取
31+
代理和代理的對象接口一致,客户端不知道代理對象
32+
*/
33+
package proxy
34+
35+
import "fmt"
36+
37+
// IGiveGift : Subject
38+
type IGiveGift interface {
39+
GiveDolls()
40+
GiveFlowers()
41+
GiveChocolate()
42+
}
43+
44+
type SchoolGirl struct {
45+
name string
46+
}
47+
48+
func (g *SchoolGirl) Name() string {
49+
if g == nil {
50+
return ""
51+
}
52+
return g.name
53+
}
54+
55+
func (g *SchoolGirl) SetName(name string) {
56+
if g == nil {
57+
return
58+
}
59+
g.name = name
60+
}
61+
62+
// Pursuit : real subject
63+
type Pursuit struct {
64+
girl SchoolGirl
65+
}
66+
67+
func (p *Pursuit) GiveDolls() {
68+
if p == nil {
69+
return
70+
}
71+
fmt.Println(p.girl.name, "送你洋娃娃")
72+
}
73+
74+
func (p *Pursuit) GiveFlowers() {
75+
if p == nil {
76+
return
77+
}
78+
fmt.Println(p.girl.name, "送你鮮花")
79+
}
80+
81+
func (p *Pursuit) GiveChocolate() {
82+
if p == nil {
83+
return
84+
}
85+
fmt.Println(p.girl.name, "送你巧克力")
86+
}
87+
88+
// Proxy Proxy
89+
type Proxy struct {
90+
pursuit Pursuit
91+
}
92+
93+
func NewProxy(girl SchoolGirl) *Proxy {
94+
pursuit := Pursuit{girl}
95+
return &Proxy{pursuit}
96+
}
97+
98+
func (p *Proxy) GiveDolls() {
99+
if p == nil {
100+
return
101+
}
102+
p.pursuit.GiveDolls()
103+
}
104+
func (p *Proxy) GiveFlowers() {
105+
if p == nil {
106+
return
107+
}
108+
p.pursuit.GiveFlowers()
109+
}
110+
111+
func (p *Proxy) GiveChocolate() {
112+
if p == nil {
113+
return
114+
}
115+
p.pursuit.GiveChocolate()
116+
}
117+
```

Singleton/README.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,42 @@
33
44
保證一個類別僅有一個實體, 並提供一個存取它的全域訪問點
55

6-
![UML](https://github.com/kimi0230/DesignPatternGolang/blob/master/UML/Singleton.png?raw=true)
6+
![UML](https://github.com/kimi0230/DesignPatternGolang/blob/master/UML/Singleton.png?raw=true)
7+
8+
### Example
9+
https://github.com/kimi0230/DesignPatternGolang/tree/master/Singleton
10+
11+
```go
12+
package singleton
13+
14+
import "sync"
15+
16+
type Singleton struct {
17+
}
18+
19+
// 方法一: 用 sync.Mutex
20+
var instance *Singleton
21+
var mu sync.Mutex
22+
23+
func GetInstance() *Singleton {
24+
if instance != nil {
25+
mu.Lock()
26+
defer mu.Unlock()
27+
if instance != nil {
28+
instance = &Singleton{}
29+
}
30+
}
31+
return instance
32+
}
33+
34+
// 方法二: 用 sync.Once
35+
var once sync.Once
36+
37+
func GetInstanceOnce() *Singleton {
38+
once.Do(func() {
39+
instance = &Singleton{}
40+
})
41+
return instance
42+
}
43+
44+
```

0 commit comments

Comments
 (0)