- public static Singleton getInstance() {
- if (instance == null) {
- synchronized (Singleton.class) {
- if (instance == null) {
- instance = new Singleton();
- }
- }
- }
- return instance;
- }
我发现网上单例模式中 getInstance 方法基本上都是这么写的,我觉得这种写法虽然正确但是一点都不易读。 请教各位,像我下面这样写正确吗?
public static synchronized Singleton getInstance(){
if(mInstance == null){
mInstance = new Singleton();
}
return mInstance;
}