From MVC architecture to C + + polymorphism implementation __c++

Source: Internet
Author: User

Turn from: http://blog.csdn.net/historyasamirror/article/details/5025061

Learning can be a very happy thing, especially when you find that the bits and pieces you learned before can be strung together or turned into a ring, it feels good. This is how the article came about.

Let's start with the MVC architecture. The two-day system has a look at the content of the MVC architecture, mainly for reference to the literature "1".

MVC should be familiar to a lot of people over the years, because quite a lot of web frameworks use this architecture, and, as early as MFC is rampant, the Document/view architecture used by MFC is also a variant of the MVC architecture. Including QT, it's model/view is also so. But they all combine the view of MVC with the functionality of controller.

The full name of MVC is Model-view-controller architecture, which was first used in the Smalltalk language. MVC is best suited for use in interactive applications.

I personally think that the most important thing to understand about the MVC architecture is two points:

1. MVC separates Data maintenance and presentation of data from user interaction. Model is responsible for the maintenance of data, just like the db and file to save data, you can think it is process. and view is responsible for the presentation of data, the data in some form in front of the user show, see it as output. Model and view relationship is like the following picture.

and controller is responsible for processing the user's input. It provides a window or a control for the user to enter data, which is input. So, an MVC architecture is to decouple the Process,input and output solutions of an interactive program.

2. This is even more important: the link between model and view and controller. Any MVC framework must provide a "change-propagation mechenism" (change-propagate mechanism). This change-propagation mechanism is the only link between model and view and controller in the MVC framework (the change-propagation mechanism is the only link between the model and the views and controllers). For example, a user changes the data in model by controller, then the model uses the change-propagate mechanism to inform the view and controller about it, allowing the view to refresh the data and display it again.

A lot of people are always not proficient in the MVC architecture, the core problem is that he is using the MVC architecture is not see the change-propagation system, so the MVC architecture of the internal operating mechanism is not understood.

The complete process is as shown in the figure:

1. User operation Controller, which is equivalent to calling controller Handleevent function;

2. The Handleevent function actually calls the member function in model and operates on the data in model.

3. Model in the data is called to complete, model will perform its own notify function;

4. The Notify function invokes all the view and controller update functions associated with this model;

5. The update function will call the respective display function and the GetData function;

6. When all this was done, Handleevent returned;

More about MVC is not in this article, after all, I write this article is not light for MVC. Interested can view the network documentation or reference documents.

The next point is to discuss the implementation of this change-propagation mechenism.

In fact, a simple MVC architecture change-propagation mechanism using observer mode + polymorphism can be done. Model maintains a pointer queue of base view (and controller), placing all pointers to the derived view associated with this model in this queue. Then the model's notify function is the update member function that calls the pointer in the queue in turn.

However, in the actual C + + MVC system, such as MFC, or QT, did not use this method to implement the change-propagation mechanism, in fact, they do not implement the mechanism of polymorphism. MFC uses the mechanism of message mapping, the basic concept is to build a message lookup table, the message and the corresponding function mapping relationship stored. Every time you process a message, you go to the table to find the corresponding function, and then callback. The signal-slot mechanism of QT (the specific implementation mechanism I am not clear, but certainly not with the polymorphic).

Why MFC and QT do not adopt polymorphism. I believe there are many reasons, such as QT's signal-slot requirements are able to cross the process, this is certainly not in polymorphic to do. I'm only talking about one reason here.

Let's talk about the implementation of the polymorphic mechanism of C + + (I recommend you look at the reference "2" instead of my paragraph, "2" to explain this problem very clearly). Many people know vtable, here is a class of a virtual function pointer array, a class pointer if you want to call the virtual function, you will first find the vtable by vptr, and then find the corresponding function. The mechanism itself is not problematic. But the point is, C + + holds a pointer to all virtual functions in vtable, that is, if a base class has 1000 virtual functions, but its inheriting class overwrites only 5 of them, there are still 1000 items in the vtable of the inherited class, and 995 of the tables are wasted. It is for this reason that MFC and QT do not adopt C + + polymorphism mechanism to implement the change-propagation mechanism. Because in MFC and QT, each of its base classes has a large number of virtual functions, and in the practical application, the inheriting class may just rewrite a few of them, if the adoption of a polymorphic implementation, then waste a lot of memory space.

By borrowing a passage from the literature "2", "It is for this reason, from owl to VCL, ... From MFC to QT, so that in recent years the GUI and game development framework, all involving a large number of event behavior of the C + + GUI Framework does not use the standard C + + polymorphic technology to construct the window class level, but to each other, invented a variety of techniques to circumvent the reef. Among the classic solutions are three, which are represented by VCL dynamic method, MFC global Event lookup table and Qt Signal/slot. And the idea behind it is the same, summed up in a Grady Booch, is: "When you find that the system needs a large number of similar small classes, you should use a large number of similar small objects to solve." "That is, some problems that would have resulted in the need to derive a new class to be resolved by instantiating a new object." This idea almost certainly leads to a mechanism like delegate in C # that becomes a necessity. Unfortunately, standard C + + does not support delegate. Although many people in the C + + community have made various efforts to apply advanced techniques such as template and functor, there is still a gap between the results and the real delegate. Therefore, in order to keep the solution simple, Borland c++builder extended the __closure keyword, MFC invented a lot of absurd macros, Qt got a MOC before the processor, eight Immortals crossing, recount. ”

Conclusion

I am not quite sure of the above arguments because, as I said, there may be various considerations in the practice of not using polymorphism, and the reason I mentioned may be negligible.

In order to refute my own point of view, you can calculate, when MFC was first born, at that time the computer memory is very small, so we are very frugal, but now, the computer's memory is very large, a vtable even thousands of function pointers, it can be negligible.

Reference documents

"1" model-oriented Software Architecture Volume 1: Mode system

Realization and reflection of "2" C + + polymorphism Technology

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.