首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Linux频道
中国IT教育
Google
首页 资讯动态 认证考试 新手入门 核心技术 高级技术 J2EE J2ME Java&XML 开源技术 其他技术 RSS订阅 论坛 专题
您现在的位置: 中国IT实验室 >> Java >> 新手入门 >> 基础入门 >> 正文

单体模式 Singleton

    一,什么是单体模式

            对象只要利用自己的属性完成了自己的任务,那该对象就是承担了责任。除了维持了自身的一致性,该对象无需承担其他任何责任。     如果该对象还承担着其他责任,而其他对象又依赖于该特定对象所承担的责任,我们就需要得到该特定对象。

            将类的责任集中到唯一的单体对象中,确保该类只有一个实例,并且为该类提供一个全局访问点。这就是单体模式的目的。

            单体模式的难点不在于单体模式的实现,而在于在系统中任何识别单体和保证单体的唯一性。

    二,单体模式的实现

            1,提供唯一的私有构造器,避免多个单体(Singleton)对象被创建,这也意味着该单体类不能有子类,那声明你的单例类为final是一个好主意,这样意图明确,并且让编译器去使用一些性能优化选项。  如果有子类的话使用protected,protected的构造方法可以被其子类以及在同一个包中的其它类调用。私有构造器可以防止客户程序员通过除由我们提供的方法之外的任意方式来创建一个实例,如果不把构造器声明为private或protected,      编译器会自动的创建一个public的构造函数。

            2,使用静态域(static field)来维护实例。

                     将单体对象作为单体类的一个静态域实例化。 使用保存唯一实例的static变量,其类型就是单例类型本身。需要的话使用final,使其不能够被重载。

                  例如:private static Rutime currentRuntime = new Runtime();

            3,使用静态方法(Static Method)来监视实例的创建。

                     a,加载时实例化

                      例如:

    view plaincopy to clipboardprint?
    public class Singleton {

 

                        private static final Singleton  Singleton _instance = new Singleton();

 

                        private Singleton() {

 

                        }

 

                        public static synchronized Singleton getInstance() {

                                return Singleton _instance;

                        }

                    }

    public class Singleton {

 

         private static final Singleton  Singleton _instance = new Singleton();

 

         private Singleton() {

 

            }

 

         public static synchronized Singleton getInstance() {

              return Singleton _instance;

           }

        }

                    b,使用时实例化(惰性初始化):这样做可以在运行时收集需要的信息来实例化单体对象,确保实例只有在需要时才被建立出来。

                     例如:

    view plaincopy to clipboardprint?
    public class Singleton {

 

                        private static final Singleton  Singleton _instance = null;

 

                        private Singleton() {

                            //使用运行时收集到的需要的信息,进行属性的初始化等操作。

                        }

 

                        public static synchronized Singleton getInstance() {

                                if (Singleton _instance == null){

 

                                Singleton _instance = new Singleton();

 

                            }

                                return Singleton _instance;

                        }

 

                    }

    public class Singleton {

 

         private static final Singleton  Singleton _instance = null;

 

         private Singleton() {

          //使用运行时收集到的需要的信息,进行属性的初始化等操作。

            }

 

         public static synchronized Singleton getInstance() {

              if (Singleton _instance == null){

 

           Singleton _instance = new Singleton();

 

          }

              return Singleton _instance;

           }

 

        }          4,单体对象的成员变量(属性):即单体对象的状态   通过单例对象的初始化来实现成员变量的初始化。   通过方法对单体对象的成员变量进行更新操作。

                 例如:

    view plaincopy to clipboardprint?
    public class Singleton {

 

                        private static final Singleton  Singleton _instance = null;

                        private Vector properties = null;

 

                        protected Singleton() {

                                //使用运行时收集到的需要的信息,进行属性的初始化等操作。

                        }

 

                        private static synchronized void syncInit() {

                            if (Singleton _instance == null) {

                                Singleton _instance = new Singleton();

                            }

                        }

 

                        public static Singleton getInstance() {

                                if (Singleton _instance == null){

                                    syncInit();

                            }

                                return Singleton _instance;

                        }

 

                        public synchronized void updateProperties() {

                            // 更新属性的操作。

                        }

 

                        public Vector getProperties() {

                                return properties;

                        }

                    }

 

    public class Singleton {

 

         private static final Singleton  Singleton _instance = null;

         private Vector properties = null;

 

         protected Singleton() {

               //使用运行时收集到的需要的信息,进行属性的初始化等操作。

            }

 

         private static synchronized void syncInit() {

          if (Singleton _instance == null) {

           Singleton _instance = new Singleton();

          }

         }

 

         public static Singleton getInstance() {

              if (Singleton _instance == null){

                 syncInit();

          }

              return Singleton _instance;

           }

 

 

[1] [2] [3] [4] [5] 下一页

【责编:landy】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·关于Java框架技术专题
 ·XML全攻略技术专题
 ·JAVA开源技术介绍专题
 ·Java嵌入式开发之J2ME技术专题
 ·超前体验 Oracle 11g的5个新特性…
 ·揭密使用VB.NET的五个实用技巧
 ·Oracle和SQL Server常用函数对比专题…
 ·展现C#世界 C#程序设计专题…
 ·Java入门 Tomcat的配置技巧精华专题…
 ·Oracle RMAN物理备份技术详解…
 今日更新
 社区讨论
 博客论点
 频道精选
 Java 频道导航