Singleton
Chinese name
a piece.
To ensure that a class has only one example and provides a global access point.
Problem application needs and only one instance of an object. In addition, delay initialization and global access are required.
discuss
For a certain class, whether it is created, initialized, access or execution, make sure there is only one instance. Putting an example as a private static data member statement, providing a public static member function package all initialization code, providing access to an instance.
When a separate instance is required, the customer calls the access function (using the class name :: operation).
Singleton is considered when the following three criteria are satisfied:
The owner of the individual instance is difficult to assign reasonably. It is desirable to delay initialization. No global access is available.
Singleton mode can extend to support specific number of instances.
The "Static member function Acquisition" method does not support subcatenization of the Singleton class.
Deleting a Singleton class / object is not a problem.
Example US President is a Singleton. The US constitution specifies how to elect the president, limit the period of this position, define the order of order. Therefore, there is only one current president at any time. Regardless of the personal characteristics of the current president, the title of "US President" is the global access point of the in progress.
Other views Singleton cannot solve global variables, it just guarantees that only one example is only one example. As for global variables, the only advantage is to reduce the namespace.
Singleton can confirm that there are several examples than the global variable.
Solving the answer to global variables is not Singleton mode, but don't use it.
Experience Rule Abstract Factory, Builder and Prototype can use Singleton in the implementation.
FACADE is usually Singletons because only one Facade object is needed.
State objects are usually Singleton.
// Discussion: Singleton // // Discussion: In the first example, a global object requires delay to initialize (initialized as needed). // This requires all user detection and may allocate a pointer. // Singleton advocates that the object itself has a responsibility to create, maintain, its own unique instance, / / and provide a global access point. #include
// New design: "Globalobj" is now a private static member function of your own class. // Provide a global access point through the public static member function INST (). // delaying the initialization code is encapsulated in the inst () function. // GlobalClass constructors and destructive functions are protected, // Users cannot create more instances, nor can they destroy the Singleton instance. class GlobalClass {public: int getValue () {return value_;} void setValue (int v) {value_ = v;} static GlobalClass * inst () {if globalObj_ = new GlobalClass; return globalObj_;} protected (globalObj_!): GlobalClass (int V = 0) {value_ = v;} ~ globalclass () {} private: int value_; static globalclass * globalobj_;}; globalclass * globalclass :: globalobj_ = 0; void foo (void) {globalclass :: INST ) -> setValue (1); cout << "foo: globalobj is" << Globalclass :: INST () -> getValue () << Endl;} void bar (void) {globalclass :: inst () -> setValue (2); COUT << "bar: globalobj is" << Globalclass :: INST () -> getValue () << Endl;} void main (void) {cout << "main: globalobj is" << globalclass: : INST () -> getValue () << Endl; foo (); bar ();} // main: globalobj is 0 // foo: globalobj is 1 // bar: globalobj IS 2
// Discussion: Singleton Desctured Class GlobalClass; Class SingDest {Public: SingDest (GlobalClass * S = 0) {Sing_ = S;} ~ SingDest (); void setsing (globalclass * s) {sing_ = s;} private: globalclass * sing _;}; class GlobalClass {public: friend class SingDest; int getValue () {return value_;} void setValue (int v) {value_ = v;} static GlobalClass * inst () {if (globalObj_!) {globalObj_ = New globalclass; dest_.setsing (globalobj_);} Return Globalobj_;} private: GlobalClass (int V = 0) {cout <<: ctor: "; value_ = v;} ~ globalclass () {cout <<": Dtor : "<< endl;} int value_; static GlobalClass * globalObj_; static SingDest dest _;}; GlobalClass * GlobalClass :: globalObj_ = 0; SingDest GlobalClass :: dest_; SingDest :: ~ SingDest () {delete sing_;} void foo (void) {globalclass :: INST () -> setValue (1); cout << "foo: globalobj IS" << Globalclass :: INST () -> getValue () <<} void bar (void) { GlobalClass :: INST () -> setValue (2); cout << "bar: globalobj IS" << Globalclass :: IST () -> getValue () << endl;} void main (void) {cout << "main : Globalobj IS "<< Globalclass :: INST () -> getValue () << endl; foo (); bar ();} // main: globalobj is: ctor: 0 // foo: globaloba is 1 // bar : Globalobj IS 2 // Main: end //: Dtor:
// Objective: Scott Meyers method. // "Globalobj" is now a static member of the access party inst (). // By declaring the constructor's non-public forced single instance. // Because of a static variable instance, the destructor must be public. // inst () static method provides access. // C When the first request, the object is assigned; then C can automatically destroy it. #include