少妇AV一区二区三区无|久久AV电影一区三|日本一级片黄色毛片|亚洲久久成人av在线久操|黄色视频在线免费看呀一区二区|综合精品视频精品久久久浪朝|亚洲午夜成人资源|欧美黄色一级片黑寡妇|内射无毛少妇特写|无码农村夜晚偷拍啪啪

設(shè)計(jì)模式:可更新的注冊(cè)式的單實(shí)例模式

時(shí)間:2009-04-16 09:40:00   來(lái)源:無(wú)憂考網(wǎng)     [字體: ]
遇到這樣一個(gè)應(yīng)用。在系統(tǒng)中需要大量的配置信息,為了不每次都找數(shù)據(jù)庫(kù)或者配置文件。需要一個(gè)生命周期和App一樣的容器(=靜態(tài)變量),但是在配置信息被修改時(shí)還需要去更新這個(gè)容器。
  首先選用的是單實(shí)例模式。單實(shí)例模式中又可分為惡漢,懶漢,以及一種基于餓漢型的注冊(cè)型。
  個(gè)人感覺(jué)懶漢型單例模式?jīng)]什么,而餓漢型的更能體現(xiàn)java特點(diǎn)。然注冊(cè)行的可擴(kuò)展性較強(qiáng),個(gè)人感覺(jué)有點(diǎn)像
  一個(gè)實(shí)例工廠。下面來(lái)一一列舉。
  惡漢:
  Java代碼
  public class EagerSingleton {
  private static final EagerSingleton m_instance = new EagerSingleton();
  private EagerSingleton() {
  }
  public static EagerSingleton getInstance()
  {
  return m_instance;
  }
  }
  懶漢:
  Java代碼
  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;
  }
  }
  注冊(cè)型:
  Java代碼
  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));
  }
  }
  Java代碼
  public class RegSingletonChild extends RegSingleton {
  private RegSingletonChild() {
  }
  /**
  * 靜態(tài)工廠方法
  */static public RegSingletonChild getInstance() {
  return (RegSingletonChild) RegSingleton.getInstance("name");
  }
  }
  由于在我們這個(gè)系統(tǒng)中各種配置信息較多,我個(gè)人感覺(jué)使用注冊(cè)型的單實(shí)例模式比較合適。(還能應(yīng)付對(duì)配置信息變化的要求)。然后就需要給我們的單實(shí)例模式添加更新的行為了。
  Java代碼
  public class ConfigClass {
  static private HashMap m_registry = new HashMap();
  static {
  ConfigClass x = new ConfigClass();
  m_registry.put(x.getClass().getName(), x);
  }
  /**
  * 保護(hù)的默認(rèn)構(gòu)造子
  */
  protected ConfigClass() {
  }
  /**
  * 靜態(tài)工廠方法,返還此類(lèi)惟一的實(shí)例
  */
  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));
  }
  }
  Java代碼
  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");
  }
  /**
  *
  * Time : 2008-12-11 下午01:59:24
  */
  public void updateProperties() {
  ConfigImpl con = new ConfigImpl();
  properties = con.getProperties();
  }
  /**
  * Time : 2008-12-11 下午01:56:53
  */
  private void initalProperties() {
  // 初始化配置信息
  }
  }
呵呵終于完成了,但是現(xiàn)在發(fā)現(xiàn)一個(gè)問(wèn)題很暈。我在ConfigImpl中的updateProperties()中有創(chuàng)建了一個(gè)ConfigImpl的實(shí)例,這樣能完成我對(duì)properties的更新嗎?
  單實(shí)例顧名思義在一個(gè)JVM中只有一個(gè)實(shí)例,這樣是否可行呢?