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

java单例模式

2015-08-21 22:39 513 查看
闲来无事,整理了下单实例模式
单例模式确保某一个类只有一个实例,而且向这个系统提供这个实例。
二、单例模式的要点:
1、某个类只能有一个实例
2、它必须自行创建这个实例
3、必须自行向这个系统提供这个实例
三、单例模式的结构及实现:
1、饿汉式单例类:
代码:
public class EagerSingleton
{
private static final EagerSingleton m_instance = new EagerSingleton();
private EagerSingleton()
{
}
public static EagerSingleton getInstance()
{
return m_instance;
}
}
当类被加载的时候,static 类型的m_instance就会被事例化,保证的单利模式的自动创建条件,他是常量,保证了值不变,即保证了对象的唯一;事例的创建调用了私有的构造方法,构造方法私有保证了外界不能直接调用。
2、懒汉式单利类:
代码:
public class LazySingleton
{
private static LazySingleton m_instance = null;
private LazySingleton()
{
}
synchronized public static LazySingleton getInstance()
{
if(m_instance == null)
{
m_instance = new LazySingleton();
}
return m_instance;
}
}
3、注册式单例类

代码
import java.util.HashMap;
public class RegSingleton
{
static private HashMap m_registy = new HashMap();
static
{
RegSingleton sl = new RegSingleton();
m_registy.put(sl.getClass().getName , x);
}
protected RegSingleton()
{
}
public static RegSingleton getInstance(String name)
{
if(name==null)
{
name = "easyworld.pattern.RegSingleton";
}
}
if(m_registy.get(name)==null)
{
try
{
m_registy.put(name,Class.forName(name).newInstance());
}
catch (Exception e)
{
System.out.println("Erorr");
}
}
return (RegSingleton)(m_registry.get(name));

}

单例模式单例模式是一种常见的设计模式,
单例模式分三种:懒汉式单例、饿汉式单例、登记式单例三种。
单例模式有一下特点:
1、单例类只能有一个实例。
2、单例类必须自己自己创建自己的唯一实例。
3、单例类必须给所有其他对象提供这一实例。

一、懒汉式单例在类被加载的时候,唯一实例已经被创建。这个设计模式在Java中容易实现,在别的语言中难以实现。
单例模式-懒汉式单例
public class LazySingleton {
/*** 私有静态对象,加载时候不做初始化 */
private static LazySingleton
m_intance=null;
/*** 私有构造方法,避免外部创建实例 */
private LazySingleton(){}
/*** 静态工厂方法,返回此类的唯一实例.* 当发现实例没有初始化的时候,才初始化. * @return LazySingleton */
synchronized public static LazySingleton getInstance(){
if(m_intance==null){
m_intance=new
LazySingleton();
}
return m_intance;
}
}
//测试一下单例模式
LazySingleton lazySingleton = LazySingleton.getInstance();
LazySingleton lazySingleton1 = LazySingleton.getInstance();
if(lazySingleton==lazySingleton1)){
System.out.println("同一个对象实例");
}else{
System.out.println("不是同一个对象实例");
}
二、饿汉式单例在类加载的时候不创建单例实例。只有在第一次请求实例的时候的时候创建,并且只在第一次创建后,以后不再创建该类的实例。
/***单例模式-饿汉式单例*/
public class EagerSingleton {
/**私有的(private)唯一(static final)实例成员,在类加载的时候就创建好了单例对象*/
private static final EagerSingleton m_instance = new
EagerSingleton();
/*** 私有构造方法,避免外部创建实例
*/
private EagerSingleton(){} //提供了一个空的构造方法
/*** 静态工厂方法,返回此类的唯一实例.* @return EagerSingleton */
public static EagerSingleton getInstance() {
return m_instance;
}
}
//下面来判断一下有没有达到单例效果(系统运行的时候只出来一个空例)
EagerSingleton eagerSingleton = EagerSingleton.getInstance();
EagerSingleton eagerSingleton1 = EagerSingleton.getInstance();
if(eagerSingleton==eagerSingleton1)){
System.out.println("同一个对象实例");
}else{
System.out.println("不是同一个对象实例");
}
三、登记式单例这个单例实际上维护的是一组单例类的实例,将这些实例存放在一个Map(登记薄)中,对于已经登记过的实例,则从工厂直接返回,对于没有登记的,则先登记,而后返回。
/*** 单例模式- 登记式单例*/
public class RegSingleton {
/*** 登记薄,用来存放所有登记的实例 */
private static Map<String, RegSingleton> m_registry =new HashMap();
//在类加载的时候添加一个实例到登记薄
static {
RegSingleton x =new RegSingleton();
m_registry.put(x.getClass().getName(), x);
}
/*** 受保护的默认构造方法*/
protected RegSingleton(){}
/*** 静态工厂方法,返回指定登记对象的唯一实例;
* 对于已登记的直接取出返回,对于还未登记的,先登记,然后取出返回
*@param name
*@return
RegSingleton */
public static RegSingleton getInstance(String name){
if (name == null){
name ="RegSingleton";
}
if (m_registry.get(name) == null) {

try {

m_registry.put(name, (RegSingleton) Class.forName(name).newInstance());
} catch
(InstantiationException e)
{

e.printStackTrace();
} catch
(IllegalAccessException e)
{

e.printStackTrace();
} catch
(ClassNotFoundException e)
{

e.printStackTrace();

}

}
return
m_registry.get(name);
}
/** * 一个示意性的商业方法 *
@return String */
public String about() {
return "Hello,I am
RegSingleton!";
}}

四、单例模式的一个应用该应用是配置文件管理类。为了本例能正确运行,我在C盘下先建立了一个xxxx.properties文件,内容如下:-------------------

user=rootpassword=leizhimin这个配置文件管理类的代码如下:
单例模式应用-单例类应用-配置文件管理*/
public class ConfigManager {
/*** 属性文件全名*/
private static final String PFILE =
"C:\\xxx.properties";
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  单例模式 java