Chinaunix首页 | 论坛 | 博客
  • 博客访问: 199834
  • 博文数量: 66
  • 博客积分: 966
  • 博客等级: 准尉
  • 技术积分: 550
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-11 11:52
文章分类

全部博文(66)

文章存档

2012年(60)

2011年(6)

分类: Java

2011-11-15 13:24:51

定义:作为对象的创建模式,单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例,这个类称为单例类。

单例模式的要点:
    1.这个类只能有一个实例
    2.它必须自行创建这个实例
    3.它必须自行向整个系统提供这个实例

饿汉式单例类
饿汉式单例类是在java语言里实现起来最为简单的单例类,实例如下:
代码清单1:饿汉式单例类

Java代码

  1. 1.public class EagerSingleton{   
  2. 2.    private static final EagerSingleton m_instance = new EagerSingleton();   
  3. 3.  
  4. 4.    /**  
  5. 5.     *  私有默认构造方法  
  6. 6.     */  
  7. 7.    private EagerSingleton(){}   
  8. 8.    /**  
  9. 9.     *  静态工厂方法  
  10. 10.     */  
  11. 11.    public static EagerSingleton getInstance(){   
  12. 12.        return m_instance;   
  13. 13.    }   
  14. 14.}  
  15. public class EagerSingleton{
  16.     private static final EagerSingleton m_instance = new EagerSingleton();

  17.     /**
  18.      *  私有默认构造方法
  19.      */
  20.     private EagerSingleton(){}
  21.     /**
  22.      *  静态工厂方法
  23.      */
  24.     public static EagerSingleton getInstance(){
  25.         return m_instance;
  26.     }
  27. }
复制代码

在这个类被加载时,静态变量m_instance会被初始化,此时类的私有构造方法会被调用,继而单例类的唯一实例就被创建出来了。

懒汉式单例类
与饿汉式单例类相同的是,类的构造方法仍是私有的。但懒汉式单例类是在第一次被引用时将自己初始化。实例如下:

Java代码

  1. 1.public class LazySingleton{   
  2. 2.    private static LazySingleton m_instance = null;   
  3. 3.    /**  
  4. 4.     *  私有默认构造方法  
  5. 5.     */  
  6. 6.    private LazySingleton(){}   
  7. 7.    /**  
  8. 8.     *  静态工厂方法  
  9. 9.     */  
  10. 10.    public synchronized static LazySingleton getInstance(){   
  11. 11.        if(m_instance == null){   
  12. 12.            m_instance = new LazySingleton();   
  13. 13.        }   
  14. 14.        return m_instance;   
  15. 15.    }   
  16. 16.}  
  17. public class LazySingleton{
  18.     private static LazySingleton m_instance = null;
  19.     /**
  20.      *  私有默认构造方法
  21.      */
  22.     private LazySingleton(){}
  23.     /**
  24.      *  静态工厂方法
  25.      */
  26.     public synchronized static LazySingleton getInstance(){
  27.         if(m_instance == null){
  28.             m_instance = new LazySingleton();
  29.         }
  30.         return m_instance;
  31.     }
  32. }
复制代码

同样,由于构造方法是私有的,懒汉式单例类也不能被继承,然而懒汉式单例类在实例化时,必须处理好在多个线程同时首次访问此类时的问题,所以必须考虑线程同步问题。

登记式单利模式
登记式单利模式是GoF为了克服饿汉式和懒汉式不可被继承的缺点而设计的。实例如下:

Java代码

  1. 1.public class RegSingleton         
  2. 2.{         
  3. 3.    static private HashMapKString,Object> m_registry = new HashMap();         
  4. 4.    static         
  5. 5.    {         
  6. 6.        RegSingleton x = new RegSingleton();         
  7. 7.        m_registry.put(x.getClass().getName(), x);         
  8. 8.    }   
  9. 9.    /**  
  10. 10.     *保护的构造方法  
  11. 11.     */  
  12. 12.    protected RegSingleton(){           
  13. 13.    }           
  14. 14.    public static RegSingleton getInstance(String name)         
  15. 15.    {         
  16. 16.        if (name == null)         
  17. 17.        {         
  18. 18.            name = "com.javapatterns.singleton.demos.RegSingleton";         
  19. 19.        }         
  20. 20.        if (m_registry.get(name) == null)         
  21. 21.        {         
  22. 22.            try         
  23. 23.            {         
  24. 24.                m_registry.put(name, Class.forName(name).newInstance());         
  25. 25.            }         
  26. 26.            catch (Exception e)         
  27. 27.            {         
  28. 28.                System.out.println("Error happened.");         
  29. 29.            }         
  30. 30.        }         
  31. 31.        return (RegSingleton) (m_registry.get(name));         
  32. 32.    }           
  33. 33.}     
  34. public class RegSingleton      
  35. {      
  36.     static private HashMapKString,Object> m_registry = new HashMap();      
  37.     static      
  38.     {      
  39.         RegSingleton x = new RegSingleton();      
  40.         m_registry.put(x.getClass().getName(), x);      
  41.     }
  42.     /**
  43.      *保护的构造方法
  44.      */
  45.     protected RegSingleton(){        
  46.     }        
  47.     public static RegSingleton getInstance(String name)      
  48.     {      
  49.         if (name == null)      
  50.         {      
  51.             name = "com.javapatterns.singleton.demos.RegSingleton";      
  52.         }      
  53.         if (m_registry.get(name) == null)      
  54.         {      
  55.             try      
  56.             {      
  57.                 m_registry.put(name, Class.forName(name).newInstance());      
  58.             }      
  59.             catch (Exception e)      
  60.             {      
  61.                 System.out.println("Error happened.");      
  62.             }      
  63.         }      
  64.         return (RegSingleton) (m_registry.get(name));      
  65.     }        
  66. }
复制代码

它的子类RegSingletonChild需要父类的帮助才能被初始化,如下:


Java代码

  1. 1.import java.util.HashMap;        
  2. 2.public class RegSingletonChild extends RegSingleton        
  3. 3.{        
  4. 4.    public RegSingletonChild() {}        
  5. 5.    public static RegSingletonChild getInstance(){        
  6. 6.        return (RegSingletonChild)RegSingleton.getInstance(        
  7. 7.        "com.javapatterns.singleton.demos.RegSingletonChild" );        
  8. 8.    }        
  9. 9.      
  10. 10.    public String about(){        
  11. 11.        return "Hello, I am RegSingletonChild.";        
  12. 12.    }        
  13. 13.}   
  14. import java.util.HashMap;     
  15. public class RegSingletonChild extends RegSingleton     
  16. {     
  17.     public RegSingletonChild() {}     
  18.     public static RegSingletonChild getInstance(){     
  19.         return (RegSingletonChild)RegSingleton.getInstance(     
  20.         "com.javapatterns.singleton.demos.RegSingletonChild" );     
  21.     }     
  22.    
  23.     public String about(){     
  24.         return "Hello, I am RegSingletonChild.";     
  25.     }     
  26. }
复制代码

使用单例模式的一个必要条件:在一个系统要求一个类只有一个实例时才应当使用单例模式。

阅读(1135) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:多线程入门(一)

给主人留下些什么吧!~~