Sams Teach Yourself C++ in 21 Days

(singke) #1
Devices
If your system interacts with or manipulates devices (such as printers, cameras, modems,
scanners, and so forth), the specifics of the device protocol ought to be encapsulated in a
class. Again, by creating classes for the interface to the device, you can plug in new
devices with new protocols and not break any of the rest of your code; just create a new
interface class that supports the same interface (or a derived interface), and off you go.

Building the Static Model ..............................................................................


When you have established your preliminary set of classes, it is time to begin modeling
their relationships and interactions. For purposes of clarity, the static model is explained
first, and then the dynamic model. In the actual design process, you will move freely
between the static and dynamic models, filling in details of both—and, in fact, adding
new classes and sketching them in as you learn from each.
The static model focuses on three areas of concern: responsibilities, attributes, and rela-
tionships. The most important of these—and the one you focus on first—is the set of
responsibilities for each class. The most important guiding principle is this:Each class
should be responsible for one thing.
That is not to say that each class has only one method. Far from it; many classes will
have dozens of methods. But all these methods must be coherent and cohesive; that is,
they must all relate to one another and contribute to the class’s capability to accomplish a
single area of responsibility.
In a well-designed system, each object is an instance of a well-defined and well-under-
stood class that is responsible for one area of concern. Classes typically delegate extrane-
ous responsibilities to other, related classes. By creating classes that have only a single
area of concern, you promote the creation of highly maintainable code.
To get a handle on the responsibilities of your classes, you might find it beneficial to
begin your design work with the use of CRC cards.

Using CRC Cards
CRC stands for Class, Responsibility, and Collaboration. A CRC card is nothing more
than a 4×6 index card. This simple, low-tech device enables you to work with other peo-
ple in understanding the primary responsibilities of your initial set of classes. You assem-
ble a stack of blank 4×6 index cards and meet around a conference table for a series of
CRC card sessions.
How to Conduct a CRC Session For a large project or component, each CRC ses-
sion should be attended, ideally, by a group of three to six people; any more becomes
unwieldy. You should have a facilitator, whose job it is to keep the session on track and

354 Day 11

Free download pdf