Chinaunix首页 | 论坛 | 博客
  • 博客访问: 442315
  • 博文数量: 1496
  • 博客积分: 79800
  • 博客等级: 大将
  • 技术积分: 9940
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-09 13:22
文章分类

全部博文(1496)

文章存档

2011年(1)

2008年(1495)

我的朋友

分类:

2008-09-09 17:23:52

    一,什么是单体模式

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

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

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

    二,单体模式的实现

            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]     

【责编:landy】

--------------------next---------------------

阅读(124) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~