Singleton mode

zhaozj2021-02-16  80

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 class globalclass {public: globalclass (int v = 0) {value_ = v;} int getValue () {return value_;} void setValue (int V) {value_ = v;} private: int value_ ;}; // Initializing GlobalClass class global pointer GlobalClass * Globalobj = 0; void foo (void) {if (! Globalobj) Globalobj = new globalclass; globalobj-> setValue (1); cout << "foo: globalobj IS" << Globalobj-> getValue () << Endl;} void bar (void) {if (! Globalobj) Globalobj = new globalclass; globalobj-> setValue (2); cout << "bar: globaloba is" << "<< globalobj- > getValue () << Endl;} void main (void) {if (! globalobj) Globalobj = new globalclass; cout << "main: globalobj is" << globalobj-> getValue () Endl; foo (); BAR ();} // main: globalobj is 0 // foo: globalobj is 1 // bar: globalobj is 2

// 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 class GlobalClass {public: int getValue () {return value_;} void setValue (int v) {value_ = v;} static GlobalClass & inst () {static GlobalClass globalObj; return globalObj;} ~ GlobalClass ( ) {Cout << ": DTOR:" << Endl;} protected: globalclass (int v = 0) {cout <<: ctor: "; value_ = v;} private: int value _;}; void foo (void ) {Globalclass :: INST (). SetValue (1); cout << "foo: globalobj is" << globalclass :: inst (). GetValue () << Endl;} void bar (void) {globalclass :: instance () .SetValue (2); cout << "bar: globalobj IS" << Globalclass :: INST (). getValue () << Endl;} void main (void) {cout << "main: globalobj IS" <

转载请注明原文地址:https://www.9cbs.com/read-12717.html

New Post(0)