单例模式代码怎么写?

 我来答
惠企百科
2022-12-11 · 百度认证:北京惠企网络技术有限公司官方账号
惠企百科
惠企百科网是一家科普类综合网站,关注热门中文知识,集聚互联网精华中文知识,本着自由开放、分享价值的基本原则,向广大网友提供专业的中文知识平台。
向TA提问
展开全部
 单例模式(Singleton) ,属于最常见的设计模式之一,大部分系统都会用到,目的是为了维护系统中唯一的一个实例。\x0d\x0a  可分为eager模式,示例代码如下:\x0d\x0a  Java代码\x0d\x0a  1.class EagerSingleton{\x0d\x0a  2. private static final EagerSingleton m_instance = new EagerSingleton();\x0d\x0a  3. private EagerSingleton(){}\x0d\x0a  4. public static EagerSingleton getInstance(){\x0d\x0a  5. return m_instance;\x0d\x0a  6. }\x0d\x0a  7.}\x0d\x0a  class EagerSingleton{\x0d\x0a  private static final EagerSingleton m_instance = new EagerSingleton();\x0d\x0a  private EagerSingleton(){}\x0d\x0a  public static EagerSingleton getInstance(){\x0d\x0a  return m_instance;\x0d\x0a  }\x0d\x0a  }\x0d\x0a  和 lazy模式,示例代码如下:\x0d\x0a  Java代码\x0d\x0a  1.class LazySingleton{\x0d\x0a  2. private static LazySingleton m_instance = null;\x0d\x0a  3. private LazySingleton(){}\x0d\x0a  4. public synchronized static getInstance(){\x0d\x0a  5. if(m_instance == null){\x0d\x0a  6. m_instance = new LazySingleton();\x0d\x0a  7. }\x0d\x0a  8. return m_instance;\x0d\x0a  9. }\x0d\x0a  10.}\x0d\x0a  class LazySingleton{\x0d\x0a  private static LazySingleton m_instance = null;\x0d\x0a  private LazySingleton(){}\x0d\x0a  public synchronized static getInstance(){\x0d\x0a  if(m_instance == null){\x0d\x0a  m_instance = new LazySingleton();\x0d\x0a  }\x0d\x0a  return m_instance;\x0d\x0a  }\x0d\x0a  }\x0d\x0a  java源码中,Runtime.getRuntime()就是单例的一个例子。\x0d\x0a  单例模式的精神就是整个系统中维护一个实例,推广开来,如果在一个系统中需要维护多个示例,那么就产生了多例模式(multiton)。\x0d\x0a  多例模式(Multiton) ,通过聚集对象了保留自身的多个示例,根据客户端的参数返回所需要的实例。\x0d\x0a  示例代码如下:\x0d\x0a  Java代码\x0d\x0a  1.class Multiton{\x0d\x0a  2. private final int INSTANCE_SIZE = 10;\x0d\x0a  3. private static Map instances = new HashMap(INSTANCE_SIZE);\x0d\x0a  4. private String name;\x0d\x0a  5. private Multiton(){}\x0d\x0a  6. private Multiton(String name){\x0d\x0a  7. this.name = name;\x0d\x0a  8. }\x0d\x0a  9. public synchronized static getInstance(String name){\x0d\x0a  10. if(instances.containsKey(name)){\x0d\x0a  11. return instances.get(name);\x0d\x0a  12. }\x0d\x0a  13. else{\x0d\x0a  14. ins = new Multiton(name);\x0d\x0a  15. instances.put(name, ins);\x0d\x0a  16. return ins;\x0d\x0a  17. }\x0d\x0a  18. }\x0d\x0a  19.}\x0d\x0a  class Multiton{\x0d\x0a  private final int INSTANCE_SIZE = 10;\x0d\x0a  private static Map instances = new HashMap(INSTANCE_SIZE);\x0d\x0a  private String name;\x0d\x0a  private Multiton(){}\x0d\x0a  private Multiton(String name){\x0d\x0a  this.name = name;\x0d\x0a  }\x0d\x0a  public synchronized static getInstance(String name){\x0d\x0a  if(instances.containsKey(name)){\x0d\x0a  return instances.get(name);\x0d\x0a  }\x0d\x0a  else{\x0d\x0a  ins = new Multiton(name);\x0d\x0a  instances.put(name, ins);\x0d\x0a  return ins;\x0d\x0a  }\x0d\x0a  }\x0d\x0a  }\x0d\x0a  [nextpage]\x0d\x0a  一个实用的例子就是KeyGenerator, 示例代码如下:\x0d\x0a  Java代码\x0d\x0a  1.class KeyGenerator{\x0d\x0a  2. private final int POOL_SIZE = 20;\x0d\x0a  3. private static Map instances = new HashMap(16);\x0d\x0a  4. private KeyInfo keyinfo;\x0d\x0a  5. private KeyGenerator(){}\x0d\x0a  6. private KeyGenerator(String keyName){\x0d\x0a  7. this.keyinfo = new KeyInfo(POOL_SIZE, keyName);\x0d\x0a  8. }\x0d\x0a  9. public synchronized static getInstance(String keyName){\x0d\x0a  10. if(instances.containsKey(keyName)){\x0d\x0a  11. return (KeyGenerator)instances.get(keyName);\x0d\x0a  12. }\x0d\x0a  13. else{\x0d\x0a  14. keyGen = new KeyGenerator(keyName);\x0d\x0a  15. instances.put(name, keyGen);\x0d\x0a  16. return keyGen;\x0d\x0a  17. }\x0d\x0a  18. }\x0d\x0a  19. public synzhronized int getNextKey(){\x0d\x0a  20. return keyinfo.getNextKey();\x0d\x0a  21. }\x0d\x0a  22. }\x0d\x0a  class KeyGenerator{\x0d\x0a  private final int POOL_SIZE = 20;\x0d\x0a  private static Map instances = new HashMap(16);\x0d\x0a  private KeyInfo keyinfo;\x0d\x0a  private KeyGenerator(){}\x0d\x0a  private KeyGenerator(String keyName){\x0d\x0a  this.keyinfo = new KeyInfo(POOL_SIZE, keyName);\x0d\x0a  }\x0d\x0a  public synchronized static getInstance(String keyName){\x0d\x0a  if(instances.containsKey(keyName)){\x0d\x0a  return (KeyGenerator)instances.get(keyName);\x0d\x0a  }\x0d\x0a  else{\x0d\x0a  keyGen = new KeyGenerator(keyName);\x0d\x0a  instances.put(name, keyGen);\x0d\x0a  return keyGen;\x0d\x0a  }\x0d\x0a  }\x0d\x0a  public synzhronized int getNextKey(){\x0d\x0a  return keyinfo.getNextKey();\x0d\x0a  }\x0d\x0a  }
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式