當前位置:星座運勢大全官網 - 星座文化 - 單例模式代碼怎麽寫?

單例模式代碼怎麽寫?

 單例模式(Singleton) ,屬於最常見的設計模式之壹,大部分系統都會用到,目的是為了維護系統中唯壹的壹個實例。\x0d\  可分為eager模式,示例代碼如下:\x0d\  Java代碼\x0d\  1.class EagerSingleton{\x0d\  2. private static final EagerSingleton m_instance = new EagerSingleton();\x0d\  3. private EagerSingleton(){}\x0d\  4. public static EagerSingleton getInstance(){\x0d\  5. return m_instance;\x0d\  6. }\x0d\  7.}\x0d\  class EagerSingleton{\x0d\  private static final EagerSingleton m_instance = new EagerSingleton();\x0d\  private EagerSingleton(){}\x0d\  public static EagerSingleton getInstance(){\x0d\  return m_instance;\x0d\  }\x0d\  }\x0d\  和 lazy模式,示例代碼如下:\x0d\  Java代碼\x0d\  1.class LazySingleton{\x0d\  2. private static LazySingleton m_instance = null;\x0d\  3. private LazySingleton(){}\x0d\  4. public synchronized static getInstance(){\x0d\  5. if(m_instance == null){\x0d\  6. m_instance = new LazySingleton();\x0d\  7. }\x0d\  8. return m_instance;\x0d\  9. }\x0d\  10.}\x0d\  class LazySingleton{\x0d\  private static LazySingleton m_instance = null;\x0d\  private LazySingleton(){}\x0d\  public synchronized static getInstance(){\x0d\  if(m_instance == null){\x0d\  m_instance = new LazySingleton();\x0d\  }\x0d\  return m_instance;\x0d\  }\x0d\  }\x0d\  java源碼中,Runtime.getRuntime()就是單例的壹個例子。\x0d\  單例模式的精神就是整個系統中維護壹個實例,推廣開來,如果在壹個系統中需要維護多個示例,那麽就產生了多例模式(multiton)。\x0d\  多例模式(Multiton) ,通過聚集對象了保留自身的多個示例,根據客戶端的參數返回所需要的實例。\x0d\  示例代碼如下:\x0d\  Java代碼\x0d\  1.class Multiton{\x0d\  2. private final int INSTANCE_SIZE = 10;\x0d\  3. private static Map instances = new HashMap(INSTANCE_SIZE);\x0d\  4. private String name;\x0d\  5. private Multiton(){}\x0d\  6. private Multiton(String name){\x0d\  7. this.name = name;\x0d\  8. }\x0d\  9. public synchronized static getInstance(String name){\x0d\  10. if(instances.containsKey(name)){\x0d\  11. return instances.get(name);\x0d\  12. }\x0d\  13. else{\x0d\  14. ins = new Multiton(name);\x0d\  15. instances.put(name, ins);\x0d\  16. return ins;\x0d\  17. }\x0d\  18. }\x0d\  19.}\x0d\  class Multiton{\x0d\  private final int INSTANCE_SIZE = 10;\x0d\  private static Map instances = new HashMap(INSTANCE_SIZE);\x0d\  private String name;\x0d\  private Multiton(){}\x0d\  private Multiton(String name){\x0d\  this.name = name;\x0d\  }\x0d\  public synchronized static getInstance(String name){\x0d\  if(instances.containsKey(name)){\x0d\  return instances.get(name);\x0d\  }\x0d\  else{\x0d\  ins = new Multiton(name);\x0d\  instances.put(name, ins);\x0d\  return ins;\x0d\  }\x0d\  }\x0d\  }\x0d\  [nextpage]\x0d\  壹個實用的例子就是KeyGenerator, 示例代碼如下:\x0d\  Java代碼\x0d\  1.class KeyGenerator{\x0d\  2. private final int POOL_SIZE = 20;\x0d\  3. private static Map instances = new HashMap(16);\x0d\  4. private KeyInfo keyinfo;\x0d\  5. private KeyGenerator(){}\x0d\  6. private KeyGenerator(String keyName){\x0d\  7. this.keyinfo = new KeyInfo(POOL_SIZE, keyName);\x0d\  8. }\x0d\  9. public synchronized static getInstance(String keyName){\x0d\  10. if(instances.containsKey(keyName)){\x0d\  11. return (KeyGenerator)instances.get(keyName);\x0d\  12. }\x0d\  13. else{\x0d\  14. keyGen = new KeyGenerator(keyName);\x0d\  15. instances.put(name, keyGen);\x0d\  16. return keyGen;\x0d\  17. }\x0d\  18. }\x0d\  19. public synzhronized int getNextKey(){\x0d\  20. return keyinfo.getNextKey();\x0d\  21. }\x0d\  22. }\x0d\  class KeyGenerator{\x0d\  private final int POOL_SIZE = 20;\x0d\  private static Map instances = new HashMap(16);\x0d\  private KeyInfo keyinfo;\x0d\  private KeyGenerator(){}\x0d\  private KeyGenerator(String keyName){\x0d\  this.keyinfo = new KeyInfo(POOL_SIZE, keyName);\x0d\  }\x0d\  public synchronized static getInstance(String keyName){\x0d\  if(instances.containsKey(keyName)){\x0d\  return (KeyGenerator)instances.get(keyName);\x0d\  }\x0d\  else{\x0d\  keyGen = new KeyGenerator(keyName);\x0d\  instances.put(name, keyGen);\x0d\  return keyGen;\x0d\  }\x0d\  }\x0d\  public synzhronized int getNextKey(){\x0d\  return keyinfo.getNextKey();\x0d\  }\x0d\  }