Introduces the design patterns involved in the MFC Framework (II)

Source: Internet
Author: User

Then the previous article introduces the design pattern (i) involved in the MFC framework

Singleton mode (Singleton pattern)

The singleton mode is a frequently used software design pattern. In its core structure, only a special class called a singleton class is included. The singleton mode ensures that there is only one instance of a class in the system and that the instance is easily accessible to the outside world. Thus, it is convenient to control the number of instances and save system resources.

Singleton mode is the best solution if you want to have only one object in a class in the system.

The main points of the singleton pattern are three: 1. A class can only have one instance;

2. It must create this instance on its own;

3. It must provide this instance to the entire system on its own.

A typical structure diagram for a singleton pattern is seen below:

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvqkjpt1q=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70 /gravity/southeast ">

As we can see, we record this unique object instance by maintaining a static member variable. This unique instance is obtained through a instance () interface.

The following is a sample example of a singleton pattern implemented in C + + to help you understand the singleton pattern. Note (VC6.0 can execute)

Code snippet 1:singleton.h//singleton.h #ifndef _singleton_h_ #define _SINGLETON_H_ #include <iostream> using namespace Std Class Singleton {public:static singleton* Instance ();//Gets the interface of the unique instance object protected:  Singleton ();//prevents being instantiated by external invocation. can also be declared as Privateprivate:static singleton* _instance; Save a unique instance object}; #endif//~_singleton_h_ Code snippet 2:singleton.cpp//singleton.cpp #include "Singleton.h" #include <iostream> using namespace Std; singleton* singleton::_instance = 0; Singleton::singleton () {cout<< "Singleton ...." <<endl;} singleton* singleton::instance () {if (_instance = = 0) {_instance = new Singleton ();}  return _instance; } code Snippet 3:MAIN.CP//main.cpp #include "Singleton.h" #include <iostream> using namespace std;  int main (int argc,char* argv[]) {singleton* sgn = singleton::instance ();  

Let's look at the singleton patterns in MFC.

each mfc Application examples are sent to the class cwinapp cwinapp cwinapp afxgetapp afxgetinstancehandle AfxGetResourceHandle afxgetappname

MFC is through ASSERT to prevent multiple constructs CWinApp of the object. The second time the CwinApp object is constructed. The expression inside the ASSERT is false, and an error message pops up.



Introduces the design patterns involved in the MFC Framework (II)

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.