By the Cocos2d-x engineering entry view Agent Mode, cocos2d-x View
With regard to Design Pattern, since the first time "four-person gang" has raised Design Patterns: Elements of Reusable Object-Oriented Software to a theoretical level, today, it has become a well-known Summary of code design experience. However, most people are daunting about the specific use of the design model, because the theory mentioned in the book is often too obscure and the readers only see the results, but they do not understand the motivation and process of such design. That is, they lack the support of large-scale project practices, or have not experienced iteration, development, and reconstruction of a project with hundreds of thousands of lines, it is indeed hard to understand the wisdom of the design model.
Naturally, the author does not dare to say that he understands the design pattern much, but only sees some shadows of the design pattern from some open-source projects. This article intends not to give too many theoretical explanations, instead, I tried to share with you the proxy mode from the code Part Of The Cocos2d-x project portal. Note: The Cocos2d-x version used by the author is 2.2.6, cannot guarantee that version 3.x is the same as applicable.
First create a simple Hello World project (the specific process is not described, you can view the tutorial online), we find the main function, the Code is as follows:
1 int APIENTRY _tWinMain(HINSTANCE hInstance, 2 HINSTANCE hPrevInstance, 3 LPTSTR lpCmdLine, 4 int nCmdShow) 5 { 6 UNREFERENCED_PARAMETER(hPrevInstance); 7 UNREFERENCED_PARAMETER(lpCmdLine); 8 9 // create the application instance10 AppDelegate app;11 CCEGLView* eglView = CCEGLView::sharedOpenGLView();12 eglView->setViewName("Hello World");13 eglView->setFrameSize(480, 320);14 return CCApplication::sharedApplication()->run();15 }
From AppDelegate app, we can see that this is a proxy mode. From its definition, we can see that class AppDelegate: private cocos2d: CCApplication inherits from the CCApplication class. Let's put it aside and continue looking at it.
Obviously, the entry to jump from the main function to the project is implemented from the code CCApplication: sharedApplication ()-> run. SharedApplication () is a singleton mode with a static pointer in it. If the pointer is null, the object is created. if the pointer is not null, the object is skipped, this setting ensures that only one Singleton is returned for multiple calls. Of course, this article does not explain the singleton mode. I would like to mention it briefly. Next, we define the transfer to CCApplication and find the following code:
1 // Initialize instance and cocos2d.2 if (!applicationDidFinishLaunching())3 {4 return 0;5 }
Obviously, this is the entrance to the entire game project. Where is the function defined? If applicationDidFinishLaunching () is a member function in the CCApplication class, we can directly call it without any concerns. Is that true? We transfer it to its definition. You can see code like bool AppDelegate: applicationDidFinishLaunching. That is, the real implementation is completed in AppDelegate. However, we found that there is neither definition nor declaration in the CCApplication class. Why can this problem be used? Let's look at the CCApplication class and see the class CC_DLL CCApplication: public CCApplicationProtocol, that is, it inherits the child CCApplicationProtocol. Jump to the definition of the function again. We can see that virtual bool applicationDidFinishLaunching () = 0; is a pure virtual function. What is a pure virtual function? Pure virtual functions are special virtual functions. In many cases, virtual functions cannot be meaningful in the base class, but declared as pure virtual functions, its implementation is left to the derived class of the base class. In a derived class, if the interface is not overwritten, the derived class is still a pure virtual base class. Obviously, the CCApplication class does not perform secondary writes, but you can directly call this interface.
Back to the top, we know that AppDelegate inherits from the CCApplication class. In AppDelegate, the definition of this function is given. virtual bool applicationDidFinishLaunching (); this is not a pure virtual function, that is, it can be implemented.
Review the logic as follows:
1 # if 0
8 CCApplicationProtocol // Interface 9 virtual bool applicationDidFinishLaunching () = 0; // define the Interface for a pure virtual function 10 11 // different logic of each platform 12 CCApplication: public CCApplicationProtocol13 run () 14 {15 applicationDidFinishLaunching (); // call this interface 16} 17 18 AppDelegate: private CCApplication19 applicationDidFinishLaunching () // implement interface 20 {21 real entry; 22} 23 24 virtual bool applicationDidFinishLaunching (); 25 26 virtual void applicationDidEnterBackground (); 27 28 virtual void applicationWillEnterForeground (); 29 30 # endif
Advantages of proxy mode:
- Clear responsibilities: the real role is to implement the actual business logic, do not care about other transactions that are not in this responsibility, complete a transaction through a later agent, with the result that the programming is concise and clear.
- Proxy objects can play a mediation role between the client and the target object, which plays a role and protects the target object.
- High scalability
Now, this article is about to end. Through a specific project case, I hope you can learn some new content about the agent model. I will not elaborate on the theory. You can go to the design pattern book.
Note: This article is the author's original work. You are welcome to repost it, but please indicate the author and its source. In addition, I am a senior at Harbin Institute of Computer Science and Technology. I am looking for a job. I am looking for a job in C ++ development direction. Please contact me by email or by yourself.