码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA 设计模式之单例模式

时间:2015-03-01 15:46:10      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:java   设计模式   单列   singleton   

概念:
  java中单例模式是一种常见的设计模式,单例模式常见的有懒汉式、饿汉式。
  单例模式有一下特点:
  1、单例类只能有一个实例。
  2、单例类必须自己自己创建自己的唯一实例。
  3、单例类必须给所有其他对象提供这一实例。

  单例模式确保某个类只有一个实例,而且自行实例化并向整个系统提供这个实例。

    单列模式主要解决了一个类中只存在唯一一个对象。

    要想保证对象唯一:

    1、为了避免其他程序过多建立该对象,先禁止其他程序建立该对象。
  2、为了其它程序访问该类对象,只能在本类中自定义一个对象。
  3、为了方便其他程序方便对自定义对象的访问,可以对外提供一些访问方式。

    如何实现:

    1、将构造函数私有化。
  2、在类中创建一个本类对象。
  3、提供一个方法获取到该对象。

    饿汉式:(类初始化对象就进行实例化)(线程安全,开发推荐使用)

    //饿汉式单例类.在<span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><span style="font-family:宋体;color:black;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"></span></span></span></span></span>Singleton1 这个类初始化时,已经自行实例化   
    public class Singleton1 {  
        //私有的默认构造子  
        private Singleton1() {}  
        //已经自行实例化   
        private static final Singleton1 single = new Singleton1();  
        //静态工厂方法   
        public static Singleton1 getInstance() {  
            return single;  
        }  
    }  <span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"></span></span></span>

    懒汉式:(调用方法才会进行实例化)(不推荐使用)

<font color="#000000">//1.懒汉式单例类.在<span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding: 0px; line-height: 1.8; margin: 0px;"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding: 0px; line-height: 1.8; margin: 0px;"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding: 0px; line-height: 1.8; margin: 0px;"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><span style="font-family:宋体;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="font-family:宋体;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"></span></span></span></span></span></span></span></span></font></span></font></span></span></span></span></font></span></span></span>Singleton2这个类的 <span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding: 0px; line-height: 1.8; margin: 0px;"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding: 0px; line-height: 1.8; margin: 0px;"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><span style="font-family:宋体;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="font-family:宋体;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding: 0px; line-height: 1.8; margin: 0px;"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"></span></span></span></span></span></span></span></span></span></span></span></font></span></span></span>getInstance()方法被调用的时候实
<span style="font-family:宋体;">//   </span>例化(延时加载) (线程不安全,不推荐使用)  
public class Singleton2 {  
    //私有的默认构造子  
    private Singleton2() {}  
    //注意,这里没有final      
    private static Singleton2 single=null;  
    //静态工厂方法   
    public static Singleton2 getInstance() {  
         if (single == null) {    
             single = new Singleton2();  
         }    
        return single;  
    }  
} 

//<font color="#006600">2.加<span><span></span><span class="keyword">synchronized</span><span>  </span></span><span style="font-family:宋体;">锁</span>可解决线程不安全问题(加<span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="black"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="black"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="#FF0000"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><span style="font-family:宋体;color:black;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="color:#009900;"><span style="font-family:宋体;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"></span></span></span></span></span></span></span></span></span></font></span></font></span></span></span></span></font></span></span></span></font></font><span style="color:#000000;"><span style="color:#006600;"><span><span class="keyword">synchronized</span><span></span></span></span></span><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"></span><font color="#000000"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="black"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="black"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="#FF0000"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><span style="font-family:宋体;color:black;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="color:#009900;"><span style="font-family:宋体;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"></span></span></span></span></span></span></span></span></span></font></span></font></span></span></span></span></font></span></span></span></font><span style="color:#009900;"></span><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"></span><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"></span><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"></span><font color="#000000"><font color="#006600"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="black"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="black"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="#FF0000"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><span style="font-family:宋体;color:black;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="color:#009900;"><span style="font-family:宋体;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"></span></span></span></span></span></span></span></span></span></font></span></font></span></span></span></span></font></span></span></span>锁,效率会降低)</font>
<span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="black"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="black"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="#FF0000"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><span style="font-family:宋体;color:black;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="color:#009900;"><span style="font-family:宋体;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"></span></span></span></span></span></span></span></span></span></font></span></font></span></span></span></span></font></span></span></span></font><pre name="code" class="java"><font color="#000000">public class Singleton2 {  
    //私有的默认构造子  
    private Singleton2() {}  
    //注意,这里没有final      
    private static Singleton2 single=null;  
    //静态工厂方法   
    public static <span><span style="color:#FF0000;"><span></span><span class="keyword">synchronized</span><span>  </span></span><span class="keyword"></span></span>Singleton2 getInstance() {  
         if (single == null) {    
             single = new Singleton2();  
         }    
        return single;  
    }  
} 

