主题:可更新的注册式的单实例模式
最近,遇到这样一个应用。在系统中需要大量的配置信息,为了不每次都找数据库或者配置文件。需要一个生命周期和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中只有一个实例,这样是否可行呢?

浙公网安备 33010602011771号