什么是单立模式,代码实现 

解决方案 »

  1.   

    单例模式
    属于创建型模式
    一个类只能有一个实例,并且自行实例化,必须自行向其他对象提供这个实例
    关键代码实现:
    public class DataCenter {
        //static variable
        private static DataCenter singleton = null;
        //private constructor
        private DataCenter () {
        }
        //static method, synchronized
        public synchronized static DataCenter getInstance() {
            if (singleton == null)
                singleton = new DataCenter ();        return singleton;
        }
    (  NS0-111 jn0-120  )
    注意事项:
    构造器不公开private
    getInstance方法的关键字synchronized static
    使用场合:
    任何只需要一个实例的地方
      

  2.   

    单例吧
    //NOTE: This is not thread safe!public class Singleton {
    private static Singleton uniqueInstance; // other useful instance variables here private int i = 0; private Singleton() {
    i++;
    } public static Singleton getInstance() {
    if (uniqueInstance == null) {
    uniqueInstance = new Singleton();
    }
    return uniqueInstance;
    } // other useful methods here public void print() {
    System.out.println(i);
    }
    }
      

  3.   

    //double-checked locking
    //Danger!  This implementation of Singleton not
    //guaranteed to work prior to Java 5public class Singleton {
    private volatile static Singleton uniqueInstance; private Singleton() {
    } public static Singleton getInstance() {
    if (uniqueInstance == null) {
    synchronized (Singleton.class) {
    if (uniqueInstance == null) {
    uniqueInstance = new Singleton();
    }
    }
    }
    return uniqueInstance;
    }
    }
      

  4.   

    //double-checked locking
    //Danger!  This implementation of Singleton not
    //guaranteed to work prior to Java 5double-check已经不推荐使用了
    用3楼的吧
      

  5.   

    http://blog.csdn.net/goldenfish1919/archive/2010/12/05/6057073.aspx
      

  6.   

    单例模式:
    设计模式之Singleton(单态)
    板桥里人 http://www.jdon.com 2002/05/07
    定义:Singleton模式主要作用是保证在Java应用程序中,一个类Class只有一个实例存在。在很多操作中,比如建立目录 数据库连接都需要这样的单线程操作。
    还有, singleton能够被状态化; 这样,多个单态类在一起就可以作为一个状态仓库一样向外提供服务,比如,你要论坛中的帖子计数器,每次浏览一次需要计数,单态类能否保持住这个计数,并且能synchronize的安全自动加1,如果你要把这个数字永久保存到数据库,你可以在不修改单态接口的情况下方便的做到。
    另外方面,Singleton也能够被无状态化。提供工具性质的功能,Singleton模式就为我们提供了这样实现的可能。使用Singleton的好处还在于可以节省内存,因为它限制了实例的个数,有利于Java垃圾回收(garbage collection)。我们常常看到工厂模式中类装入器(class loader)中也用Singleton模式实现的,因为被装入的类实际也属于资源。
    如何使用?一般Singleton模式通常有几种形式:
    public class Singleton {//懒汉式
      private Singleton(){}  //不能再其它类中new
      //在自己内部定义自己一个实例,是不是很奇怪?  //注意这是private 只供内部调用
      private static Singleton instance = new Singleton();
      //这里提供了一个供外部访问本class的静态方法,可以直接访问    public static Singleton getInstance() {    return instance;      } }
     
    第二种形式:
    public class Singleton {//饿汉式
      private Singleton(){}  //  (原文档中没有 ,自己加的)
        private static Singleton instance = null;  public static synchronized Singleton getInstance() {  //这个方法比上面有所改进,不用每次都进行生成对象,只是第一次       //使用时生成实例,提高了效率!  if (instance==null)    instance=new Singleton();  return instance;   }
    }
     使用Singleton.getInstance()可以访问单态类。
    上面第二中形式是lazy initialization,也就是说第一次调用时初始Singleton,以后就不用再生成了。
    注意到lazy initialization形式中的synchronized,这个synchronized很重要,如果没有synchronized,那么使用getInstance()是有可能得到多个Singleton实例。关于lazy initialization的Singleton有很多涉及double-checked locking (DCL)的讨论,有兴趣者进一步研究。
    一般认为第一种形式要更加安全些。
    使用Singleton注意事项:有时在某些情况下,使用Singleton并不能达到Singleton的目的,如有多个Singleton对象同时被不同的类装入器装载;在EJB这样的分布式系统中使用也要注意这种情况,因为EJB是跨服务器,跨JVM的。
    我们以SUN公司的宠物店源码(Pet Store 1.3.1)的ServiceLocator为例稍微分析一下:在Pet Store中ServiceLocator有两种,一个是EJB目录下;一个是WEB目录下,我们检查这两个ServiceLocator会发现内容差不多,都是提供EJB的查询定位服务,可是为什么要分开呢?仔细研究对这两种ServiceLocator才发现区别:在WEB中的ServiceLocator的采取Singleton模式,ServiceLocator属于资源定位,理所当然应该使用Singleton模式。但是在EJB中,Singleton模式已经失去作用,所以ServiceLocator才分成两种,一种面向WEB服务的,一种是面向EJB服务的。
    Singleton模式看起来简单,使用方法也很方便,但是真正用好,是非常不容易,需要对Java的类 线程 内存等概念有相当的了解。
    进一步深入可参考:
    Double-checked locking and the Singleton pattern
    When is a singleton not a singleton?
      

  7.   

    单例模式典型的特征就是构造函数的访问权限是private,在getInstance()中只有当number==0时才可以创建一个对象。
      

  8.   

     饿汉式单例
    public class EagerSingleton 

    private static final EagerSingleton m_instance = new EagerSingleton(); 
    /** 
    * 私有的默认构造子 
    */ 
    private EagerSingleton() { } 
    /** 
    * 静态工厂方法 
    */ 
    public static EagerSingleton getInstance() 
    {
    return m_instance; 
    }
    }
    懒汉式单例public class LazySingleton 

    private static LazySingleton 
    m_instance = null; 
    /** 
    * 私有的默认构造子,保证外界无法直接实例化 
    */ 
    private LazySingleton() { } 
    /** 
    * 静态工厂方法,返还此类的惟一实例 
    */ 
    synchronized public static LazySingleton getInstance() 

    if (m_instance == null) 
    {
    m_instance = new LazySingleton(); 

    return m_instance; 
    }
    }
      

  9.   


    需要重新objec类里面的clone方法,同样是为了防止外部实例化。
      

  10.   

    http://blog.csdn.net/sageparadise/article/details/63296161.实现形式一 
    /**
     * 单例模式(懒汉式)====>程序执行过程需要这个类的对象时在实例化该类的对象
     * 
     * @author sageparadise
     * 
     */
    public class Singleton { private static Singleton singleton; // 构造方法私有化,保证在类的外部无法实例化该类的对象
     private Singleton() {
     } public static Singleton getSingletonInstance() {
      if (singleton == null) {
       singleton = new Singleton();
      }
      return singleton;
     }}
    2.实现形式二
    /**
     * 单例模式 (饥饿式)====>类加载的时候就实例化该类的对象
     * 
     * @author sageparadise
     * 
     */
    public class Singleton {
     private static Singleton  singleton = new Singleton(); // 构造方法私有化,保证在类的外部无法实例化该类的对象
     private Singleton() {
     } public static Singleton getSingletonInstance() {
      return singleton;
     }}
      

  11.   

    单例模式:一个类在应用程序中只有一个实例,要做到这一点,必须不能让类的使用者轻易把类new出来,因为new Object() 一次就产生一个实例。所以类的构造方法必须是private的。接下来单例类就必须提供一个访问器(get方法)获得单例类的实例。下面有个例子:
    public class Singleton{    private static Singleton instance;
        
        static{
             instance = new Singleton()//在类加载时,就生成他的实例
        }    //防止类的使用者new Singleton()
        private Singleton (){}    //Singleton 类的访问器,可以通过这个方法获得类的实例
        public static Singleton getInstance(){
            return instance ;
        }
    }