我正在按照本书(https://github.com/PacktPublishing/Go-Design-Patterns/blob/master/Chapter02/main.go)中所述的Singleton设计模式进行操作,并将此代码保存在文件“ singleton2.go”中:
package singleton2
type Singleton interface {
AddOne() int
}
type singleton struct {
count int
}
//var instance = &singleton{}
var instance *singleton
func GetInstance() *singleton {
if instance == nil {
return new(singleton)
}
return instance
}
func (s *singleton) AddOne() int {
s.count++
return s.count
}
然后我有此测试文件(singleton2_test.go):
package singleton2
import "testing"
func TestGetInstance(t *testing.T) {
counter1 := GetInstance()
if counter1 == nil {
t.Fatal("expected pointer to Singleton after calling GetInstance(), not nil")
}
expectedCounter := counter1
currentCount := counter1.AddOne()
if currentCount != 1 {
t.Errorf("After calling for the first time to count, the counter must be 1 but it is %d", currentCount)
}
counter2 := GetInstance()
if counter2 != expectedCounter {
t.Errorf("Expected same instance in counter2 but it got a different instance.")
t.Logf("Got %v, want %v", counter2, expectedCounter)
}
currentCount = counter2.AddOne()
if currentCount != 2 {
t.Errorf("After calling AddOne() using second counter, the count must be 2, but it was %d", currentCount)
}
}
问题是测试总是失败:
--- FAIL: TestGetInstance (0.00s)
singleton2_test.go:20: Expected same instance in counter2 but it got a different instance.
singleton2_test.go:21: Got &{0}, want &{1}
singleton2_test.go:26: After calling AddOne() using second counter, the count must be 2, but it was 1
FAIL
exit status 1
FAIL github.com/d3c3/design_patterns/singleton/singleton2 0.029s
有趣的是,如果我将var instance *singleton
行更改为var instance = &singleton{}
行,则测试通过!这是为什么? IMO,它也应与“ var instance * singleton”一起使用
有人能解释这种行为差异吗?
答案 0 :(得分:5)
遵循代码的逻辑,很明显问题出在哪里。
声明但不初始化单例(var instance *singleton
)时,instance
为nil
。调用GetInstance
会将instance == nil
评估为true,并在每次调用时返回一个新的*singleton
。这就是counter2
永远不会等于expectedCounter
的原因-对GetInstance
的每次调用都返回一个新的计数器实例。
初始化单例实例(var instance = &singleton{}
)时,对GetInstance
的调用将返回该单例实例,因为它不是nil
。
我想您想将GetInstance
修改为以下内容:
func GetInstance() *singleton {
if instance == nil {
instance = new(singleton)
}
return instance
}
编辑
尽管您没有提到需要该单例是线程安全的,但用户colminator正确地指出,以这种方式实例化一个单例会导致多个go例程实例化其自己的单例实例,挫败了拥有一个人的目的。鉴于Golang中的并发性很高,了解更多信息可能会有所帮助! Check out the link he posted here。