单例模式

单例模式的一个特点是在整个工程中只返回一个实例。

//饿汉模式

//不管你什么时候用这只“狗”,我现在就给你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;
}
posted @ 2017-02-15 11:05  师然  阅读(115)  评论(0编辑  收藏  举报