您的位置:首页 > 编程语言 > Java开发

Java设计模式--单例模式

2016-12-22 16:31 323 查看
单例模式的代码:
public class Singleton{
private static Singleton uniqueInstance;
private Singleton(){
}
public static Singleton getInstance(){
if(uniqueSingleton == null){
return new Singleton();
}
return uniqueSingleton;

}

}


分析:

1. 通过构造函数私有化,保证了其他类不能继承并实例化,只能在类的内部访问。

2. 通过定义静态的uniqueInstance,确保实例化的对象,都能被确切的知道。

3. getInstance是一个静态的public方法,可以在类外惊醒访问。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: