JAVA设计模式之单例模式

<div id="article_details" class="details">
    <div class="article_title">   
         <span class="ico ico_type_Original"></span>

    <h1>
        <span class="link_title"><a href="/jason0539/article/details/23297037">
        JAVA设计模式之单例模式        
           
        </a>
        </span>

         
    </h1>
</div>

   

        <div class="article_manage clearfix">
        <div class="article_l">
            <span class="link_categories">
            标签:
              <a href="http://www.csdn.net/tag/java%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f" target="_blank" οnclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">java设计模式</a><a href="http://www.csdn.net/tag/%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f" target="_blank" οnclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">设计模式</a>
            </span>
        </div>
        <div class="article_r">
            <span class="link_postdate">2014-04-16 06:51</span>
            <span class="link_view" title="阅读次数">494828人阅读</span>
            <span class="link_comments" title="评论次数"> <a href="#comments" οnclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_pinglun'])">评论</a>(92)</span>
            <span class="link_collect tracking-ad" data-mod="popu_171"> <a href="javascript:void(0);" οnclick="javascript:collectArticle('JAVA%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f%e4%b9%8b%e5%8d%95%e4%be%8b%e6%a8%a1%e5%bc%8f','23297037');return false;" title="收藏" target="_blank">收藏</a></span>
             <span class="link_report"> <a href="#report" οnclick="javascript:report(23297037,2);return false;" title="举报">举报</a></span>

        </div>
    </div>    <style type="text/css">        
            .embody{
                padding:10px 10px 10px;
                margin:0 -20px;
                border-bottom:solid 1px #ededed;                
            }
            .embody_b{
                margin:0 ;
                padding:10px 0;
            }
            .embody .embody_t,.embody .embody_c{
                display: inline-block;
                margin-right:10px;
            }
            .embody_t{
                font-size: 12px;
                color:#999;
            }
            .embody_c{
                font-size: 12px;
            }
            .embody_c img,.embody_c em{
                display: inline-block;
                vertical-align: middle;               
            }
             .embody_c img{               
                width:30px;
                height:30px;
            }
            .embody_c em{
                margin: 0 20px 0 10px;
                color:#333;
                font-style: normal;
            }
    </style>
    <script type="text/javascript">
        $(function () {
            try
            {
                var lib = eval("("+$("#lib").attr("value")+")");
                var html = "";
                if (lib.err == 0) {
                    $.each(lib.data, function (i) {
                        var obj = lib.data[i];
                        //html += '<img src="' + obj.logo + '"/>' + obj.name + "  ";
                        html += ' <a href="' + obj.url + '" target="_blank">';
                        html += ' <img src="' + obj.logo + '">';
                        html += ' <em><b>' + obj.name + '</b></em>';
                        html += ' </a>';
                    });
                    if (html != "") {
                        setTimeout(function () {
                            $("#lib").html(html);                      
                            $("#embody").show();
                        }, 100);
                    }
                }      
            } catch (err)
            { }
            
        });
    </script>

  

  
  
     


