zoukankan      html  css  js  c++  java
  • [设计模式] 5 单例模式 singleton

    转处 http://blog.csdn.net/wuzhekai1985

    软件领域中的设计模式为开发人员提供了一种使用专家设计经验的有效途径。设计模式中运用了面向对象编程语言的重要特性:封装、继承、多态,真正领悟设计模式的精髓是可能一个漫长的过程,需要大量实践经验的积累。最近看设计模式的书,对于每个模式,用C++写了个小例子,加深一下理解。主要参考《大话设计模式》和《设计模式:可复用面向对象软件的基础》(DP)两本书。本文介绍单例模式的实现。

          单例的一般实现比较简单,下面是代码和UML图。由于构造函数是私有的,因此无法通过构造函数实例化,唯一的方法就是通过调用静态函数GetInstance。

          UML图:

          代码:

    #include <iostream>
    using namespace std;
    
    
    class Singleton  
    {
        public:
            static Singleton* GetInstance();
        private:
            Singleton() {}
            static Singleton *singleton;
    };
    Singleton* Singleton::singleton = NULL;
    Singleton* Singleton::GetInstance()
    {
        if(singleton == NULL)
            singleton = new Singleton();
        return singleton;
    }
    
    int main()
    {
        Singleton* s = Singleton::GetInstance();
    }

    这里只有一个类,如何实现Singleton类的子类呢?也就说Singleton有很多子类,在一种应用中,只选择其中的一个。最容易就是在GetInstance函数中做判断,比如可以传递一个字符串,根据字符串的内容创建相应的子类实例。这也是DP书上的一种解法,书上给的代码不全。这里重新实现了一下,发现不是想象中的那么简单,最后实现的版本看上去很怪异。在VS2008下测试通过。

    #include <iostream>
    #include <string.h>
    using namespace std;
    
    class Singleton  
    {
        public:
            static Singleton* GetInstance(const char* name);
            virtual void Show() {cout << "Singleton" << endl;}
        protected: 
            Singleton() {}// must be protectd, otherwise sub-class can't call father's ctor
        private:
            static Singleton *singleton; 
    };
    
    class SingletonA: public Singleton
    {
        friend class Singleton;//must be friend, otherwise father-class can't access sub-class's ctor
        public:
        void Show() { cout<<"SingletonA"<<endl; }
        private:   
        SingletonA() {} //must be private, let outbound can't new a instance of SingltonA
    };
    
    class SingletonB: public Singleton
    {
        friend class Singleton; //must be friend, otherwise father-class can't access sub-class's ctor
        public:
        void Show(){ cout<<"SingletonB"<<endl; }
        private: 
        SingletonB() {} //must be private, let outbound can't new a instance of SingltonA
    };
    
    Singleton* Singleton::singleton = NULL;
    Singleton* Singleton::GetInstance(const char* name)
    {
        if(singleton == NULL)
        {
            if(strcmp(name, "SingletonA") == 0)
                singleton = new SingletonA();
            else if(strcmp(name,"SingletonB") == 0)
                singleton = new SingletonB();
            else
                singleton = new Singleton();
        }
        return singleton;
    }
    
    
    
    int main()
    {
        Singleton *st = Singleton::GetInstance("SingletonA");
        st->Show();
        return 0;
    }

    上面代码有一个地方很诡异,父类为子类的友元,如果不是友元,函数GetInstance会报错,意思就是无法调用SingletonA和SIngletonB的构造函数。父类中调用子类的构造函数,我还是第一次碰到。当然了把SingletonA和SIngletonB的属性设为public,GetInstance函数就不会报错了,但是这样外界就可以定义这些类的对象,违反了单例模式。

           看似奇怪,其实也容易解释。在父类中构建子类的对象,相当于是外界调用子类的构造函数,因此当子类构造函数的属性为私有或保护时,父类无法访问。为共有时,外界就可以访问子类的构造函数了,此时父类当然也能访问了。只不过为了保证单例模式,所以子类的构造函数不能为共有,但是又希望在父类中构造子类的对象,即需要调用子类的构造函数,这里没有办法才出此下策:将父类声明为子类的友元类。

          

  • 相关阅读:
    ASP.NET Forums 2.0 修改集锦(一)
    在自己的应用程序中显示Windows关于对话框
    ASP.NET Forums 2.0 本地化修改(四)
    Flash对双字节的支持问题
    DotNetNuke 2.1.2安装指南
    ASP.NET Forums 2.0 本地化修改(五) 增加页面Meta标记的keywords和description
    ASP.NET Forums 2.0 本地化修改(三)
    JavaScript利用正则表达式自己写数字判断函数
    hash表基础知识(转载)
    求子数组的最大和
  • 原文地址:https://www.cnblogs.com/diegodu/p/4432297.html
Copyright © 2011-2022 走看看