The MFC is not the only library that uses the design mode, and it is impossible to be a unique one. It can be sure that all good frames must be full of design patterns. The purpose of this paper is to remind everyone that when learning the framework, use the theory and ideas of design patterns to understand the links between many classes, from the overall angle to the study section, obviously better results than the overall to understand the whole. This article will give some design patterns in the MFC.
However, it has to be sorrowful, and the core class CWnd in the MFC has violated a basic standard of frame design: Minimize. It can be said that CWnd is a good example of a "root fertilizer" criterion. I don't know how many people really understand the number of member functions in CWnd? In a sense, MFC generally achieves inheritance, and rarely uses a combination class or object, and it is also in a certain sense and the software design pattern is contrary. Inheritance in the three major characteristics of OO, it seems to mean that we use the inheritance mode to build a frame, but in the design mode, it can be seen more important ways. Hey, I have also been one of the victims of inheritance ideas.
However, I must admit that inheritance is more easier to understand than the combination, at least for me. If you should splify CWND into more small interfaces from the angle of the perfect software design pattern? In this way, our view, dialog, button, etc. can get a clearer interface. But this cost also increases the difficulty of understanding the framework. From a entry perspective, we can't ask him to learn "design mode" first, then learn MFC. My suggestion is to learn MFC for a while (half a year? 100 examples? Understand 20 classes of MFC?), You should learn from design patterns. Perhaps this is also a good way to learn the framework.
I have to remind them, please pay attention to the consequences of excessive use of mode design. Netwind (Wheated Wind) article (mode design and formism) http://www.sawin.com.cn/doc/sd/pattern/pattern.htm is a very good example. For my own experience, when I started to contact design mode, I simply had the feeling of 醍醐 醍醐 顶,,,,,,,,? ! Now, I have to admit that the design is good, but it is not easy, especially for my lazy programer, "the principle is deep, the application is fast" is my goal. This can feel from my "Jinshan Words' Laboratory Reading Procedure", a program running for more than an hour, I will be too lazy to optimize. :)
Small Note: Of course, many obscured macros are also added to the MFC's framework to add a lot of "brilliance." To a certain extent, I agree that Bigwhiteshark comrades at http://dev.9cbs.net/Article/20/20831.shtm (criticized to VC, I want to combat the enthusiasm of everyone to learn VC ...) View: from Microsoft Strategy to see the future of VC! Microsoft's strategy is to provide a new environment with .NET, then gradually modify its OS, and finally abandon the NT core, the ultimate goal is to preempt the high-end market share occupied by UNIX and Linux (this is Borland Li Wei, I I feel that it makes sense). Now the .NET framework also supports the original technology (such as COM ), also MFC7 in VC.NET, but this is just a transitional product, just like Windows 3.1, it is to be abandoned. . Final suggestion: Limited to my theoretical level and time energy, I suggest that you will integrate your understanding of the design model in MFC, I believe this is a very meaningful job. I will continue this job.
Welcome to my blog: caijingwei.blogcn.com.
Factory
Define an interface for creating an object, which allows the subclass to decide which class is instantiated. Factory Method delays a class of instantiation to their subclasses.
The process of creating its view is created. CFAMewnd is a CREATOR that creates different ConcreteProducts by calling cframeWnd :: CreateView ().
Description: In fact, CreateView () and dynamically created objects are closely related. This is a more interesting design in the MFC, and the C RTTI (runtime type information) is different. You can go to study Runtime_Class, Declare_Dynamic, Declare_DyncReate these macros.
Builder:
Separate a complex object construct and its representation, so that the same creation process can create different representations.
In Int CMAINFRAME :: OnCreate (LPCreateStruct LPCreatestruct), create a frame window:
m_wnddlgbar.create ()
m_wndrebar.create ()
m_wndstatusbar.create ()
Can you understand as a simplified Builder mode? Director and Builder are CMAINFRAME.
Adapter
Convert a class interface into another interface that customers want.
Adapters in STL: Stack, Queue, Priority_Queue. It can be seen as a simplified Adapter mode, which changes the interface of the base class, but there is no other object.
Description: STL is not part of the MFC, part of the C standard library, which is placed in this article, which can be said to be the author's lazy. :)
Composite:
All window classes are derived from CWnd, while derived window classes can also contain other classes derived from the CWnd, the parent child relationship between the windows constitutes the tree structure of the window. This mechanism is a typical structural mode Composite in the design mode. You have a series of similar objects, and each of this series of objects has a series of objects, so recursively, manage it. very convenient. This is the basic model of Windows message processing.
Proxy
Provide a proxy for other objects to control access to this object. Smart Pointer, such as _com_ptr_ttemplate:
I believe that comrades of learning MFC have a universal feeling: I don't know which message mapping function to use, I don't know where to write code. For example, cdocument (), onopendocument (), onsavedocument (), onclosedocument (). Also, for example, the process related functions: prepareprinting (), onbeginprinting (), onPreparedc (), onPrint (), OneNDPrinting (). In fact, this is to use the Template mode: Define the algorithm framework in an operation, and delay some steps into the subclass. The aim is to make the subclass do not need to change the structure of an algorithm to redefine certain specific steps of the algorithm. It is important to: The base class must indicate which of the hook operations (which can be redefined), which is an abstract operation (must be redefined).
Iterator:
The ITerator in STL has a good iderator mode from the name and way of operation. If you want more subordinate, the Iterator in the STL belongs to the external iterator, which is controlled by the customer.
Foreach () statement in C # and VB. Object sets in the foreach () statement must implement an internal iterator.
The MVCMVC mode is a basic design pattern for separating user interface logic to business logic, which divides data processing, interface, and user behavior control: Model-View-Controller.
Model: Responsible for data acquisition and changes in the current application and related business logic
View: Responsible for display information
Controller: Responsible for collecting input to transformation users
VIEW and Controller are dependent on Model, but Model does not depend on View, nor does it depend on Controller, which is one of the main advantages of separation, so Model can establish and test separately for code multiplexing, View and Controller only need MODEL provides data, they will not know, nor is it that data is stored in SQL Server or any place in an Oracle database. .
The DOC-View structure in the MFC can be seen as a simplification of this mode. The Document class is equivalent to Model, and it does not seem to express the Controller, and the Controller function can be implemented in the Document class.