|
|
|
|
|
|
|
ber (the ID number) and the extra operations of Punch, Print, and so forth. Both inheritance and composition give us four private data members: hours, minutes, seconds, and ID number. However, the use of inheritance means that all of the Time operations are also valid for TimeCard objects. A user of the TimeCard class couldeither intentionally or accidentallyinvoke operations such as Set and Increment, which are not appropriate operations on a time card. Furthermore, inheritance leads to a confused design in this example. It is not true that a TimeCard is a Time; rather, a TimeCard has a Time (and an ID number). In general, the best design strategy is to use inheritance for is-a relationships and composition for has-a relationships. |
|
|
|
|
|
|
|
|
Step 3: Design the Driver |
|
|
|
|
|
|
|
|
The final step is to design the driverthe top-level algorithm. In OOD, the driver is the glue that puts the objects (along with their operations) together. When implementing the design in C++, the driver becomes the main function. |
|
|
|
|
|
|
|
|
Notice that structured design begins with the design of the top-level algorithm, whereas OOD ends with the top-level algorithm. In OOD, most of the control flow has already been designed in steps 1 and 2; the algorithms are located within the operations on objects. As a result, the driver often has very little to do but process user commands or input some data and then delegate tasks to various objects. |
|
|
|
| |
|
|
|
|
The Iterative Nature of Object-Oriented Design |
|
|
|
| |
|
|
|
|
Software developers, researchers, and authors have proposed many different strategies for performing OOD. Common to nearly all of these strategies are three fundamental steps: |
|
|
|
| |
|
|
|
|
1. Identify the objects and operations. |
|
|
|
| |
|
|
|
|
2. Determine the relationships among objects. |
|
|
|
| | |
|
|
|
|
Experience with large software projects has shown that these three steps are not necessarily sequentialStep 1, Step 2, Step 3, then we are done. In practice, Step 1 occurs first, but only as a first approximation. During Steps 2 and 3, new objects or operations may be discovered, leading us back to Step 1 again. It is realistic to think of Steps 1 through 3 not as a sequence but as a loop. |
|
|
|
| |
|
|
|
|
Furthermore, each step is an iterative process within itself. Step 1 may entail working and reworking our view of the objects and operations. Similarly, Steps 2 and 3 often involve experimentation and revision. In any step, we may conclude that a potential ob- |
|
|
|
|
|
|
|
|
|
(text box continues on next page) |
|
|
|
|
|