包含标签 设计模式 的文章

Golang 实现单例模式

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 package main import "fmt" type Single struct { } var single *Single func GetSingle() *Single { if single == nil { single = &Single{} } return single } func main() { fmt.……

阅读全文

Python实现单例模式

使用__new__方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class Singleton(object): def __new__(cls, *args, **kw): if not hasattr(cls, '_instance'): orig = super(Singleton, cls) cls._instance = orig.__new__(cls, *args, **kw) return cls._instance class MyClass(Singleton): pass 共享属性 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class Borg(object): _state = {} def __new__(cls, *args, **kw): ob = super(Borg, cls).……

阅读全文