1. Use a template to implement the singleton Mode
In the previous article, we used multiple methods to implement the singleton mode. Now we use the template method to implement it:
Singleton. h:
C ++ code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
# Ifndef _ singleton_h _ # DEFINE _ singleton_h _# Include <cstdlib> # Include <iostream> Using namespace STD; Template <typename T> Class Singleton { Public: Static T & getinstance () { Init (); Return * instance _; } PRIVATE: Static void Init () { If (instance _ = 0) { Instance _ = new T; Atexit (destroy); // call the registered function when the program ends. } } Static void destroy () { Delete instance _; } Singleton (const Singleton & other ); Singleton & operator = (const Singleton & other ); Singleton (); ~ Singleton (); Static T * instance _; }; Template <typename T> T * Singleton <t>: instance _ = 0; # Endif // _ singleton_h _ |
Main. cpp:
C ++ code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
# Include <iostream> Using namespace STD;# Include "Singleton. H" Class applicationimpl { Public: Applicationimpl () { Cout <"applicationimpl..." <Endl; } ~ Applicationimpl () { Cout <"~ Applicationimpl... "<Endl; } Void run () { Cout <"Run..." <Endl; } }; Typedef Singleton <applicationimpl> application; Int main (void) { Application: getinstance (). Run (); Application: getinstance (). Run (); Return 0; } |
Singleton is implemented as a template class, And applicationimpl class is packaged as a singleton mode class. We can see that the constructor and destructor are called only once. The program uses the axexit function to register the functions that need to be called when the program ends.
Ii. dynamically create objects using templates
In the previous article, the macro definition was used to dynamically create objects. Now, in dynbase. H, the macro definition is replaced with a template class, and other code remains unchanged:
C ++ code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
// Class register //{ // Public: // Register (const string & name, create_func func) //{ // Dynobjectfactory: Register (name, func ); //} //}; // // # Define register_class (class_name )\ // Class class_name # register {\ // Public :\ // Static void * newinstance ()\ //{\ // Return New class_name ;\ //}\ // Private :\ // Static register Reg _;\ //};\ // Register class_name # register: Reg _ (# class_name, class_name ## register: newinstance)
Template <typename T> Class delegatingclass { Public: Delegatingclass (const string & name) { Dynobjectfactory: Register (name, & (delegatingclass: newinstance )); }Static void * newinstance () { Return New T; } }; # Define register_class (class_name) delegatingclass <class_name> class ## class_name (# class_name) |
That is, the macro definition extension of register_class (class_name) constructs a template class instance object. Three macro definitions are called, namely, three template class instance objects, and the constructor is called.
Delegatingclass (const string & name), and then call register to complete registration. The subsequent process is the same as that of the previous program. The output is as follows:
Refer:
C ++ primer version 4
Valid tive C ++ 3rd
C ++ programming specifications