标题:singleton单例模式


设计模式的说法源自《设计模式》一书,原名《Design Patterns: Elements of Reusable Object-Oriented Software》。1995年出版,出版社:Addison Wesly Longman.Inc。该书提出了23种基本设计模式,第一次将设计模式提升到理论高度,并将之规范化。设计模式在名企面试中出现的几率还是很大的,面试题可简单可难。简单的可以问你有多少种模式,每种模式的原理是什么,你自己在开发中用过几种?复杂点让你实现其中一个模式,而单例模式是被考查最多的。

 

单例模式是一种常用的软件设计模式,在各大名企面试题中出现的频率很高。简单的会问你设计模式用过哪几种,它们的应用场景是什么,复杂的面试题就是让你写出一种设计模式的实现。

 

单例模式最初的定义出现于《设计模式》(艾迪生维斯理, 1994):“保证一个类仅有一个实例,并提供一个访问它的全局访问点。”Java中单例模式定义:“一个类有且仅有一个实例,并且自行实例化向整个系统提供。”

 

单例模式的要点有三个;一是某个类只能有一个实例;二是它必须自行创建这个实例;三是它必须自行向整个系统提供这个实例。从具体实现角度来说,就是以下三点:一是单例模式的类只提供私有的构造函数,二是类定义中含有一个该类的静态私有对象,三是该类提供了一个静态的公有的函数用于创建或获取它本身的静态私有对象。

 

C++:

class CSingleton

{

private:

    CSingleton() //构造函数是私有的,保证不能在栈上创建对象

    {

    }

  static CSingleton *m_pInstance;

public:

    static CSingleton * GetInstance()

    {

      

        if(m_pInstance == NULL) //判断是否第一次调用

            m_pInstance = new CSingleton();

        return m_pInstance;

    }

};

CSingleton *CSingleton::m_pInstance=NULL; 

 

思考题:C++实现中如何保证单例模式的多线程安全?

JAVA:

懒汉,线程不安全

public class Singleton {

    private static Singleton instance;

    private Singleton (){}

 

    public static Singleton getInstance() {

       if (instance == null) {

           instance = new Singleton();

       }

       return instance;

    }

}

 

懒汉,线程安全

public class Singleton {

    private static Singleton instance;

    private Singleton (){}

    public static synchronized Singleton getInstance() {

       if (instance == null) {

           instance = new Singleton();

       }

       return instance;

    }

}

 

双重校验锁

public class Singleton {

    private volatile static Singleton singleton;

    private Singleton (){}

    public static Singleton getSingleton() {

       if (singleton == null) {

           synchronized (Singleton.class) {

              if (singleton == null) {

                  singleton = new Singleton();

              }

           }

       }

       return singleton;

    }

}

 

思考题答案:

//双重校验

class CSingleton

{

private:

    CSingleton() //构造函数是私有的,保证不能在栈上创建对象

    {

    }

  static CSingleton *m_pInstance;

public:

    static CSingleton * GetInstance()

    {

      

        if(m_pInstance == NULL) //判断是否第一次调用

         {

            lock();

            if(m_pInstance==NULL)

                m_pInstance = new CSingleton();

            unlock();

         }

        return m_pInstance;

    }

};

CSingleton *CSingleton::m_pInstance=NULL; 

 

//懒汉模式

class CSingleton

{

private:

    CSingleton() //构造函数是私有的,保证不能在栈上创建对象

    {

    }

  static CSingleton *m_pInstance;

public:

    static CSingleton * GetInstance()

    {

        //lock()

        if(m_pInstance == NULL) //判断是否第一次调用

            m_pInstance = new CSingleton();

        //unlcok()

        return m_pInstance;

    }

};

CSingleton *CSingleton::m_pInstance=NULL; 

 

有关单例模式的更多方法参考:链接1


看文字不过瘾?点击我,进入周哥教IT视频教学
麦洛科菲长期致力于IT安全技术的推广与普及,我们更专业!我们的学员已经广泛就职于BAT360等各大IT互联网公司。详情请参考我们的 业界反馈 《周哥教IT.C语言深学活用》视频

我们的微信公众号,敬请关注