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

Java进阶(极客)——单例模式(一)基本原理

2015-07-18 10:25 756 查看
单利模式:保证一个类中只能有一个对象

<span style="font-size:18px;">public class Singleton {

private static Singleton uniqeInstance = null;

/**把构造方法写成私有的,防止在外面new对象,保证对象的唯一性;
*
*/
private Singleton() {

};

/**
*在静态方法中实例化静态对象,外面调用此方法就能保证对象的唯一性
*
*
*/
public static Singleton getInstance() {
if (uniqeInstance == null) {
uniqeInstance = new Singleton();
}
return uniqeInstance;

}

}
</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: