In this chapter, you will develop a data model and a simple program to test it. The example is called DMTEST.
This section is an overview of what the program does and what you will be learning about the Microsoft Foundation Class Library.
A data model is an abstraction that represents the structure of the data that a program manages. The data model for this chapter, for example, consists of person objects and a list object to contain them.
The data model is completely independent of the user interface. The data model knows nothing about how the data will be displayed to the user, nor does it know how the user will communicate with the program. The data model communicates with the user interface of the program through a well-defined set of member functions.
One way to think of this relationship is that the data model is a server, and the user interface is a client. The user interface interacts with the user and translates user input into requests that the interface sends to the data model. The data model responds to the requests and sends information back to the user interface, which the user interface then displays to the user.
Figure 2.1 shows the relationship between the user, the user interface, and the data model. You can see that the user never directly interacts with the data model.
The data model's independence from the user interface is a very important concept in the design of resusable programs. This independence enhances the reusability of the data model. Thus, the data model for names and phone numbers developed in this chapter can be used with a text-only interface or with a Microsoft Windows interface without any changes to the data model. The next chapters show how to develop a Windows user interface that works with the data model to create a complete interactive program.
The purpose of the example program is to manage a list of names and phone numbers. Each data item represents a single person and contains that person's name and phone number. The user can add persons to the list, find all matches for a specified name, and save and restore the data to and from a disk file. The C++ objects constituting the data model provide all of these capabilities.
The DMTEST program demonstrates these capabilities by:
Creating a database and adding names to it.
Serializing the database (writing it to disk).
Deserializing the database (reading it from disk).
Searching the database for a person.
Disposing of the objects.
To view the complete code for the DMTEST program, see Listings 1, 2, and 3 at the end of the chapter.
The code shown is available on the distribution disks in files PERSON.H, PERSON.CPP, and DMTEST.CPP.
Microsoft Foundation Classes Used in the Data Model
This chapter demonstrates the use of six classes from the Microsoft Foundation Class Library:
Class CObject
Each record in the database is represented by an object of the class CPerson, which is derived from the Microsoft Foundation Class CObject.The CPerson class builds upon the functionality of CObject, adding member variables representing the name and phone number of a person. In addition, the CPerson class overrides functions from CObject that are related to serialization so that the name and phone number can be saved to and restored from disk.
Class CObList
Collection classes are designed to contain collections of similar objects. The Microsoft Foundation Class Library provides three kinds of collections: lists, arrays, and maps (or dictionaries). In the example, a list collection is used to contain all the CPerson objects in the database. The Microsoft Foundation Classes include several useful list classes, but because we need some specialized list functionality, the list used in this chapter will be derived from the Microsoft Foundation CObList class. The specialized list class makes use of all of CObList's capabilities, but also adds some new functions, including one that can find all elements of the list that match a specified last name.
Class CString
CString objects represent the name and phone number member variables of a CPerson object.
Class CTime
A CTime object represents the last modification time and date of a CPerson
object.
Classes CFile and CArchive
A CFile object identifies and opens the file used to serialize the database. Serialization in the Microsoft Foundation Class Library is done with a CArchive object, which uses an opened CFile object to perform the serialization.
Other Capabilities Demonstrated
The following list describes other capabilities that your data objects can use. Some are available because your objects are derived from class CObject and some simply because you are using the Microsoft Foundation Class Library. These capabilities are demonstrated in the DMTEST program.
Serialization
Serialization is the act of saving an object to a disk file or reading it back in (sometimes called “deserialization”). Objects of the class CPerson can serialize themselves to and from a disk file. Likewise, the collection of CPerson objects can serialize itself and all its elements. Because a collection can automatically serialize all its elements, the act of serializing the database is reduced to a single function call to serialize the collection. This cuts down the amount of code you have to write.
Exceptions
The Microsoft Foundation Class Library's exception-handling mechanisms “catch” exceptions that are “thrown” by the Microsoft Foundation Class Library functions as those functions encounter errors. Exceptions provide a way for you to respond to errors, especially by the file-handling classes. This, along with the TRACE macro for printing messages, gives you a convenient, structured way to process errors, both during development and in the finished program.
Diagnostics
The TRACE macro is used throughout the code in this chapter to provide diagnostic output to track program progress. The Microsoft Foundation Class Library also provides an ability to dump the contents of objects to assist in debugging your program and facilities for testing the validity of your assumptions, such as whether a pointer points to a valid area of memory.