A singleton pattern of design patterns

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.