单例模式(饱汉式 饿汉式)

/*
2011-9-18
author:BearFly1990
*/
package designpattern;

public class SingletonPattern {
    private static SingletonPattern sp = null;
    private SingletonPattern() {
        
    }
    public static synchronized SingletonPattern getInstance(){
        if(sp == null){
            sp = new SingletonPattern();
        }
        return sp;
    }
}
class SingletonPattern2 {
    private static SingletonPattern2 sp = new SingletonPattern2();
    private SingletonPattern2() {
        
    }
    public static synchronized SingletonPattern2 getInstance(){
        return sp;
    }
}

你可能感兴趣的:(JAVAEE)