熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Java與模式之單例模式

2013-11-23 18:52:45  來源: Java核心技術 

  單例模式(Singleton) 屬於最常見的設計模式之一大部分系統都會用到目的是為了維護系統中唯一的一個實例
    可分為eager模式示例代碼如下
    Java代碼
    class EagerSingleton{
         private static final EagerSingleton m_instance = new EagerSingleton()
         private EagerSingleton(){}
         public static EagerSingleton getInstance(){
             return m_instance;
         }
    }
    class EagerSingleton{
    private static final EagerSingleton m_instance = new EagerSingleton()
    private EagerSingleton(){}
    public static EagerSingleton getInstance(){
    return m_instance;
    }
    }
    和 lazy模式示例代碼如下
    Java代碼
    class LazySingleton{
         private static LazySingleton m_instance = null;
         private LazySingleton(){}
         public synchronized static getInstance(){
            if(m_instance == null){
                   m_instance = new LazySingleton()
            }
            return m_instance;
         }
    }
    class LazySingleton{
    private static LazySingleton m_instance = null;
    private LazySingleton(){}
    public synchronized static getInstance(){
    if(m_instance == null){
    m_instance = new LazySingleton()
    }
    return m_instance;
    }
    }
    java源碼中RuntimegetRuntime()就是單例的一個例子
    單例模式的精神就是整個系統中維護一個實例推廣開來如果在一個系統中需要維護多個示例那麼就產生了多例模式(multiton)
    多例模式(Multiton) 通過聚集對象了保留自身的多個示例根據客戶端的參數返回所需要的實例
    示例代碼如下
    Java代碼
    class Multiton{
         private final int INSTANCE_SIZE = ;
         private static Map instances = new HashMap(INSTANCE_SIZE)
         private String name;
         private Multiton(){}
         private Multiton(String name){
              thisname = name;
         }
         public synchronized static getInstance(String name){
                if(ntainsKey(name)){
                     return instancesget(name)
                }
                else{
                     ins = new Multiton(name)
                     instancesput(name ins)
                     return ins;
                }
         }
    }
    class Multiton{
    private final int INSTANCE_SIZE = ;
    private static Map instances = new HashMap(INSTANCE_SIZE)
    private String name;
    private Multiton(){}
    private Multiton(String name){
    thisname = name;
    }
    public synchronized static getInstance(String name){
    if(ntainsKey(name)){
    return instancesget(name)
    }
    else{
    ins = new Multiton(name)
    instancesput(name ins)
    return ins;
    }
    }
    }

  [nextpage]
    一個實用的例子就是KeyGenerator 示例代碼如下
    Java代碼
    class KeyGenerator{
         private final int POOL_SIZE = ;
         private static Map instances = new HashMap(
         private KeyInfo keyinfo;
         private KeyGenerator(){}
         private KeyGenerator(String keyName){
              thiskeyinfo = new KeyInfo(POOL_SIZE keyName)
         }
         public synchronized static getInstance(String keyName){
                if(ntainsKey(keyName)){
                     return (KeyGenerator)instancesget(keyName)
                }
                else{
                     keyGen = new KeyGenerator(keyName)
                     instancesput(name keyGen)
                     return keyGen;
                }
         }
         public synzhronized int getNextKey(){
               return keyinfogetNextKey()
         }
    }
    class KeyGenerator{
    private final int POOL_SIZE = ;
    private static Map instances = new HashMap(
    private KeyInfo keyinfo;
    private KeyGenerator(){}
    private KeyGenerator(String keyName){
    thiskeyinfo = new KeyInfo(POOL_SIZE keyName)
    }
    public synchronized static getInstance(String keyName){
    if(ntainsKey(keyName)){
    return (KeyGenerator)instancesget(keyName)
    }
    else{
    keyGen = new KeyGenerator(keyName)
    instancesput(name keyGen)
    return keyGen;
    }
    }
    public synzhronized int getNextKey(){
    return keyinfogetNextKey()
    }
    }
    其實用單例模式也照樣可以實現KeyGenerator 無非是把聚集裡的KeyGenerator實例換成KeyInfo示例而已
    無論是用單例模式還是多例模式都可以做到同樣的一件事情


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25909.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.