单例模式
Python
- #参考:http://ghostfromheaven.iteye.com/blog/1562618
- #使用装饰器(decorator),
- #这是一种更pythonic,更elegant的方法,
- #单例类本身根本不知道自己是单例的,因为他本身(自己的代码)并不是单例的
- def singleton(cls, *args, **kw):
- instances = {}
- def _singleton():
- if cls not in instances:
- instances[cls] = cls(*args, **kw)
- return instances[cls]
- return _singleton
- @singleton
- class MyClass4(object):
- a = 1
- def __init__(self, x=0):
- self.x = x
- one = MyClass4()
- two = MyClass4()
- two.a = 3
- print one.a
- #3
- print id(one)
- #29660784
- print id(two)
- #29660784
- print one == two
- #True
- print one is two
- #True
- one.x = 1
- print one.x
- #1
- print two.x
- #1
Java
public class Singleton {
private static class SingletonHolder {
private static final Singleton INSTANCE = new Singleton();
}
private Singleton (){}
public static final Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
}
=======
public enum Singleton {
INSTANCE;
public void whateverMethod() {
}
}