I still join the following instructions below:
// ******************************************************** **************** / / 3.5 Singleton - Object creation mode // 1. Intention / / Ensure that only one instance is only one instance, and provide an access to it Global access point. // 2. Motivation // For some classes, only one instance is very important. Although there are many hit // printers in the system, but only one print spooler, only // a file system and a window manager should be available. A digital filter can only have an A / D conversion ////er. A accounting system can only be dedicated to a company. // How can we guarantee that only one instance is only one instance and this instance is easy to be accessed? // A global variable allows an object to be accessed, but it does not prevent you from instantiating multiple objects. // A better way is that the class is responsible for saving its unique example. This class can guarantee that there is no other instance to be created (by intercepting a request for creating a new object), and it can // to provide an access to the instance. This is the Singleton mode. // 3. Applicability // You can use Singleton mode in the case below. When the class can only have one instance and the customer can access it from a well known access point. //. When this unique instance should be expandable by subclassization, and the customer should use an extension instance without changing // code. // 4. Structure // 5. Participants //. Singleton // - Define a getInstance action that allows customers to access its unique instances. GetInstance // is a class operation (ie a class method in SmallTalk and a static member function in C ). // - may be responsible for creating its own unique instance. // 6. Collaboration //. Customers can only access a Singleton instance through Singlet's GetInstance operation. // 7. Effect // Singleton mode There are many advantages: // 1) Controlled access to the unique instance because Singleton classes encapsulates its unique example, so it can // how to get rid of how to control the customer and when to access it. // 2) Reduce the SingleTon mode is an improvement to global variables. It avoids the global variable contamination names of those deposits // store the only instance. // 3) Allows the essential of the Singleton class to the operation and representation of the Singleton class, and use this extension class // to configure an application is easy. You can use the instance of the class you need to configure the application at the time of run //. // 4) Allow variable objective instances This mode makes you easy to change your ideas and allow multiple instances of // Singleton class. In addition, you can use the same way to control the number of // instances used by the application. Only the operation that allows access to the Singleton instance needs to be changed. // 5) Method for more flexible and other package single-piece functions than class operations is the use of class operations (ie, // static member functions in C or class methods in SmallTalk). However, both language technologies are difficult to change / vary to allow a class to have multiple instances. // Further, the static member function in C is not a virtual function, so the subclass cannot redefine them. // 8. Implement Class Singleton {Std :: auto_ptr
// Return this Singleton Class' Instance Pointer Static Singleton * instance () {if (! M_pinstance.get ()) {m_pinstance = std :: auto_ptr
How to use it? Don't try to derive your single subclass from this class, which is not appropriate. If you need multiple single subclasses, or use the following macro definition more appropriate:
#define define_nsingleton (CLS) / private: / static st: auto_ptr
Assume that you need to implement a single-piece yy, so writing:
Class Yy {Define_Singleton (YY); Public: // Your Interfaces Here ...}; in the CPP file, write: import_nsingleton (yy);
Use this statement when you need to introduce this class:
YY * pyy = yy :: instance ();
this is all. If you need to define other single classes, repeat the definition above, you can.
Said so much, what is the purpose?
Please read the moving part of the above. I will add these situations:
When you want to centrally manage all configurations you need to use, you can declare a class of CToolSoptions, which contains a collection of configuration properties. For this example, it is obviously an example. It is enough; when writing a plot program, consider drawing rectangles, circles, etc., each derived class is responsible for handling specific drawing actions and related UIs logic. These tools are typically selected when the module button is selected by the user. In this way, you should register all the primitive tools, so that the drawing programs can be used to use those primitives. Similarly, this manager responsible for managing registration information only needs an instance.
Take care of your surroundings, the single piece is really typically an example that cannot be typical, so that it can be seen everywhere. Various design patterns are the same in good design code. Want to write an expert-style elegant code, master mode is a necessary means.