设计模式------单例模式

转自:http://blog.csdn.net/jiujie395/article/details/8536880

单例模式分为三种:饿汉式单例,懒汉式单例,登记式单例

单例模式的特点:

1.单例只有一个实例

2.单例的实例对象都是自己实例化出来的

3.单例类必须给其他的对象提供这个唯一的实例

饿汉式单例:

[java]  view plain  copy
  1. <strong>package pattern.singleton;</strong>  
  2. /** 
  3.  * 饿汉式单例模式:在类被加载的时候,唯一实例已经被创建 
  4.  */  
  5. public class HungrySingleton {  
  6.     public static final HungrySingleton singleton = new HungrySingleton();  
  7.     private HungrySingleton(){} //私有构造方法,避免外部创建实例  
  8.       
  9.     public static HungrySingleton getInstance(){  
  10.         return singleton;  
  11.     }  
  12. }  

[java]  view plain  copy
  1. package pattern.singleton;  
  2.   
  3. /**  
  4.  * 通用的饿汉式单例模式 ,解决多线程访问的问题 
  5.  */   
  6. public class SingletonPattern {  
  7.     private static final SingletonPattern singletonPattern = new SingletonPattern();  
  8.       
  9.     private SingletonPattern(){}//私有构造方法,避免外部创建实例  
  10.       
  11.     public static synchronized SingletonPattern getInstance(){  
  12.         return singletonPattern;  
  13.     }  
  14. }  


懒汉式单例:

[java]  view plain  copy
  1. package pattern.singleton;  
  2. /** 
  3.  * 懒汉单例模式:只有在第一次请求实例的时候的时候创建,并且只在第一次创建后,以后不再创建该类的实例。 
  4.  */  
  5. public class LazySingleton {  
  6.     private static LazySingleton singleton = null;  
  7.     private LazySingleton(){}   //私有构造方法,避免外部创建实例  
  8.     public static LazySingleton getInstance(){  
  9.         if(singleton == null){  
  10.             singleton = new LazySingleton();  
  11.         }  
  12.         return singleton;  
  13.     }  
  14. }  
[java]  view plain  copy
  1. package pattern.singleton;  
  2.   
  3. /**  
  4.  * 通用的懒汉式单例模式 ,解决多线程访问的问题 
  5.  */   
  6. public class SingletonPattern {  
  7.     private static SingletonPattern singletonPattern = null;  
  8.       
  9.     private SingletonPattern(){}//私有构造方法,避免外部创建实例  
  10.       
  11.     public static synchronized SingletonPattern getInstance(){  
  12.         if(singletonPattern == null){  
  13.             singletonPattern = new SingletonPattern();  
  14.         }  
  15.         return singletonPattern;  
  16.     }  
  17. }  

登记式单例: 

[java]  view plain  copy
  1. package pattern.singleton;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5.   
  6. /** 
  7.  * 登记式单例实际上维护的是一组单例类的实例,将这些实例存放在一个Map(登记薄)中,对于已经登记过的实例, 
  8.  * 则从工厂直接返回,对于没有登记的,则先登记,而后返回. 
  9.  * 登记式单例类是为了克服饿汉式单例类及懒汉式单例类均不可继承的缺点而设计的.当单例类需要被继承时适用. 
  10.  */  
  11. public class RegisterSingleton {  
  12.     private static Map<String, RegisterSingleton> map = new HashMap<String, RegisterSingleton>();  
  13.     static {  
  14.         RegisterSingleton singleton = new RegisterSingleton();  
  15.         map.put(singleton.getClass().getName(), singleton);  
  16.     }  
  17.   
  18.     protected RegisterSingleton() {}// 受保护的构造函数  
  19.   
  20.     public static RegisterSingleton getInstance(String name) {  
  21.         if (name == null) {  
  22.             name = RegisterSingleton.class.getName();  
  23.             System.out.println(name);  
  24.         }  
  25.         if (map.get(name) == null) {  
  26.             try {  
  27.                 map.put(name, (RegisterSingleton) Class.forName(name)  
  28.                         .newInstance());  
  29.             } catch (Exception e) {  
  30.                 e.printStackTrace();  
  31.             }  
  32.         }  
  33.         return map.get(name);  
  34.     }  
  35. }  
  36.   
  37. package pattern.singleton;  
  38.   
  39. public class RegisterSingletonChild extends RegisterSingleton{  
  40.       
  41.     public static RegisterSingletonChild getInstance(){  
  42.         return (RegisterSingletonChild)RegisterSingleton.getInstance("pattern.singleton.RegisterSingletonChild");  
  43.     }  
  44.       
  45.     public static void main(String[] args) {  
  46.         RegisterSingletonChild.getInstance();  
  47.         RegisterSingleton.getInstance("pattern.singleton.RegisterSingletonChild");  
  48.     }  
  49. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值