Design Mode C ++ Implementation (4) -- Singleton Mode

Source: Internet
Author: User

The design patterns in the software field provide developers with an effective way to use expert design experience. Objects are used in the design model.Programming LanguageImportant features: encapsulation, inheritance, polymorphism, true understanding of the essence of the design model is a long process that requires a lot of practical experience. I recently read a book on design patterns. I wrote a small example in C ++ for each pattern to help me better understand it. Refer to "big talk Design Patterns" and "design patterns: the basis for reusable object-oriented software" (DP. This section describes how to implement the singleton mode.

The implementation of a singleton is relatively simple. below isCodeAnd UML diagrams. The constructor is private, so it cannot be instantiated through the constructor. the only method is to call the static function getinstance.

UML diagram:

Code:

 
// Singleton. hclass Singleton {public: static Singleton * getinstance (); Private: Singleton () {} static Singleton * Singleton ;}; // Singleton. cppsingleton * singleton: Singleton = NULL; Singleton * singleton: getinstance () {If (Singleton = NULL) Singleton = new Singleton (); Return Singleton ;}

There is only one class. How can we implement the subclass of the singleton class? In other words, Singleton has many sub-classes. In an application, select only one of them. The easiest way is to make judgments in the getinstance function. For example, you can pass a string and create a subclass instance based on the content of the string. This is also a solution in the DP book. The Code provided in the book is incomplete. Here we implement it again and find that it is not as simple as we thought, and the final implementation version looks weird. The test passed in vs2008.

 // Singleton. h # pragma once # include 
  
    using namespace STD; Class Singleton {public: static Singleton * getinstance (const char * Name); Virtual void show () {} protected: // It must be protected. If it is a private attribute, the subclass cannot access the constructor Singleton () {} PRIVATE: static Singleton * Singleton; // the pointer of the unique instance }; // Singleton. CPP # include "Singleton. H "# include" singletona. H "# include" singletonb. H "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 ;}
  
// Singletona. h # pragma once # include "Singleton. H "class singletona: Public Singleton {friend class Singleton; // it must be a friend class; otherwise, the parent class cannot access the subclass's constructor public: void show () {cout <"singletona" <Endl;} PRIVATE: // is the protection attribute, so that the external world cannot instantiate singletona () {}}through the constructor; // singletonb. h # pragma once # include "Singleton. H "class singletonb: Public Singleton {friend class Singleton; // it must be a friend class; otherwise, the parent class cannot access the subclass's constructor public: void show () {cout <"singletonb" <Endl;} PRIVATE: // protects attributes, so that the external world cannot instantiate singletonb () {}} through the constructor (){}};
 
# Include "Singleton. H" int main () {Singleton * ST = singleton: getinstance ("singletona"); ST-> show (); Return 0 ;}

The code above is very strange. If the parent class is a child-class friend, if it is not a friend, the getinstance function will report an error, meaning that the constructor of singletona and singletonb cannot be called. In the parent class, I used to call the subclass constructor for the first time. Of course, if the attributes of singletona and singletonb are set to public, the getinstance function will not report an error. However, in this way, objects of these classes can be defined, which violates the singleton mode.
It seems strange, but it is easy to explain. Constructing a subclass object in the parent class is equivalent to calling the subclass constructor. Therefore, when the attributes of the subclass constructor are private or protected, the parent class cannot be accessed. In common, the outside world can access the constructor of the subclass. In this case, the parent class can also access the constructor. However, to ensure the singleton mode, the constructor of the subclass cannot be the same, but you want to construct the object of the subclass in the parent class, that is, you need to call the constructor of the subclass, there is no way to come up with this policy: declare the parent class as a Child class's friend Meta class.

I have a blogArticleCopyright, reprint please indicate the source of http://blog.csdn.net/wuzhekai1985

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.