-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingleton-pattern.go
More file actions
69 lines (55 loc) · 1.4 KB
/
singleton-pattern.go
File metadata and controls
69 lines (55 loc) · 1.4 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
package main
import (
"fmt"
"sync"
)
/*
*
- Tuy nhiên đối với đoạn code bên dưới trong môi trường concurrent không có cơ chế synchronize đúng, thì nhiều goroutine có thể detect và cố tạo những instance mới at the same time
----> Và kết quả là nhiều instance được khởi tạo.
- Để giải quyết vấn đề này nó có thể sử dụng cơ chế để đảm bảo chỉ tạo một instance trong môi trường concurrent.
---> Trong package sync của Go có đối tượng sync.Once --> Thực hiện cơ chế thực thi một lần.
*/
type singleton struct {
value string
}
var instance *singleton
func getInstance1() *singleton {
if instance == nil {
instance = &singleton{value: "unique value"}
}
return instance
}
/*
*
- Apply sync.Once
*/
var once sync.Once
func initInstance() {
instance = &singleton{value: "unique value"}
}
// init only one time.
func getInstance() *singleton {
if instance == nil {
once.Do(initInstance)
}
return instance
}
func main() {
firstInit := getInstance()
fmt.Println(firstInit.value)
anotherInstance := getInstance()
if anotherInstance == firstInit {
fmt.Println("Both instances are the same")
}
var wg sync.WaitGroup
for i := 0; i < 1000; i++ {
wg.Add(1)
go func() {
defer wg.Done()
singletonInstance := getInstance()
fmt.Println(singletonInstance.value)
}()
}
wg.Wait()
}