<span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="black"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="black"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="#FF0000"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="black"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="color:#009900;"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="color:black;"><span style="font-size:14px;"><span style="color:#FF0000;"></span></span></span></span></font></span></span></font></span></span></span></font></font></span></font></span></span></span></span></font></span></span></span></font><pre name="code" class="java"><font color="#000000">//<font color="#006600"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><span style="font-family:宋体;color:black;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><span style="font-family:宋体;color:black;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="font-family:宋体;color:#FF0000;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><span style="font-family:宋体;color:black;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></font></font><span style="color:#000000;"><span style="color:#006600;"><span><span class="keyword"><span style="font-family:宋体;">3.</span>synchronized</span><span></span></span></span></span><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"></span><font color="#000000"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="black"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="black"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="font-family:宋体;color:#FF0000;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><span style="font-family:宋体;color:black;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="color:#009900;"><span style="font-family:宋体;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"></span></span></span></span></span></span></span></span></span></span></span></font></span></span></span></span></font></span></span></span></font><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"></span><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"></span><font color="#000000"><font color="#006600"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="black"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="black"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="font-family:宋体;color:#FF0000;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><span style="font-family:宋体;color:black;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="color:#009900;"><span style="font-family:宋体;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"></span></span></span></span></span></span></span></span></span></span></span></font></span></span></span></span></font></span></span></span>锁<span style="font-family:宋体;">判断次数比写法2要少,效率比写法而高,线程安全</span></font>
<span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="black"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="black"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="font-family:宋体;color:#FF0000;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><span style="font-family:宋体;color:black;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="color:#009900;"><span style="font-family:宋体;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"></span></span></span></span></span></span></span></span></span></span></span></font></span></span></span></span></font></span></span></span></font><pre name="code" class="java"><span style="color:#000000;">public class Singleton2 {  
    //私有的默认构造子  
    private Singleton2() {}  
    //注意,这里没有final      
    private static Singleton2 single=null;  
    //静态工厂方法   
    public static <span><span class="keyword"></span></span>Singleton2 getInstance() {  
         if (single == null) { <span style="font-family:宋体;">                </span>   <span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="black"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="black"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="#FF0000"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><span style="font-family:宋体;color:black;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="color:#009900;"><span style="font-family:宋体;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="color:black;"></span></span></span></span></span></span></span></span></span></font></font></span></font></span></span></span></span></font></span></span></span></span><pre name="code" class="java"><span style="color:#000000;"></span><pre name="code" class="java"><pre name="code" class="java"><span style="color:#000000;"><span><font color="#FF0000"><span class="keyword">              synchronized</span><span>(Singleton2.class){
<font face="宋体"> <font color="#000000">                 if(single == null){                       <span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding: 0px; line-height: 1.8; margin: 0px;"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding: 0px; line-height: 1.8; margin: 0px;"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding: 0px; line-height: 1.8; margin: 0px;"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><span style="font-family:宋体;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="font-family:宋体;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"></span></span></span></span></span></span></span></span></font></span></font></span></span></span></span></font></span></span></span></font></font></span></font></span></span><pre name="code" class="java"><pre name="code" class="java"><pre name="code" class="java"><pre name="code" class="java"><pre name="code" class="java"><span style="color:#000000;">                   single = new Singleton2(); </span>





}
}  } return single; } }










    以上两种方式的区别:

<span style="color:#000000;">    饿汉式单例类.在<span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding: 0px; line-height: 1.8; margin: 0px;"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><span style="font-family:宋体;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"></span></span></span></span></span>Singleton1 这个类初始化时,对象已经自行实例化。</span>
<span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="black"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="black"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="#FF0000"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="black"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="color:#009900;"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="color:black;"><span style="font-size:14px;"><span style="color:#FF0000;"></span></span></span></span></font></span></span></font></span></span></span></font></font></span></font></span></span></span></span></font></span></span></span><pre name="code" class="java"><font color="#000000">    懒汉式单例类.在<span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding: 0px; line-height: 1.8; margin: 0px;"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding: 0px; line-height: 1.8; margin: 0px;"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><span style="font-family:宋体;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="font-family:宋体;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding: 0px; line-height: 1.8; margin: 0px;"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><span style="font-family:宋体;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="font-family:宋体;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></font></span></span></span>Singleton2这个类初始化时<span style="font-family:宋体;">对象不存在</span>,<span style="font-family:宋体;">调用</span><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding: 0px; line-height: 1.8; margin: 0px;"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><span style="font-family:宋体;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding: 0px; line-height: 1.8; margin: 0px;"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><span style="font-family:宋体;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="font-family:宋体;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="font-family:宋体;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding: 0px; line-height: 1.8; margin: 0px;"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="black"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="black"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="#FF0000"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><span style="font-family:宋体;color:black;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="color:#009900;"><span style="font-family:宋体;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"></span></span></span></span></span></span></span></span></span></font></span></font></span></span></span></span></font></span></span></span></font><span style="color:#000000;">Singleton2这个
    类</span><font color="#000000"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="black"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="black"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="font-family:宋体;color:#FF0000;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><span style="font-family:宋体;color:black;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="color:#009900;"></span></span></span></span></span></span></span></span></span></font></span></span></span></span></font></span></span></span></font><font color="#000000"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="black"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="black"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><font style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" face="宋体" color="#FF0000"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; color:rgb(0,128,0); padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px" lang="EN-US"><span style="font-family:宋体;color:black;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span size="3" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="color:#009900;"><span style="font-family:宋体;padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"></span></span></span></span></span></span></span></span></span></font></span></font></span></span></span></span></font></span></span></span>getInstance()方法被调用的时候对象才实例化<span style="font-family:宋体;">。

     <span style="font-size:18px;color:#FF0000;">建议:开发使用饿汉式
    
</span></span></font>






JAVA 设计模式之单例模式

标签:java   设计模式   单列   singleton   

原文地址:http://blog.csdn.net/joakim_zhou/article/details/44002689

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!