|
|
|
|
|
|
|
If a message is created first, who creates it? Is it created by the menu command code? If so, does the menu also tell the message to edit itself, or is this part of the constructor method of the message? |
|
|
|
|
|
|
|
|
It makes sense for the constructor to do this at first glance; after all, every time you create a message you'll probably want to edit it. Nonetheless, this is not a good design idea. First, it is very possible that the premise is wrong: you may create canned messages (that is, error messages mailed to the system operator) that are not put into an editor. Second, and more important, a constructor's job is to create an object: it should do no more and no less than that. Once a mail message is created, the constructor's job is done. Adding a call to the edit method just confuses the role of the constructor and makes the mail message vulnerable to failures in the editor. |
|
|
|
|
|
|
|
|
Worse yet, the edit method will call another class: the editor; causing its constructor to be called. But the editor is not a base class of the message, nor is it contained within the message; it would be unfortunate if the construction of the message depended on successful construction of the editor. |
|
|
|
|
|
|
|
|
Finally, you won't want to call the editor at all if the message can't be successfully created; yet successful creation would, in this scenario, depend on calling the editor! Clearly you want to fully return from message's constructor before calling Message::Edit(). |
|
|
|
|
|
|
|
|
Working with Driver Programs |
|
|
|
|
|
|
|
|
New Term: One approach to surfacing design issues is to create a driver program early in the process. A driver program is a function that exists only to demonstrate or test other functions. For example, the driver program for PostMaster might offer a very simple menu that will create PostMasterMessage objects, manipulate them, and otherwise exercise some of the design.x |
|
|
|
|
|
|
|
|
Listing 22.3 illustrates a somewhat more robust definition of the PostMasterMessage class and a simple driver program. |
|
|
|
|
|
|
|
|
LISTING 22.3 A DRIVER PROGRAM FORPostMasterMessage |
|
|
|
 |
|
|
|
|
1: #include <iostream.h>
2: #include <string.h>
3:
4: typedef unsigned long pDate;
5: enum SERVICE { PostMaster, Interchange, CompuServe, Prodigy, AOL, Internet };
6:
7: class String
8: { |
|
|
|
 |
|
|
|
|
continues |
|
|
|
|
|