<div id="article_content" class="article_content tracking-ad" data-mod="popu_307" data-dsm="post">
<p><span style="color: rgb(54, 46, 43); font-family: Arial; font-size: 14px; line-height: 26px;">本文继续介绍</span><a target="_blank" href="http://blog.csdn.net/jason0539/article/details/44956775" style="color: rgb(106, 57, 6); text-decoration: none; font-family: Arial; font-size: 14px; line-height: 26px;">23种设计模式系列</a><span style="color: rgb(54, 46, 43); font-family: Arial; font-size: 14px; line-height: 26px;">之单例模式。</span><br></p><p><span style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; font-family:宋体; padding-top:0px"><span style="font-size:14px;">概念:<span lang="EN-US" style="padding: 0px; line-height: 1.8; margin: 0px;"><br style="padding-bottom:0px; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"></span>  <span lang="EN-US" style="padding: 0px; line-height: 1.8; margin: 0px;"><a href="http://lib.csdn.net/base/java" class="replace_word" title="Java 知识库" target="_blank" style="color:#df3434; font-weight:bold;">Java</a></span>中单例模式是一种常见的设计模式,单例模式的写法有好几种,这里主要介绍三种:懒汉式单例、饿汉式单例、登记式单例。<span lang="EN-US" style="padding: 0px; line-height: 1.8; margin: 0px;"><br style="padding-bottom:0px; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"></span>  单例模式有以下特点:<span lang="EN-US" style="padding: 0px; line-height: 1.8; margin: 0px;"><br style="padding-bottom:0px; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"></span>  <span style="padding: 0px; line-height: 1.8; margin: 0px; color: rgb(0, 128, 0);"><span lang="EN-US" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px">1</span>、单例类只能有一个实例。<span lang="EN-US" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><br style="padding-bottom:0px; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"></span>  <span lang="EN-US" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px">2</span>、单例类必须自己创建自己的唯一实例。<span lang="EN-US" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><br style="padding-bottom:0px; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"></span>  <span lang="EN-US" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px">3</span>、单例类必须给所有其他对象提供这一实例。<span lang="EN-US" style="padding-bottom:0px; line-height:1.8; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"><br style="padding-bottom:0px; 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:宋体; color:black; 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">  单例模式确保某个类只有一个实例,而且自行实例化并向整个系统提供这个实例。在计算机系统中,线程池、缓存、日志对象、对话框、打印机、显卡的驱动程序对象常被设计成单例。这些应用都或多或少具有资源管理器的功能。每台计算机可以有若干个打印机,但只能有一个Printer Spooler,以避免两个打印作业同时输出到打印机中。每台计算机可以有若干通信端口,系统应当集中管理这些通信端口,以避免一个通信端口同时被两个请求同时调用。总之,选择单例模式就是为了避免不一致状态,避免政出多头。</span></span></span></span></span></span></p><p align="left"><br><span style="font-size:18px;">一、懒汉式单例</span></p><p align="left"></p><div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><b>[java]</b> <a href="#" class="ViewSource" title="view plain" οnclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" target="_blank">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" οnclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" target="_blank">copy</a><div style="position: absolute; left: 803px; top: 755px; width: 27px; height: 15px; z-index: 99;"><embed id="ZeroClipboardMovie_1" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="27" height="15" name="ZeroClipboardMovie_1" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=1&width=27&height=15" wmode="transparent"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" οnclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" target="_blank">print</a></span><a href="#" class="About" title="?" οnclick="dp.sh.Toolbar.Command('About',this);return false;" target="_blank">?</a></div></div><ol start="1" class="dp-j"><li class="alt"><span><span class="comment">//懒汉式单例类.在第一次调用的时候实例化自己 </span><span>  </span></span></li><li class=""><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> Singleton {  </span></span></li><li class="alt"><span>    <span class="keyword">private</span><span> Singleton() {}  </span></span></li><li class=""><span>    <span class="keyword">private</span><span> </span><span class="keyword">static</span><span> Singleton single=</span><span class="keyword">null</span><span>;  </span></span></li><li class="alt"><span>    <span class="comment">//静态工厂方法 </span><span>  </span></span></li><li class=""><span>    <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> Singleton getInstance() {  </span></span></li><li class="alt"><span>         <span class="keyword">if</span><span> (single == </span><span class="keyword">null</span><span>) {    </span></span></li><li class=""><span>             single = <span class="keyword">new</span><span> Singleton();  </span></span></li><li class="alt"><span>         }    </span></li><li class=""><span>        <span class="keyword">return</span><span> single;  </span></span></li><li class="alt"><span>    }  </span></li><li class=""><span>}  </span></li></ol></div><pre code_snippet_id="294885" snippet_file_name="blog_20140416_5_6095009" class="java" name="code" style="display: none;">//懒汉式单例类.在第一次调用的时候实例化自己 
public class Singleton {
    private Singleton() {}
    private static Singleton single=null;
    //静态工厂方法 
    public static Singleton getInstance() {
         if (single == null) {  
             single = new Singleton();
         }  
        return single;
    }
}</pre><div><span style="font-size:14px;"><br></span></div><div><span style="font-size:14px;">Singleton通过将构造方法限定为private避免了类在外部被实例化,在同一个虚拟机范围内,Singleton的唯一实例只能通过getInstance()方法访问。</span></div><p align="left" style=""><span style="font-size:14px;">(事实上,通过Java反射机制是能够实例化构造方法为private的类的,那基本上会使所有的Java单例实现失效。此问题在此处不做讨</span><span style="font-size:14px;">论,姑且掩耳盗铃地认为反射机制不存在。)</span></p><p align="left" style=""><span style="font-size: 14px;">但是以上懒汉式单例的实现没有考虑线程安全问题,它是线程不安全的,并发环境下很可能出现多个Singleton实例,要实现线程安全,有以下三种方式,都是对getInstance这个方法改造,保证了懒汉式单例的线程安全,如果你第一次接触单例模式,对线程安全不是很了解,可以先跳过下面这三小条,去看饿汉式单例,等看完后面再回头考虑线程安全的问题:</span></p><p align="left" style=""><span style="font-size:14px;"><span style="font-size: 14px;"><br></span></span></p><p align="left" style=""><span style="font-size:14px;"><span style="font-size: 14px;">1、在getInstance方法上加同步</span></span></p><p align="left" style=""><span style="font-size:14px;"><span style="font-size: 14px;"></span></span></p><div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><b>[java]</b> <a href="#" class="ViewSource" title="view plain" οnclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" target="_blank">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" οnclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" target="_blank">copy</a><div style="position: absolute; left: 803px; top: 1352px; width: 27px; height: 15px; z-index: 99;"><embed id="ZeroClipboardMovie_2" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="27" height="15" name="ZeroClipboardMovie_2" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=2&width=27&height=15" wmode="transparent"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" οnclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" target="_blank">print</a></span><a href="#" class="About" title="?" οnclick="dp.sh.Toolbar.Command('About',this);return false;" target="_blank">?</a></div></div><ol start="1" class="dp-j"><li class="alt"><span><span class="keyword">public</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">synchronized</span><span> Singleton getInstance() {  </span></span></li><li class=""><span>         <span class="keyword">if</span><span> (single == </span><span class="keyword">null</span><span>) {    </span></span></li><li class="alt"><span>             single = <span class="keyword">new</span><span> Singleton();  </span></span></li><li class=""><span>         }    </span></li><li class="alt"><span>        <span class="keyword">return</span><span> single;  </span></span></li><li class=""><span>}  </span></li></ol></div><pre code_snippet_id="294885" snippet_file_name="blog_20150801_12_2606987" name="code" class="java" style="display: none;">public static synchronized Singleton getInstance() {
         if (single == null) {  
             single = new Singleton();
         }  
        return single;
}</pre><br><p></p><p align="left" style=""><span style="font-size:14px;"><span style="font-size: 14px;">2、<span style="font-family:'Helvetica Neue';font-size:14px;">双重检查锁定</span></span><br></span></p><p align="left" style=""><span style="font-size:14px;"><span style="font-size: 14px;"></span></span></p><div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><b>[java]</b> <a href="#" class="ViewSource" title="view plain" οnclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" target="_blank">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" οnclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" target="_blank">copy</a><div style="position: absolute; left: 803px; top: 1597px; width: 27px; height: 15px; z-index: 99;"><embed id="ZeroClipboardMovie_3" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="27" height="15" name="ZeroClipboardMovie_3" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=3&width=27&height=15" wmode="transparent"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" οnclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" target="_blank">print</a></span><a href="#" class="About" title="?" οnclick="dp.sh.Toolbar.Command('About',this);return false;" target="_blank">?</a></div></div><ol start="1" class="dp-j"><li class="alt"><span><span class="keyword">public</span><span> </span><span class="keyword">static</span><span> Singleton getInstance() {  </span></span></li><li class=""><span>        <span class="keyword">if</span><span> (singleton == </span><span class="keyword">null</span><span>) {    </span></span></li><li class="alt"><span>            <span class="keyword">synchronized</span><span> (Singleton.</span><span class="keyword">class</span><span>) {    </span></span></li><li class=""><span>               <span class="keyword">if</span><span> (singleton == </span><span class="keyword">null</span><span>) {    </span></span></li><li class="alt"><span>                  singleton = <span class="keyword">new</span><span> Singleton();   </span></span></li><li class=""><span>               }    </span></li><li class="alt"><span>            }    </span></li><li class=""><span>        }    </span></li><li class="alt"><span>        <span class="keyword">return</span><span> singleton;   </span></span></li><li class=""><span>    }  </span></li></ol></div><pre code_snippet_id="294885" snippet_file_name="blog_20150801_13_2050580" name="code" class="java" style="display: none;">public static Singleton getInstance() {
        if (singleton == null) {  
            synchronized (Singleton.class) {  
               if (singleton == null) {  
                  singleton = new Singleton(); 
               }  
            }  
        }  
        return singleton; 
    }</pre><p></p><p align="left" style="">3、静态内部类</p><p align="left" style=""></p><div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><b>[java]</b> <a href="#" class="ViewSource" title="view plain" οnclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" target="_blank">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" οnclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" target="_blank">copy</a><div style="position: absolute; left: 803px; top: 1878px; width: 27px; height: 15px; z-index: 99;"><embed id="ZeroClipboardMovie_4" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="27" height="15" name="ZeroClipboardMovie_4" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=4&width=27&height=15" wmode="transparent"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" οnclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" target="_blank">print</a></span><a href="#" class="About" title="?" οnclick="dp.sh.Toolbar.Command('About',this);return false;" target="_blank">?</a></div></div><ol start="1" class="dp-j"><li class="alt"><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> Singleton {    </span></span></li><li class=""><span>    <span class="keyword">private</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">class</span><span> LazyHolder {    </span></span></li><li class="alt"><span>       <span class="keyword">private</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">final</span><span> Singleton INSTANCE = </span><span class="keyword">new</span><span> Singleton();    </span></span></li><li class=""><span>    }    </span></li><li class="alt"><span>    <span class="keyword">private</span><span> Singleton (){}    </span></span></li><li class=""><span>    <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">final</span><span> Singleton getInstance() {    </span></span></li><li class="alt"><span>       <span class="keyword">return</span><span> LazyHolder.INSTANCE;    </span></span></li><li class=""><span>    }    </span></li><li class="alt"><span>}    </span></li></ol></div><pre code_snippet_id="294885" snippet_file_name="blog_20150801_14_3446019" name="code" class="java" style="display: none;">public class Singleton {  
    private static class LazyHolder {  
       private static final Singleton INSTANCE = new Singleton();  
    }  
    private Singleton (){}  
    public static final Singleton getInstance() {  
       return LazyHolder.INSTANCE;  
    }  
}  </pre><span style="font-family:Helvetica Neue;"><span style="font-size: 14px;">这种比上面1、2都好一些,既实现了线程安全,又避免了同步带来的性能影响。</span></span><br><br><p></p><p align="left" style=""><br></p><p align="left"><span style="font-size:18px;">二、饿汉式单例</span></p><p align="left"></p><div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><b>[java]</b> <a href="#" class="ViewSource" title="view plain" οnclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" target="_blank">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" οnclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" target="_blank">copy</a><div style="position: absolute; left: 803px; top: 2246px; width: 27px; height: 15px; z-index: 99;"><embed id="ZeroClipboardMovie_5" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="27" height="15" name="ZeroClipboardMovie_5" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=5&width=27&height=15" wmode="transparent"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" οnclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" target="_blank">print</a></span><a href="#" class="About" title="?" οnclick="dp.sh.Toolbar.Command('About',this);return false;" target="_blank">?</a></div></div><ol start="1" class="dp-j"><li class="alt"><span><span class="comment">//饿汉式单例类.在类初始化时,已经自行实例化 </span><span>  </span></span></li><li class=""><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> Singleton1 {  </span></span></li><li class="alt"><span>    <span class="keyword">private</span><span> Singleton1() {}  </span></span></li><li class=""><span>    <span class="keyword">private</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">final</span><span> Singleton1 single = </span><span class="keyword">new</span><span> Singleton1();  </span></span></li><li class="alt"><span>    <span class="comment">//静态工厂方法 </span><span>  </span></span></li><li class=""><span>    <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> Singleton1 getInstance() {  </span></span></li><li class="alt"><span>        <span class="keyword">return</span><span> single;  </span></span></li><li class=""><span>    }  </span></li><li class="alt"><span>}  </span></li></ol></div><pre code_snippet_id="294885" snippet_file_name="blog_20140416_4_5080470" class="java" name="code" style="display: none;">//饿汉式单例类.在类初始化时,已经自行实例化 
public class Singleton1 {
    private Singleton1() {}
    private static final Singleton1 single = new Singleton1();
    //静态工厂方法 
    public static Singleton1 getInstance() {
        return single;
    }
}</pre><div><span style="font-size:14px;">饿汉式在类创建的同时就已经创建好一个静态的对象供系统使用,以后不再改变,所以天生是线程安全的。</span></div><div><br></div><br><p></p><p align="left"><span style="font-size:18px;">三、登记式单例(可忽略)</span></p><div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><b>[java]</b> <a href="#" class="ViewSource" title="view plain" οnclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" target="_blank">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" οnclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" target="_blank">copy</a><div style="position: absolute; left: 803px; top: 2614px; width: 27px; height: 15px; z-index: 99;"><embed id="ZeroClipboardMovie_6" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="27" height="15" name="ZeroClipboardMovie_6" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=6&width=27&height=15" wmode="transparent"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" οnclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" target="_blank">print</a></span><a href="#" class="About" title="?" οnclick="dp.sh.Toolbar.Command('About',this);return false;" target="_blank">?</a></div></div><ol start="1" class="dp-j"><li class="alt"><span><span class="comment">//类似Spring里面的方法,将类名注册,下次从里面直接获取。</span><span>  </span></span></li><li class=""><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> Singleton3 {  </span></span></li><li class="alt"><span>    <span class="keyword">private</span><span> </span><span class="keyword">static</span><span> Map<String,Singleton3> map = </span><span class="keyword">new</span><span> HashMap<String,Singleton3>();  </span></span></li><li class=""><span>    <span class="keyword">static</span><span>{  </span></span></li><li class="alt"><span>        Singleton3 single = <span class="keyword">new</span><span> Singleton3();  </span></span></li><li class=""><span>        map.put(single.getClass().getName(), single);  </span></li><li class="alt"><span>    }  </span></li><li class=""><span>    <span class="comment">//保护的默认构造子</span><span>  </span></span></li><li class="alt"><span>    <span class="keyword">protected</span><span> Singleton3(){}  </span></span></li><li class=""><span>    <span class="comment">//静态工厂方法,返还此类惟一的实例</span><span>  </span></span></li><li class="alt"><span>    <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> Singleton3 getInstance(String name) {  </span></span></li><li class=""><span>        <span class="keyword">if</span><span>(name == </span><span class="keyword">null</span><span>) {  </span></span></li><li class="alt"><span>            name = Singleton3.<span class="keyword">class</span><span>.getName();  </span></span></li><li class=""><span>            System.out.println(<span class="string">"name == null"</span><span>+</span><span class="string">"--->name="</span><span>+name);  </span></span></li><li class="alt"><span>        }  </span></li><li class=""><span>        <span class="keyword">if</span><span>(map.get(name) == </span><span class="keyword">null</span><span>) {  </span></span></li><li class="alt"><span>            <span class="keyword">try</span><span> {  </span></span></li><li class=""><span>                map.put(name, (Singleton3) Class.forName(name).newInstance());  </span></li><li class="alt"><span>            } <span class="keyword">catch</span><span> (InstantiationException e) {  </span></span></li><li class=""><span>                e.printStackTrace();  </span></li><li class="alt"><span>            } <span class="keyword">catch</span><span> (IllegalAccessException e) {  </span></span></li><li class=""><span>                e.printStackTrace();  </span></li><li class="alt"><span>            } <span class="keyword">catch</span><span> (ClassNotFoundException e) {  </span></span></li><li class=""><span>                e.printStackTrace();  </span></li><li class="alt"><span>            }  </span></li><li class=""><span>        }  </span></li><li class="alt"><span>        <span class="keyword">return</span><span> map.get(name);  </span></span></li><li class=""><span>    }  </span></li><li class="alt"><span>    <span class="comment">//一个示意性的商业方法</span><span>  </span></span></li><li class=""><span>    <span class="keyword">public</span><span> String about() {      </span></span></li><li class="alt"><span>        <span class="keyword">return</span><span> </span><span class="string">"Hello, I am RegSingleton."</span><span>;      </span></span></li><li class=""><span>    }      </span></li><li class="alt"><span>    <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">void</span><span> main(String[] args) {  </span></span></li><li class=""><span>        Singleton3 single3 = Singleton3.getInstance(<span class="keyword">null</span><span>);  </span></span></li><li class="alt"><span>        System.out.println(single3.about());  </span></li><li class=""><span>    }  </span></li><li class="alt"><span>}  </span></li></ol></div><pre code_snippet_id="294885" snippet_file_name="blog_20140416_6_8189343" class="java" name="code" style="display: none;">//类似Spring里面的方法,将类名注册,下次从里面直接获取。
public class Singleton3 {
    private static Map<String,Singleton3> map = new HashMap<String,Singleton3>();
    static{
        Singleton3 single = new Singleton3();
        map.put(single.getClass().getName(), single);
    }
    //保护的默认构造子
    protected Singleton3(){}
    //静态工厂方法,返还此类惟一的实例
    public static Singleton3 getInstance(String name) {
        if(name == null) {
            name = Singleton3.class.getName();
            System.out.println("name == null"+"--->name="+name);
        }
        if(map.get(name) == null) {
            try {
                map.put(name, (Singleton3) Class.forName(name).newInstance());
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        return map.get(name);
    }
    //一个示意性的商业方法
    public String about() {    
        return "Hello, I am RegSingleton.";    
    }    
    public static void main(String[] args) {
        Singleton3 single3 = Singleton3.getInstance(null);
        System.out.println(single3.about());
    }
}</pre><p align="left"><span style="font-size:12px;"> </span><span style="font-size: 14px;">登记式单例</span><span style="font-size: 14px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;">实际上维护了一组单例类的实例,将这些实例存放在一个Map(登记薄)中,对于已经登记过的实例,则从Map直接返回,对于没有登记的,则先登记,然后返回。</span><span style="font-size: 14px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;"> </span></p><p align="left"><span style="font-size: 14px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;">这里我对登记式单例标记了可忽略,我的理解来说,首先它用的比较少,另外其实内部实现还是用的饿汉式单例,因为其中的static方法块,它的单例在类被装载的时候就被实例化了。</span></p><p align="left"><span style="font-size:14px;"><br></span></p><p align="left"><span style="font-size:18px;">饿汉式和懒汉式区别</span></p><p align="left"><span style="font-size:14px;">从名字上来说,饿汉和懒汉,</span></p><p align="left"><span style="font-size:14px;">饿汉就是类一旦加载,就把单例初始化完成,保证getInstance的时候,单例是已经存在的了,</span></p><p align="left"><span style="font-size:14px;">而懒汉比较懒,只有当调用getInstance的时候,才回去初始化这个单例。</span></p><p align="left"><span style="font-size:14px;">另外从以下两点再区分以下这两种方式:</span></p><p align="left"><span style="font-size:14px;"><br></span></p><p align="left"><span style="font-size:14px;">1、线程安全:</span></p><p align="left"><span style="font-size:14px;">饿汉式天生就是线程安全的,可以直接用于多线程而不会出现问题,</span></p><p align="left"><span style="font-size:14px;">懒汉式本身是非线程安全的,为了实现线程安全有几种写法,分别是上面的1、2、3,这三种实现在资源加载和性能方面有些区别。</span></p><p align="left"><br><br></p><p align="left"><span style="font-size:14px;">2、资源加载和性能:</span></p><p align="left"><span style="font-size:14px;">饿汉式在类创建的同时就实例化一个静态对象出来,不管之后会不会使用这个单例,都会占据一定的内存,但是相应的,在第一次调用时速度也会更快,因为其资源已经初始化完成,</span></p><p align="left"><span style="font-size:14px;">而懒汉式顾名思义,会延迟加载,在第一次使用该单例的时候才会实例化对象出来,第一次调用时要做初始化,如果要做的工作比较多,性能上会有些延迟,之后就和饿汉式一样了。</span></p><p align="left"><span style="font-size:14px;">至于1、2、3这三种实现又有些区别,</span></p><p align="left"><span style="font-size:14px;">第1种,在方法调用上加了同步,虽然线程安全了,但是每次都要同步,会影响性能,毕竟99%的情况下是不需要同步的,</span></p><p align="left"><span style="font-size: 14px;">第2种,在getInstance中做了两次null检查,确保了只有第一次调用单例的时候才会做同步,这样也是线程安全的,同时避免了每次都同步的性能损耗</span></p><p align="left"><span style="font-size: 14px;">第3种,<span style="font-family: 'Helvetica Neue'; font-size: 14px;">利用了classloader的机制来保证初始化instance时只有一个线程,所以也是线程安全的,同时没有性能损耗,所以一般我倾向于使用这一种。</span></span></p><p align="left"><br></p><p align="left"><span style="font-size:18px;">什么是线程安全?</span></p><p align="left"><span style="font-size:14px;">如果你的代码所在的进程中有多个线程在同时运行,而这些线程可能会同时运行这段代码。如果每次运行结果和单线程运行的结果是一样的,而且其他的变量的值也和预期的是一样的,就是线程安全的。</span></p><p align="left"><span style="font-size:14px;">或者说:一个类或者程序所提供的接口对于线程来说是原子操作,或者多个线程之间的切换不会导致该接口的执行结果存在二义性,也就是说我们不用考虑同步的问题,那就是线程安全的。</span></p><p align="left"><span style="font-size:14px;"><br></span></p><p align="left"><span style="font-size:18px;">应用</span></p><p align="left"><span style="font-size:14px;">以下是一个单例类使用的例子,以懒汉式为例,这里为了保证线程安全,使用了<span style="font-family: 'Helvetica Neue'; font-size: 14px;">双重检查锁定的方式</span>:</span></p><p align="left"></p><div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><b>[java]</b> <a href="#" class="ViewSource" title="view plain" οnclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" target="_blank">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" οnclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" target="_blank">copy</a><div style="position: absolute; left: 803px; top: 4512px; width: 27px; height: 15px; z-index: 99;"><embed id="ZeroClipboardMovie_7" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="27" height="15" name="ZeroClipboardMovie_7" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=7&width=27&height=15" wmode="transparent"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" οnclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" target="_blank">print</a></span><a href="#" class="About" title="?" οnclick="dp.sh.Toolbar.Command('About',this);return false;" target="_blank">?</a></div></div><ol start="1" class="dp-j"><li class="alt"><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> TestSingleton {  </span></span></li><li class=""><span>    String name = <span class="keyword">null</span><span>;  </span></span></li><li class="alt"><span>  </span></li><li class=""><span>        <span class="keyword">private</span><span> TestSingleton() {  </span></span></li><li class="alt"><span>    }  </span></li><li class=""><span>  </span></li><li class="alt"><span>    <span class="keyword">private</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">volatile</span><span> TestSingleton instance = </span><span class="keyword">null</span><span>;  </span></span></li><li class=""><span>  </span></li><li class="alt"><span>    <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> TestSingleton getInstance() {  </span></span></li><li class=""><span>           <span class="keyword">if</span><span> (instance == </span><span class="keyword">null</span><span>) {    </span></span></li><li class="alt"><span>             <span class="keyword">synchronized</span><span> (TestSingleton.</span><span class="keyword">class</span><span>) {    </span></span></li><li class=""><span>                <span class="keyword">if</span><span> (instance == </span><span class="keyword">null</span><span>) {    </span></span></li><li class="alt"><span>                   instance = <span class="keyword">new</span><span> TestSingleton();   </span></span></li><li class=""><span>                }    </span></li><li class="alt"><span>             }    </span></li><li class=""><span>           }   </span></li><li class="alt"><span>           <span class="keyword">return</span><span> instance;  </span></span></li><li class=""><span>    }  </span></li><li class="alt"><span>  </span></li><li class=""><span>    <span class="keyword">public</span><span> String getName() {  </span></span></li><li class="alt"><span>        <span class="keyword">return</span><span> name;  </span></span></li><li class=""><span>    }  </span></li><li class="alt"><span>  </span></li><li class=""><span>    <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> setName(String name) {  </span></span></li><li class="alt"><span>        <span class="keyword">this</span><span>.name = name;  </span></span></li><li class=""><span>    }  </span></li><li class="alt"><span>  </span></li><li class=""><span>    <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> printInfo() {  </span></span></li><li class="alt"><span>        System.out.println(<span class="string">"the name is "</span><span> + name);  </span></span></li><li class=""><span>    }  </span></li><li class="alt"><span>  </span></li><li class=""><span>}  </span></li></ol></div><pre code_snippet_id="294885" snippet_file_name="blog_20150801_10_131752" name="code" class="java" style="display: none;">public class TestSingleton {
	String name = null;

        private TestSingleton() {
	}

	private static volatile TestSingleton instance = null;

	public static TestSingleton getInstance() {
           if (instance == null) {  
             synchronized (TestSingleton.class) {  
                if (instance == null) {  
                   instance = new TestSingleton(); 
                }  
             }  
           } 
           return instance;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void printInfo() {
		System.out.println("the name is " + name);
	}

}</pre><p>可以看到里面加了volatile关键字来声明单例对象,既然synchronized已经起到了多线程下原子性、有序性、可见性的作用,为什么还要加volatile呢,原因已经在下面评论中提到,<span style="font-family:monospace;color:#333333;"><span style="font-size: 14px; line-height: 26px;"></span></span></p><p>还有疑问可参考http://www.iteye.com/topic/652440<br>和http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html<br></p><br><p></p><div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><b>[java]</b> <a href="#" class="ViewSource" title="view plain" οnclick="dp.sh.Toolbar.Command('ViewSource',this);return false;" target="_blank">view plain</a><span data-mod="popu_168"> <a href="#" class="CopyToClipboard" title="copy" οnclick="dp.sh.Toolbar.Command('CopyToClipboard',this);return false;" target="_blank">copy</a><div style="position: absolute; left: 803px; top: 5329px; width: 27px; height: 15px; z-index: 99;"><embed id="ZeroClipboardMovie_8" src="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="27" height="15" name="ZeroClipboardMovie_8" align="middle" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=8&width=27&height=15" wmode="transparent"></div></span><span data-mod="popu_169"> <a href="#" class="PrintSource" title="print" οnclick="dp.sh.Toolbar.Command('PrintSource',this);return false;" target="_blank">print</a></span><a href="#" class="About" title="?" οnclick="dp.sh.Toolbar.Command('About',this);return false;" target="_blank">?</a></div></div><ol start="1" class="dp-j"><li class="alt"><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> TMain {  </span></span></li><li class=""><span>    <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">void</span><span> main(String[] args){  </span></span></li><li class="alt"><span>        TestStream ts1 = TestSingleton.getInstance();  </span></li><li class=""><span>        ts1.setName(<span class="string">"jason"</span><span>);  </span></span></li><li class="alt"><span>        TestStream ts2 = TestSingleton.getInstance();  </span></li><li class=""><span>        ts2.setName(<span class="string">"0539"</span><span>);  </span></span></li><li class="alt"><span>          </span></li><li class=""><span>        ts1.printInfo();  </span></li><li class="alt"><span>        ts2.printInfo();  </span></li><li class=""><span>          </span></li><li class="alt"><span>        <span class="keyword">if</span><span>(ts1 == ts2){  </span></span></li><li class=""><span>            System.out.println(<span class="string">"创建的是同一个实例"</span><span>);  </span></span></li><li class="alt"><span>        }<span class="keyword">else</span><span>{  </span></span></li><li class=""><span>            System.out.println(<span class="string">"创建的不是同一个实例"</span><span>);  </span></span></li><li class="alt"><span>        }  </span></li><li class=""><span>    }  </span></li><li class="alt"><span>}  </span></li></ol></div><pre code_snippet_id="294885" snippet_file_name="blog_20140416_3_6781930" class="java" name="code" style="display: none;">public class TMain {
	public static void main(String[] args){
		TestStream ts1 = TestSingleton.getInstance();
		ts1.setName("jason");
		TestStream ts2 = TestSingleton.getInstance();
		ts2.setName("0539");
		
		ts1.printInfo();
		ts2.printInfo();
		
		if(ts1 == ts2){
			System.out.println("创建的是同一个实例");
		}else{
			System.out.println("创建的不是同一个实例");
		}
	}
}
</pre><p align="left"><span style="font-size:14px;"> 运行结果:</span></p><p style=""><img src="https://img-blog.csdn.net/20140416064753093?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvamFzb24wNTM5/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt=""><br style="padding-bottom:0px; margin:0px; padding-left:0px; padding-right:0px; padding-top:0px"></p><p style=""><span style="font-size:14px;">结论:由结果可以得知单例模式为一个面向对象的应用程序提供了对象惟一的访问点,不管它实现何种功能,整个应用程序都会同享一个实例对象。</span></p><p style=""><span style="font-size:14px;">对于单例模式的几种实现方式,知道饿汉式和懒汉式的区别,线程安全,资源加载的时机,还有懒汉式为了实现线程安全的3种方式的细微差别。</span></p><p style=""><br></p><p style=""><span style="font-size:14px;"><br></span></p><p align="left"><span style="color: rgb(54, 46, 43); font-family: Arial; font-size: 14px; line-height: 26px;">更多设计模式:</span><a target="_blank" href="http://blog.csdn.net/jason0539/article/details/44956775" style="color: rgb(106, 57, 6); text-decoration: none; font-family: Arial; font-size: 14px; line-height: 26px;">23种设计模式系列</a></p><p align="left"></p><p style="color: rgb(54, 46, 43); font-family: Arial; line-height: 26px; font-size: 14px;"><br></p><p style="color: rgb(54, 46, 43); font-family: Arial; line-height: 26px; font-size: 14px;">作者:jason0539</p><p style="color: rgb(54, 46, 43); font-family: Arial; line-height: 26px; font-size: 14px;">博客:<a target="_blank" href="http://blog.csdn.net/jason0539" style="color: rgb(106, 57, 6); text-decoration: none; font-family: 'WenQuanYi Micro Hei Mono', 'WenQuanYi Micro Hei', 'Microsoft Yahei Mono', 'Microsoft Yahei', sans-serif !important;">http://blog.csdn.net/jason0539</a>(转载请说明出处)</p><p></p><p><br></p><iframe width="100%" height="400" class="share_self" frameborder="0" scrolling="no" src="http://widget.weibo.com/weiboshow/index.php?language=&width=0&height=550&fansRow=2&ptype=1&speed=0&skin=5&isTitle=1&noborder=1&isWeibo=1&isFans=1&uid=2553717707&verifier=3201fa5c&dpc=1"></iframe>   
</div>




<!-- Baidu Button BEGIN -->




<div class="bdsharebuttonbox tracking-ad bdshare-button-style0-16" style="float: right;" data-mod="popu_172" data-bd-bind="1504158075637">
<a href="#" class="bds_more" data-cmd="more" style="background-position:0 0 !important; background-image: url(http://bdimg.share.baidu.com/static/api/img/share/icons_0_16.png?v=d754dcc0.png) !important" target="_blank"></a>
<a href="#" class="bds_qzone" data-cmd="qzone" title="分享到QQ空间" style="background-position:0 -52px !important" target="_blank"></a>
<a href="#" class="bds_tsina" data-cmd="tsina" title="分享到新浪微博" style="background-position:0 -104px !important" target="_blank"></a>
<a href="#" class="bds_tqq" data-cmd="tqq" title="分享到腾讯微博" style="background-position:0 -260px !important" target="_blank"></a>
<a href="#" class="bds_renren" data-cmd="renren" title="分享到人人网" style="background-position:0 -208px !important" target="_blank"></a>
<a href="#" class="bds_weixin" data-cmd="weixin" title="分享到微信" style="background-position:0 -1612px !important" target="_blank"></a>
</div>
<script>window._bd_share_config = { "common": { "bdSnsKey": {}, "bdText": "", "bdMini": "1", "bdMiniList": false, "bdPic": "", "bdStyle": "0", "bdSize": "16" }, "share": {} }; with (document) 0[(getElementsByTagName('head')[0] || body).appendChild(createElement('script')).src = 'http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion=' + ~(-new Date() / 36e5)];</script>
<!-- Baidu Button END -->

   

<!--172.16.140.13-->

<!-- Baidu Button BEGIN -->
<script type="text/javascript" id="bdshare_js" data="type=tools&uid=1536434" src="http://bdimg.share.baidu.com/static/js/bds_s_v2.js?cdnversion=417822"></script>

<script type="text/javascript">
    document.getElementById("bdshell_js").src = "http://bdimg.share.baidu.com/static/js/shell_v2.js?cdnversion=" + Math.ceil(new Date()/3600000)
</script>
<!-- Baidu Button END -->



 


        <div id="digg" articleid="23297037">
            <dl id="btnDigg" class="digg digg_disable" οnclick="btndigga();">
               
                 <dt>顶</dt>
                <dd>205</dd>
            </dl>
           
              
            <dl id="btnBury" class="digg digg_disable" οnclick="btnburya();">
              
                  <dt>踩</dt>
                <dd>8</dd>               
            </dl>
            
        </div>
     <div class="tracking-ad" data-mod="popu_222"><a href="javascript:void(0);" target="_blank"> </a>   </div>
    <div class="tracking-ad" data-mod="popu_223"> <a href="javascript:void(0);" target="_blank"> </a></div>
    <script type="text/javascript">
        function btndigga() {
            $(".tracking-ad[data-mod='popu_222'] a").click();
        }
        function btnburya() {
            $(".tracking-ad[data-mod='popu_223'] a").click();
        }
            </script>

   <ul class="article_next_prev">
                <li class="prev_article"><span οnclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_shangyipian']);location.href='http://blog.csdn.net/jason0539/article/details/23733309';">上一篇</span><a href="http://blog.csdn.net/jason0539/article/details/23733309" οnclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_shangyipian'])">android之短信发不出去,短信空指针,smsManager.sendTextMessage报空指针异常</a></li>
                <li class="next_article"><span οnclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_xiayipian']);location.href='http://blog.csdn.net/jason0539/article/details/24010633';">下一篇</span><a href="http://blog.csdn.net/jason0539/article/details/24010633" οnclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_xiayipian'])">windows平台下载android源码</a></li>
    </ul>

    <div style="clear:both; height:10px;"></div>


            <div class="similar_article">
                    <h4></h4>
                    <div class="similar_c" style="margin:20px 0px 0px 0px">
                        <div class="similar_c_t">
                            相关文章推荐
                        </div>
                   
                        <div class="similar_wrap tracking-ad" data-mod="popu_36" style="max-height:250px">                       
                            <ul class="similar_list fl">    
                                   <li>
                                       <em>•</em>
                                       <a href="http://blog.csdn.net/cgt_cgt/article/details/76643096" title="java设计模式之单例模式" strategy="BlogCommendFromCsdn_0" target="_blank">java设计模式之单例模式</a>
                                   </li>
                                   <li>
                                       <em>•</em>
                                       <a href="http://edu.csdn.net/huiyiCourse/detail/501?utm_source=blog7" title="【直播】大中型UGC信息网站SEO分享--乔向阳" strategy="undefined" target="_blank">【直播】大中型UGC信息网站SEO分享--乔向阳</a>
                                   </li>
                                   <li>
                                       <em>•</em>
                                       <a href="http://download.csdn.net/download/boy4522/1039902" title="JAVA设计模式中的单例模式" strategy="BlogCommendFromCsdn_1" target="_blank">JAVA设计模式中的单例模式</a>
                                   </li>
                                   <li>
                                       <em>•</em>
                                       <a href="http://edu.csdn.net/huiyiCourse/series_detail/60?utm_source=blog7" title="【直播】打通Linux脉络 进程、线程和调度--宋宝华" strategy="undefined" target="_blank">【直播】打通Linux脉络 进程、线程和调度--宋宝华</a>
                                   </li>
                                   <li>
                                       <em>•</em>
                                       <a href="http://blog.csdn.net/u010983881/article/details/52470499" title="【Java】设计模式:深入理解单例模式" strategy="BlogCommendFromCsdn_2" target="_blank">【Java】设计模式:深入理解单例模式</a>
                                   </li>
                                   <li>
                                       <em>•</em>
                                       <a href="http://edu.csdn.net/huiyiCourse/detail/503?utm_source=blog7" title="【直播】Java最佳学习路线指导--肖海鹏" strategy="undefined" target="_blank">【直播】Java最佳学习路线指导--肖海鹏</a>
                                   </li>
                                   <li>
                                       <em>•</em>
                                       <a href="http://download.csdn.net/download/bihaichentian/2399185" title="Java单例模式应用研究.pdf" strategy="BlogCommendFromCsdn_3" target="_blank">Java单例模式应用研究.pdf</a>
                                   </li>
                                   <li>
                                       <em>•</em>
                                       <a href="http://edu.csdn.net/combo/detail/539?utm_source=blog7" title="【套餐】C++音视频实战技术套餐--夏曹俊" strategy="undefined" target="_blank">【套餐】C++音视频实战技术套餐--夏曹俊</a>
                                   </li>
                            </ul>
                              <ul class="similar_list fr">      
                                   <li>
                                       <em>•</em>
                                       <a href="http://download.csdn.net/download/lgywsdy/9748795" title="设计模式--单例模式java例子" strategy="BlogCommendFromCsdn_4" target="_blank">设计模式--单例模式java例子</a>
                                   </li>
                                   <li>
                                       <em>•</em>
                                       <a href="http://edu.csdn.net/combo/detail/544?utm_source=blog7" title="【套餐】0基础拿下HTML5和CSS3--李仁密" strategy="undefined" target="_blank">【套餐】0基础拿下HTML5和CSS3--李仁密</a>
                                   </li>
                                   <li>
                                       <em>•</em>
                                       <a href="http://download.csdn.net/download/beau_lily/9460204" title="修炼JAVA开发技术:在架构中体验设计模式和算法之美" strategy="BlogCommendFromCsdn_5" target="_blank">修炼JAVA开发技术:在架构中体验设计模式和算法之美</a>
                                   </li>
                                   <li>
                                       <em>•</em>
                                       <a href="http://edu.csdn.net/combo/detail/532?utm_source=blog7" title="【套餐】机器学习之数学基础系列--AI100" strategy="undefined" target="_blank">【套餐】机器学习之数学基础系列--AI100</a>
                                   </li>
                                   <li>
                                       <em>•</em>
                                       <a href="http://blog.csdn.net/smartbetter/article/details/68953251" title="Java 设计模式情景分析 ——单例模式" strategy="BlogCommendFromCsdn_6" target="_blank">Java 设计模式情景分析 ——单例模式</a>
                                   </li>
                                   <li>
                                       <em>•</em>
                                       <a href="http://download.csdn.net/download/wqh1011/3070747" title="java单例模式完全剖析" strategy="BlogCommendFromCsdn_7" target="_blank">java单例模式完全剖析</a>
                                   </li>
                                   <li>
                                       <em>•</em>
                                       <a href="http://blog.csdn.net/xiezhongtian/article/details/77676380" title="设计模式----单例模式(java)" strategy="BlogCommendFromCsdn_8" target="_blank">设计模式----单例模式(java)</a>
                                   </li>
                                   <li>
                                       <em>•</em>
                                       <a href="http://download.csdn.net/download/axi295309066/9806014" title="Java和Android源码设计模式 版本2" strategy="BlogCommendFromCsdn_9" target="_blank">Java和Android源码设计模式 版本2</a>
                                   </li>
                            </ul>
                        </div>
                    </div>
                </div>   
      
</div>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值