单例模式的一个特点是在整个工程中只返回一个实例。
//饿汉模式
//不管你什么时候用这只“狗”,我现在就给你new出来
private static Dog dog = new Dog();
private Dog(){
}
public static Dog getDog()
{
return dog;
}
//懒汉模式
private static Dog dog = null;
private Dog(){
}
public static Dog getDog()
{
if(dog==null)
{
dog= new Dog();
}
return dog;
}
原文:http://www.cnblogs.com/kellybaby/p/6400550.html