A glimpse of proxy mode by Cocos2d-x project entrance

Source: Internet
Author: User

With regard to design patterns, since "Gang of four" was first raised to a theoretical height in "design patterns:elements of reusable object-oriented software", Development to this day has become a well-known summary of code design experience. However, most people are intimidated by the specific use of design patterns, specifically because the theories mentioned in the book are often too obscure for the reader to see the results, but they do not understand the motives and processes of the design, that is, the lack of support for large project practices, or the iterative, development, and refactoring of a hundreds of thousands of-line project, It is really difficult to understand the wisdom of design patterns.

Naturally, the author also dare not say how to understand the design pattern, just from some open-source projects to see a little design pattern of shadow, this article intends not to do too much theoretical explanation, but directly from the COCOS2D-X Project entrance code part of the attempt to share with you the proxy model embodied. Note: The cocos2d-x version used by the author is 2.2.6 and cannot be guaranteed to be the same as the 3.X version.

First build a simple Hello World project (the specific process does not elaborate, the web can see the tutorial), we find the main function, the code is as follows:

1 intapientry _tWinMain (hinstance hinstance,2 hinstance hPrevInstance,3 LPTSTR lpCmdLine,4                        intncmdshow)5 {6 Unreferenced_parameter (hprevinstance);7 Unreferenced_parameter (lpcmdline);8 9     //Create the application instanceTen appdelegate app; Onecceglview* Eglview =Cceglview::sharedopenglview (); AEglview->setviewname ("Hello World"); -Eglview->setframesize (480, the); -     returnCcapplication::sharedapplication ()run (); the}

From the appdelegate app; We see that this is a proxy mode, view its definition we saw class appdelegate: private cocos2d:: Ccapplication , that is, inherits from the Ccapplication class. Let's put it aside and keep looking down.

Obviously, the entry that wants to jump from the main function to the project is implemented from the Code of ccapplication::sharedapplication ()->run ( ). where Sharedapplication () is a singleton pattern with a static pointer inside it, and the pointer is empty to create the object, not NULL to skip, so set to ensure that multiple calls still return only one singleton. Of course, this article is not to explain the singleton mode, simple mention. Below we go to the definition of ccapplication, find the following code:

 1  //  2   Applicationdidfinishlaunching ())  3   {  4  return  0  ;  5 } 

Obviously, this is the entrance to the entire game project. Let's consider where the function is defined. If  applicationdidfinishlaunching ()   is a member function in the Ccapplication class, We can call directly without concern. And the truth is this? We go into its definition. See  bool appdelegate:: Applicationdidfinishlaunching ()   code like this. That is, the real implementation is done in the appdelegate. However, we find that there is neither definition nor declaration in the Ccapplication class, why is it possible to use? We see the Ccapplication class, see  class Cc_dll Ccapplication: public ccapplicationprotocol   This sentence, That is, it is an inherited child ccapplicationprotocol. Jumping again to the definition of the function, we see  virtual bool applicationdidfinishlaunching () = 0 ;  , this is a pure virtual function. What is pure virtual function? Pure virtual function is a special virtual function, in many cases, in the base class can not give a meaningful implementation of virtual function, and declare it as pure virtual function, its implementation is left to the derived class of the base class to do. In a derived class, if the interface is not replicated (OverRide), the derived class remains a pure virtual base class. Obviously, there is no replication in the Ccapplication class, but the interface can be called directly.

Back to the top, we know that Appdelegate inherits from the Ccapplication class and gives the definition of the function in Appdelegate virtual bool Applicationdidfinishlaunching (); , this is not a pure virtual function, that is, it can be implemented.

Review the logic and organize the following:

1 #if0

8Ccapplicationprotocol//Interface9 Virtual BOOLApplicationdidfinishlaunching () =0;//an interface that defines a pure virtual functionTen One //different logic for each platform ACcapplication: PublicCcapplicationprotocol - Run () - { theApplicationdidfinishlaunching ();//Call this interface - } - -Appdelegate:Privateccapplication +Applicationdidfinishlaunching ()//implementing Interfaces - { + the real entrance; A } at - Virtual BOOLapplicationdidfinishlaunching (); - - Virtual voidApplicationdidenterbackground (); - - Virtual voidApplicationwillenterforeground (); in - #endif

About the advantages of the proxy mode:

    • Clear responsibilities, the real role is to achieve the actual business logic, do not care about other non-responsibility of the transaction, through the late agent to complete a transaction, the accompanying result is simple and clear programming.
    • A proxy object can act as an intermediary between the client and the target object, which plays a role and protects the target object.
    • High scalability

Well, this article is going to end, through a concrete project case, I hope you can learn some new content of the agency model. On the part of the theory is not much elaboration, we can go to see the "design pattern" this book.

Note: This article is the author's original, welcome reprint, but please indicate the author and source. In addition, the author is currently in the University of Computer College senior reading, is looking for work, the desire to seek for C + + development direction, welcome private messages or mail contact.

A glimpse of proxy mode by Cocos2d-x project entrance

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.