主题:可更新的注册式的单实例模式

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

 

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

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

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

 

恶汉:

 

Java代码 复制代码
  1. public class EagerSingleton {   
  2.     private static final EagerSingleton m_instance = new EagerSingleton();   
  3.   
  4.     private EagerSingleton() {   
  5.     }   
  6.   
  7.     public static EagerSingleton getInstance()    
  8.         {   
  9.   
  10.   
  11.         return m_instance;    
  12.         }   
  13. }  

 

懒汉:

Java代码 复制代码
  1. public class LazySingleton {   
  2.   
  3.     private static LazySingleton m_instance = null;   
  4.   
  5.     private LazySingleton() {   
  6.     }   
  7.   
  8.     synchronized public static LazySingleton getInstance() {   
  9.         if (m_instance == null) {   
  10.             m_instance = new LazySingleton();   
  11.         }   
  12.         return m_instance;   
  13.     }   
  14. }  

      

注册型:

Java代码 复制代码
  1. public class RegSingleton {   
  2.     static private HashMap m_registry = new HashMap();   
  3.     static {   
  4.         RegSingleton x = new RegSingleton();   
  5.         m_registry.put(x.getClass().getName(), x);   
  6.     }   
  7.   
  8.       
  9.     protected RegSingleton() {   
  10.     }   
  11.   
  12.     static public RegSingleton getInstance(String name) {   
  13.         if (name == null) {   
  14.             name = "name";   
  15.         }   
  16.         if (m_registry.get(name) == null) {   
  17.             try {   
  18.                 m_registry.put(name, Class.forName(name).newInstance());   
  19.             } catch (Exception e) {   
  20.                 System.out.println("Error happened.");   
  21.             }   
  22.         }   
  23.         return (RegSingleton) (m_registry.get(name));   
  24.     }   
  25.   
  26.       
  27. }  
Java代码 复制代码
  1. public class RegSingletonChild extends RegSingleton {   
  2.   
  3.     private RegSingletonChild() {   
  4.     }   
  5.   
  6.     /**  
  7.      * 静态工厂方法  
  8.      */  
  9.     static public RegSingletonChild getInstance() {   
  10.         return (RegSingletonChild) RegSingleton.getInstance("name");   
  11.     }   
  12.   
  13. }  

 

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

 

Java代码 复制代码
  1. public class ConfigClass {   
  2.     static private HashMap m_registry = new HashMap();   
  3.     static {   
  4.         ConfigClass x = new ConfigClass();   
  5.         m_registry.put(x.getClass().getName(), x);   
  6.     }   
  7.   
  8.     /**  
  9.      * 保护的默认构造子  
  10.      */  
  11.     protected ConfigClass() {   
  12.     }   
  13.   
  14.     /**  
  15.      * 静态工厂方法,返还此类惟一的实例  
  16.      */  
  17.     static public ConfigClass getInstance(String name) {   
  18.         if (name == null) {   
  19.             name = "singleConfig.ConfigClass";   
  20.         }   
  21.         if (m_registry.get(name) == null) {   
  22.             try {   
  23.                 m_registry.put(name, Class.forName(name).newInstance());   
  24.             } catch (Exception e) {   
  25.                 System.out.println("Error happened.");   
  26.             }   
  27.         }   
  28.         return (ConfigClass) (m_registry.get(name));   
  29.     }   
  30. }  
Java代码 复制代码
  1. public class ConfigImpl extends ConfigClass {   
  2.   
  3.  private List properties = null;   
  4.   
  5.     /**  
  6.      * @return the properties  
  7.      */  
  8.     public List getProperties() {   
  9.         return properties;   
  10.     }   
  11.   
  12.     private ConfigImpl() {   
  13.   
  14.         initalProperties();   
  15.     }   
  16.   
  17.     public static ConfigImpl getInstance() {   
  18.         return (ConfigImpl) ConfigClass.getInstance("singleConfig.ok.ConfigImpl");   
  19.     }   
  20.   
  21.     /**  
  22.      *   
  23.      * @author xiaofeng.bai<BR>  
  24.      * <B>Time</B> : 2008-12-11 下午01:59:24  
  25.      */  
  26.     public void updateProperties() {   
  27.         ConfigImpl con = new ConfigImpl();   
  28.   
  29.         properties = con.getProperties();   
  30.     }   
  31.   
  32.     /**  
  33.      * @author xiaofeng.bai<BR>  
  34.      * <B>Time</B> : 2008-12-11 下午01:56:53  
  35.      */  
  36.     private void initalProperties() {   
  37.         // 初始化配置信息   
  38.     }   
  39. }  

 

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

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

 

posted @ 2009-02-18 13:30  猪鼻驴耳  阅读(136)  评论(0)    收藏  举报