The singleton pattern contains only one special class of a singleton class in its core structure, guaranteeing that there is only one object in the whole system through a singleton class.
Code:
1#include <iostream>2 3 classA4 {5 Public:6 intA;7 intb;8 intC;9 StaticAInstance;Ten StaticA *getinstance () One { A if(!Instance) - { -Instance =NewA; the } - returnInstance; - } - }; + -A * A::instance =NULL; + A voidfunct () at { -A *BB =a::getinstance (); -Std::cout << Bb->a <<Std::endl; - -Std::cout << bb->b <<Std::endl; - inStd::cout << bb->c <<Std::endl; - } to + - intMainintargcChar*argv[]) the { *A *AA =a::getinstance (); $Aa->a =1;Panax NotoginsengAa->b =3; -Aa->c =5; the funct (); + AStd::cin.Get(); the return 0; +}
This model uses a lot in cocos, such as the Ccdirector class.
For example, we write a tool class. Using a singleton is a good choice.
A singleton pattern of design patterns