设计模式 - 单件模式(singleton pattern) 详解

来源:互联网 发布:电子数据交换的好处 编辑:程序博客网 时间:2024/05/16 04:40

单件模式(singleton pattern) : 确保一个类只有一个实例, 并提供一个全局访问点.

单价模式包括3个部分私有构造器静态变量静态方法.


具体方法:

1. 标准的单例模式:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * @time 2014.6.5 
  3.  */  
  4. package singleton;  
  5.   
  6. /** 
  7.  * @author C.L.Wang 
  8.  * 
  9.  */  
  10. public class Singleton {  
  11.     private static Singleton uniqueInstance; //静态变量  
  12.       
  13.     private Singleton() {} //私有构造函数  
  14.       
  15.     public static Singleton getInstance() { //静态方法  
  16.         if (uniqueInstance == null)  
  17.             uniqueInstance = new Singleton();  
  18.         return uniqueInstance;  
  19.     }  
  20.   
  21. }  

2. 考虑多线程的三种方法:

同步(synchronized)方法, 添加"synchronized",  会导致性能下降, 每次调用示例, 都需要同步, 但是使用简单.

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * @time 2014.6.5 
  3.  */  
  4. package singleton;  
  5.   
  6. /** 
  7.  * @author C.L.Wang 
  8.  * 
  9.  */  
  10. public class Singleton {  
  11.     private static Singleton uniqueInstance; //静态变量  
  12.       
  13.     private Singleton() {} //私有构造函数  
  14.       
  15.     public static synchronized Singleton getInstance() { //静态方法  
  16.         if (uniqueInstance == null)  
  17.             uniqueInstance = new Singleton();  
  18.         return uniqueInstance;  
  19.     }  
  20.   
  21. }  


急切(eagerly)方法, 开始时创建实例, 会在不需要时, 占用实例空间, 即占用空间时间过长.

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * @time 2014.6.5 
  3.  */  
  4. package singleton;  
  5.   
  6. /** 
  7.  * @author C.L.Wang 
  8.  * 
  9.  */  
  10. public class Singleton {  
  11.     private static Singleton uniqueInstance = new Singleton(); //静态变量  
  12.       
  13.     private Singleton() {} //私有构造函数  
  14.       
  15.     public static synchronized Singleton getInstance() { //静态方法  
  16.         //if (uniqueInstance == null)  
  17.             //uniqueInstance = new Singleton();  
  18.         return uniqueInstance;  
  19.     }  
  20.   
  21. }  

双重检查加锁(double-checked locking)方法, 使用"volatile"和"synchronized (Singleton.class)", 减少时间消耗, 适用于java1.4以上版本.

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * @time 2014.6.5 
  3.  */  
  4. package singleton;  
  5.   
  6. /** 
  7.  * @author C.L.Wang 
  8.  * 
  9.  */  
  10. public class Singleton {  
  11.     private volatile static Singleton uniqueInstance; //静态变量  
  12.       
  13.     private Singleton() {} //私有构造函数  
  14.       
  15.     public static synchronized Singleton getInstance() { //静态方法  
  16.         if (uniqueInstance == null) {  
  17.             synchronized (Singleton.class) {  
  18.                 if (uniqueInstance == null)  
  19.                     uniqueInstance = new Singleton();  
  20.             }  
  21.         }  
  22.         return uniqueInstance;  
  23.     }  
  24.   
  25. }  

3. 使用单件模式的例子:

代码:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * @time 2014.6.5 
  3.  */  
  4. package singleton;  
  5.   
  6. /** 
  7.  * @author C.L.Wang 
  8.  * 
  9.  */  
  10. public class ChocolateBoiler { //巧克力锅炉  
  11.     private boolean empty;  
  12.     private boolean boiled;  
  13.       
  14.     public static ChocolateBoiler uniqueInstance; //静态变量  
  15.       
  16.     private ChocolateBoiler() { //私有构造函数  
  17.         empty = true;  
  18.         boiled = false;  
  19.     }  
  20.       
  21.     public static ChocolateBoiler getInstance() { //静态方法  
  22.         if (uniqueInstance == null)   
  23.             uniqueInstance = new ChocolateBoiler();  
  24.         return uniqueInstance;  
  25.     }  
  26.       
  27.     public void fill() { //填满  
  28.         if (isEmpty()) {  
  29.             empty = false;  
  30.             boiled = false;  
  31.         }  
  32.     }  
  33.       
  34.     public void drain() { //倾倒  
  35.         if (!isEmpty() && isBoiled())  
  36.             empty = true;  
  37.     }  
  38.       
  39.     public void boil() { //煮  
  40.         if (!isEmpty() && !isBoiled()) {  
  41.             boiled = true;  
  42.         }  
  43.     }  
  44.       
  45.     public boolean isEmpty() {  
  46.         return empty;  
  47.     }  
  48.       
  49.     public boolean isBoiled() {  
  50.         return boiled;  
  51.     }  
  52.   
  53. }  

4. 枚举单件(enum singleton)模式, 也可以保证线程安全.

代码:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * @time 2014.6.5 
  3.  */  
  4. package singleton;  
  5.   
  6. /** 
  7.  * @author C.L.Wang 
  8.  * 
  9.  */  
  10. public class EnumSingleton {  
  11.   
  12.     /** 
  13.      * @param args 
  14.      */  
  15.     public static void main(String[] args) {  
  16.         // TODO Auto-generated method stub  
  17.   
  18.         eSingleton d1 = eSingleton.INSTANCE;  
  19.         d1.setName("Spike");  
  20.           
  21.         eSingleton d2 = eSingleton.INSTANCE;  
  22.         d2.setName("Caroline");  
  23.           
  24.         System.out.println(d1);  
  25.         System.out.println(d2);  
  26.           
  27.         System.out.println(d1 == d2);  
  28.     }  
  29.   
  30. }  
  31.   
  32. enum eSingleton {  
  33.       
  34.     INSTANCE;  
  35.       
  36.     private String name;  
  37.       
  38.     public String getName() {  
  39.         return name;  
  40.     }  
  41.       
  42.     public void setName(String name) {  
  43.         this.name = name;  
  44.     }  
  45.       
  46.     @Override  
  47.     public String toString() {  
  48.         return "[" + name + "]";  
  49.     }  
  50. }  

输出:

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. [Caroline]  
  2. [Caroline]  
  3. true  
0 0
原创粉丝点击