By default, the MFC Appwizard creates an application skelton with a document class and a view class. MFC separates data managment into these two classes.
The Document stores the data and updating multiple views of the data. The view displays the data and messages user interaction with it, including selection and editing.
In this model, an MFC document object reads and writes data to persistent storage. A separate view object manages data display, from rendering the data in a window to user selection and editing of data. The view object obtains display data from the document and communicates back to the document any changes. While you can easily ignore the document/view separation, there are compelling reasons to follow this model in most cases one of the best is when you need multiple views of the same document, such as both a spreadsheet and a chart view. The document/view model lets a separate view object represent each view of data, while code common to all views can reside in the document. The document also takes on the task of updating all views whenever the data changes.
The MFC document/view architecture makes it easy to support multiple view, multiple document types, splitter windows and other valuable user interface features.
At the heart of document/view are four key classes. These are as follows :-
1) The CDocument class supports objects used to store or control your progress data and provides the basic functionality for programmers defined document classes.
2) The Cview class provides the basic functionality for programmer defined view classes. A view is attached to document and acts as an intermediary between the document and the user. The view renders an image of the document on the screen and interprets user input as operations upon the document.
3) CFrameWnd supports objects that provides the frame around one or more views of a document.
4) CDocTemplate ( or CSingleDocTemplate or CMultiDocTemplate ) It supports an object that co-ordinates one or more existing documents of a given type manages creating the correct document, view and frame windows object for that type.
Document their views and frames windows are created by the document template. The document template is responsible for creating and managing all documents of one document type.
In SDI applications the document template (which is used to create new document ) is of CSingleDocTemplate type and the program places view, document and main window classes into that template.
In MDI applications, view and document classes connected to CMultiDocTemplate type document template as well as to childFrame class which supports MDI child windows ( windows that appear inside the main window).
# Advantages of the Document/View architecture :->
The key to using the MFC Document/View architecture is that the architecture supports multiple views of the same document particularly well. Suppose your application lets users view numerical data either in spreadsheet form or in chart form. A user might want to see simultaneously both, the raw data in spreadsheet form and a chart that results from the data you display these separate views in separate frame windows or in splitter panes within a single window. Now suppose the user can edit the data in the spreadsheet and the changes instantly reflected in the chart.
In MFC, the spreadsheet view and the chart view would be based on different classes derived from CView both views would be associated with a single document object. The document stores the data. Both views access the document and display the data they retrieves from it.
When a user updates one of the views, that object calls: CDocument : : UpdateAllviews method. That function notifies all of the document's views, and each view update itself using the latest data from the document.
This scenario would be difficult to code without the separation of data from view; Particularly if the views stored the data themselves with document/view architecture it is easy. The framework does most of the co-ordination work for you.