2020-04-21 17:50:21 +03:00
|
|
|
package singleton
|
|
|
|
|
|
|
|
import "sync"
|
|
|
|
|
2020-04-23 04:47:58 +03:00
|
|
|
////////////////////////////////
|
|
|
|
//way 1
|
|
|
|
//使用 sync的 once.Do(){}确保执行一次
|
|
|
|
////////////////////////////////
|
2020-04-21 17:50:21 +03:00
|
|
|
|
2020-04-23 04:47:58 +03:00
|
|
|
//Worker Singleton 是单例模式类
|
|
|
|
type Worker struct{}
|
|
|
|
|
|
|
|
//better to be pointer
|
|
|
|
var onlyTheWorker *Worker
|
|
|
|
|
|
|
|
// init a control
|
2020-04-21 17:50:21 +03:00
|
|
|
var once sync.Once
|
|
|
|
|
2020-04-23 04:47:58 +03:00
|
|
|
//GetWorkerInstance 总是获取到同一个Worker对象(内存位置相同)
|
|
|
|
func GetWorkerInstance() *Worker {
|
|
|
|
|
|
|
|
//be sure ,to do this,only once!
|
2020-04-21 17:50:21 +03:00
|
|
|
once.Do(func() {
|
2020-04-23 04:47:58 +03:00
|
|
|
onlyTheWorker = &Worker{}
|
2020-04-21 17:50:21 +03:00
|
|
|
})
|
|
|
|
|
2020-04-23 04:47:58 +03:00
|
|
|
return onlyTheWorker
|
|
|
|
}
|
|
|
|
|
|
|
|
//Manager Singleton 是单例模式类
|
|
|
|
type Manager struct{}
|
|
|
|
|
|
|
|
//better to be pointer
|
|
|
|
var instance *Manager
|
|
|
|
|
|
|
|
//better to be pointer
|
|
|
|
var onlyTheManager *Manager
|
|
|
|
|
|
|
|
////////////////////////////////
|
|
|
|
//way2
|
|
|
|
//使用func init(){}函数来初始化保证,只初始化一次,更简单.
|
|
|
|
////////////////////////////////
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
onlyTheManager = &Manager{}
|
|
|
|
}
|
|
|
|
|
|
|
|
//GetManagerInstance 总是获取到同一个Manager对象(内存位置相同)
|
|
|
|
func GetManagerInstance() *Manager {
|
|
|
|
return onlyTheManager
|
2020-04-21 17:50:21 +03:00
|
|
|
}
|