Objective: To ensure that there is only one instance object for this class in the program. Method: Use a static object. Important: 1. A class can have only one instance, 2, and it must create the instance itself, 3. It must provide this instance to the entire system on its own. Implementation method: 1. The singleton mode class only provides a private constructor, 2. The class definition contains a static private object of the class, and 3, which provides a static public function to create or get a static private object of that itself. Application Scenario: Resource Manager C + + code implementation://C10 SingleTon #include <iostream>using namespace std; Class egg{static Egg e; int num; Egg (int x): num (x) {} Egg (const egg&); Copy constructor public:static egg* getinstance () {return &e;} int val () {return num--;}}; Egg egg::e (+); int main () {//egg x (); Cout<<egg::getinstance ()->val () <<endl; Cout<<egg:: GetInstance ()->val () <<endl; Cout<<egg::getinstance ()->val () <<endl;}
Single case Mode singleton