A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 经济 中级黑马   /  2015-6-2 23:39  /  326 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

一、Java单例设计模式的分类
                  在Java中单例设计模式,可以分为懒汉式和饿汉式,代码实现分别如下:

                               /*饿汉式代码*/

                              public class Sington {

                                       //将构造函数私有,避免外部创建对象
                                       privateSington() {
                                        }

                                      //创建实例化对象
                                      private static Sington sington = new Sington();

                                      //返回实例对象
                                     public static Sington getSington() {
                                      return sington;
                                      }
                               }

                              /*懒汉式代码*/

                           public class Sington {

                                  //将构造函数私有,避免外部创建对象
                                 private Sington() {
                                   }

                                 //创建实例化对象
                                private static Sington sington = null;

                                //返回实例对象
                                public synchronized static Sington getSington() {
                                 if (sington != null) {
                                       return sington;
                                     } else {
                                       sington = new Sington();
                                       return sington;
                                       }
                                     }

                               }
  二、结论及建议
                   (1)上面两种代码都能完成相同的功能,不同的是,懒汉式需要用synchronized来保证线程的安全性,和第一种比较来说,需要消耗更多的资源

                   (2)通过查看Runtime类源码发现,其设计为单例模式,并且用的是饿汉式,源码如下:
                            public class Runtime {
                             private static Runtime currentRuntime = new Runtime();
                             /**
                              * Returns the runtime object associated with the current Java application.
                             * Most of the methods of class <code>Runtime</code> are instance
                             * methods and must be invoked with respect to the current runtime object.
                             *
                             * @return  the <code>Runtime</code> object associated with the current
                             *          Java application.
                             */
                            public static Runtime getRuntime() {
                            return currentRuntime;
                             }
                            /** Don't let anyone else instantiate this class */
                             private Runtime() {}
               (3)通过以上分析,可知,饿汉式优于懒汉式,所以在开发过程中,我们建议使用饿汉式。



5 个回复

倒序浏览
更多信息,请大家,参看我的博客 http://blog.csdn.net/u012193715
回复 使用道具 举报
http://blog.csdn.net/u012193715
回复 使用道具 举报
来看看,学习学习讲解的!
回复 使用道具 举报
不错不错
回复 使用道具 举报
来看看学习的
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马