JDK设计模式应用——单例模式(Singleton)(转)

JDK设计模式应用——单例模式(Singleton)

    《JDK源码分析》的分支,讲解设计模式在jdk中使用。

   我们从三个方面讲述,一是:jdk源码中的设计模式;二是:讲解设计模式(UML图);三是:实现我们自己的设计模式代码。今天带来最简单的设计模式——单例模式(Singleton)

 

一、jdk源码中的设计模式

 

   我们先看java.lang包下的class Runtime

Java代码   收藏代码
  1. public class Runtime {  
  2.   
  3.     private Runtime() {}//私有的构造方法  
  4.   
  5.     //Runtime类的私有静态实例  
  6.     private static Runtime currentRuntime = new Runtime();  
  7.   
  8.     //获得静态实例的唯一方法  
  9.     public static Runtime getRuntime() {  
  10.         return currentRuntime;  
  11.     }  
  12.   
  13. }  

    解释:

   (1)存在私有的构造函数,不能通过new来得到类的实例。

   (2)私有的静态实例,一个class只能对应唯一的对象。

   (3)只能通过:类.get方法获得这个唯一的对象实例。

 

     我们可以通过如下代码验证

Java代码   收藏代码
  1. Runtime r1 = Runtime.getRuntime();  
  2. Runtime r2 = Runtime.getRuntime();  
  3. //“==”为地址判断,为true表示:r1与r2指向同一对象。  
  4. System.out.println(r1 == r2);  

   结果显然为:true

 

 

二、讲解设计模式(UML图)

 

                     

   上面是Singleton的UML图

   单例模式是一种常用的软件设计模式。在它的核心结构中只包含一个被称为单例类的特殊类。通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问,从而方便对实例个数的控制并节约系统资源。如果希望在系统中某个类的对象只能存在一个,单例模式是最好的解决方案。(来源——百度百科)

   单例模式的思想使用在很多方面,比如:一台计算机可以有若干打印机,但是一个时间只能有一个Printer Spooler,避免两个文件同时输入到打印机中。

   单例模式满足三个条件:

   (1)某个类只能有一个实例。

   (2)类必须自行创建这个类的实例(创建静态成员变量或者在类的静态方法中生成实例)。

   (3)类必须自行向整个系统提供这个实例。

 

 

 三、实现我们自己的设计模式

 

    我们实现单例模式分为两种:1、静态成员变量  2、静态方法(有人成为饿汉式和懒汉式)

   1、静态成员变量

Java代码   收藏代码
  1. public class SingletonTest  
  2. {  
  3.         //在类里面实例化静态成员变量  
  4.     private static SingletonTest singletonTest = new SingletonTest();  
  5.   
  6.     private SingletonTest()  
  7.     {  
  8.     }  
  9.   
  10.     public static SingletonTest getInstance()  
  11.     {  
  12.         return singletonTest;  
  13.     }  
  14. }  

 

 2、静态方法(最常用的方法)

Java代码   收藏代码
  1. public class SingletonTest  
  2. {  
  3.     private static SingletonTest singletonTest = null;  
  4.   
  5.     private SingletonTest()  
  6.     {  
  7.     }  
  8.   
  9.     public static SingletonTest getInstance()  
  10.     {  
  11.         //在类的静态方法中实例化单例  
  12.         if (null == singletonTest)  
  13.         {  
  14.             singletonTest = new SingletonTest();  
  15.         }  
  16.   
  17.         return singletonTest;  
  18.     }  
  19. }  

 

3、第二种方法构建单例模式是最常用的,但是在并发程序中会导致单例模式失效。

   如下面的代码所示:

Java代码   收藏代码
  1. public class SingletonTest  
  2. {  
  3.     private static SingletonTest singletonTest = null;  
  4.   
  5.     private SingletonTest()  
  6.     {  
  7.     }  
  8.   
  9.     public static SingletonTest getInstance()  
  10.     {  
  11.         if (null == singletonTest)  
  12.         {  
  13.             try  
  14.             {  
  15.                 Thread.sleep((long) (Math.random() * 4000));  
  16.             } catch (InterruptedException e)  
  17.             {  
  18.                 // TODO Auto-generated catch block  
  19.                 e.printStackTrace();  
  20.             }  
  21.             singletonTest = new SingletonTest();  
  22.         }  
  23.   
  24.         return singletonTest;  
  25.     }  
  26.   
  27.     public static void main(String[] args)  
  28.     {  
  29.         // 打印出两个对象  
  30.         new ThreadTest().start();  
  31.         new ThreadTest().start();  
  32.     }  
  33. }  
  34.   
  35. class ThreadTest extends Thread  
  36. {  
  37.     @Override  
  38.     public void run()  
  39.     {  
  40.         System.out.println(SingletonTest.getInstance());  
  41.     }  
  42. }  

   输出的结果是:不是同一个对象,单例模式失效了。

Java代码   收藏代码
  1. com.shy.test.SingletonTest@55fe910c  
  2. com.shy.test.SingletonTest@3be4d6ef  

   对于单例模式(Singleton)来说,如果在 getInstance()方法中生成 Singleton 实例则可能会产生同步问题,即可能会生成两个不同的对象。

 

  解释:

   当两个线程同时执行到getInstance()方法时,此时singletonTest为null,两个线程都会分别实例化出一个对象,因此出现了两个对象。

  解决方法:(使用synchronized解决同步问题)

Java代码   收藏代码
  1. public class SingletonTest  
  2. {  
  3.     private static SingletonTest singletonTest = null;  
  4.     private SingletonTest()  
  5.     {  
  6.     }  
  7.     public static SingletonTest getInstance()  
  8.     {  
  9.         if (null == singletonTest)  
  10.         {  
  11.             synchronized (SingletonTest.class)  
  12.             {  
  13.                 singletonTest = new SingletonTest();  
  14.             }  
  15.         }  
  16.         return singletonTest;  
  17.     }  
  18. }  

总结:Java中单例模式(Singleton Pattern)定义:“一个类有且仅有一个实例,并且自行实例化向整个系统提供。”

转自:http://770736680.iteye.com/blog/2035740

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值