论坛首页 Java企业应用论坛

可更新的注册式的单实例模式

浏览 2839 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-12-11   最后修改:2008-12-12

       最近,遇到这样一个应用。在系统中需要大量的配置信息,为了不每次都找数据库或者配置文件。需要一个生命周期和App一样的容器(=静态变量),但是在配置信息被修改时还需要去更新这个容器。

 

      首先选用的是单实例模式。单实例模式中又可分为恶汉,懒汉,以及一种基于饿汉型的注册型。

      个人感觉懒汉型单例模式没什么,而饿汉型的更能体现java特点。然注册行的可扩展性较强,个人感觉有点像

       一个实例工厂.下面来一一列举。

 

恶汉:

 

public class EagerSingleton {
    private static final EagerSingleton m_instance = new EagerSingleton();

    private EagerSingleton() {
    }

    public static EagerSingleton getInstance() 
        {


        return m_instance; 
        }
}

 

懒汉:

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;
    }
}

      

注册型:

public class RegSingleton {
    static private HashMap m_registry = new HashMap();
    static {
        RegSingleton x = new RegSingleton();
        m_registry.put(x.getClass().getName(), x);
    }

   
    protected RegSingleton() {
    }

    static public RegSingleton getInstance(String name) {
        if (name == null) {
            name = "name";
        }
        if (m_registry.get(name) == null) {
            try {
                m_registry.put(name, Class.forName(name).newInstance());
            } catch (Exception e) {
                System.out.println("Error happened.");
            }
        }
        return (RegSingleton) (m_registry.get(name));
    }

   
}
public class RegSingletonChild extends RegSingleton {

    private RegSingletonChild() {
    }

    /**
     * 静态工厂方法
     */
    static public RegSingletonChild getInstance() {
        return (RegSingletonChild) RegSingleton.getInstance("name");
    }

}

 

由于在我们这个系统中各种配置信息较多,我个人感觉使用注册型的单实例模式比较合适。(还能应付对配置信息变化的要求)。然后就需要给我们的单实例模式添加更新的行为了。

 

public class ConfigClass {
    static private HashMap m_registry = new HashMap();
    static {
        ConfigClass x = new ConfigClass();
        m_registry.put(x.getClass().getName(), x);
    }

    /**
     * 保护的默认构造子
     */
    protected ConfigClass() {
    }

    /**
     * 静态工厂方法,返还此类惟一的实例
     */
    static public ConfigClass getInstance(String name) {
        if (name == null) {
            name = "singleConfig.ConfigClass";
        }
        if (m_registry.get(name) == null) {
            try {
                m_registry.put(name, Class.forName(name).newInstance());
            } catch (Exception e) {
                System.out.println("Error happened.");
            }
        }
        return (ConfigClass) (m_registry.get(name));
    }
}
public class ConfigImpl extends ConfigClass {

 private List properties = null;

    /**
     * @return the properties
     */
    public List getProperties() {
        return properties;
    }

    private ConfigImpl() {

        initalProperties();
    }

    public static ConfigImpl getInstance() {
        return (ConfigImpl) ConfigClass.getInstance("singleConfig.ok.ConfigImpl");
    }

    /**
     * 
     * @author xiaofeng.bai<BR>
     * <B>Time</B> : 2008-12-11 下午01:59:24
     */
    public void updateProperties() {
        ConfigImpl con = new ConfigImpl();

        properties = con.getProperties();
    }

    /**
     * @author xiaofeng.bai<BR>
     * <B>Time</B> : 2008-12-11 下午01:56:53
     */
    private void initalProperties() {
        // 初始化配置信息
    }
}

 

呵呵终于完成了,但是现在发现一个问题很晕。我在ConfigImpl中的updateProperties()中有创建了一个ConfigImpl的实例,这样能完成我对properties的更新吗?

单实例顾名思义在一个JVM中只有一个实例,这样是否可行呢?

 

 

   发表时间:2009-01-07  
和java与模式 那里很像呀。。。。
只是
public void updateProperties() { 
        Config con = new (ConfigImpl)       ConfigClass.getInstance("singleConfig.ok.ConfigImpl");  //改成这样呢? 随便说说的
 
        properties = con.getProperties(); 
    } 
 
    /**
     * @author xiaofeng.bai<BR>
     * <B>Time</B> : 2008-12-11 下午01:56:53
     */ 
    private void initalProperties() { 
        // 初始化配置信息 
    } 
这个没有,(*^__^*) 嘻嘻……,新手报道
0 请登录后投票
   发表时间:2009-01-07  
public void updateProperties() { 
  
    } 
这个方法不要呀也可以
在测试,类里用

Config con = new (ConfigImpl)       ConfigClass.getInstance("singleConfig.ok.ConfigImpl");

  con .getProperties()
应该就确保一个了吧
0 请登录后投票
   发表时间:2009-04-10  
能否请楼主简要介绍下RegSingleton 类中static private的用意。实在看不明白,这样的修饰有什么特别的用意吗?

在jdk source中,也发现File.java中也出现这样的用法。
0 请登录后投票
   发表时间:2009-04-11  
你确定这个程序能通过? 不会跑出access exception, 注册式的singleton, 子类的constructor必须是public
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics