Object Design Using CRC Cards by Charles W. Neville copyrignt 2000 by Charles W. Neville 0. Introduction. CRC cards are 4 by 6 index cards used in the design of object oriented programs. The initials CRC stand for "Class, Responsibility and Collaboration". Normally, a significant program written in an object oriented language like Java is divided into several different classes. These classes cooperatively work together to accomplish the goals of the program. It can be difficult to understand how to divide a program into separate classes, and how to divide a given class into separate methods. CRC cards help in this design process. The front of a CRC card for a class looks like this, --------------------------------------------------- | Class Name | Collaborators | | ----------------------------- | | | Responsibilities | | | | | | | | | | | --------------------------------------------------- The "Class Name" section just contains the name of the class. The "Responsibilities" section describes the tasks an object of the class is supposed to carry out. It should simply describes what is to be done, and it should avoid mentioning any of the detaile details of how the tasks are to be carried out. The "Collaborators" section should definitely contain any classes the object needs to carry out its tasks. In some designs, but certainly not all, it may also include classes that use the object. The back of a CRC card for a class looks like this, --------------------------------------------------- | Class Name | | ----------------------------- | | Variables | | | | | | Methods | | | | | | | --------------------------------------------------- The "Variables" section lists the global variables (object state variables) an object of the class needs to maintain. The Methods section lists the methods of the class. It should simply list the method signatures (method return types, names and parameter lists) and brief descriptions of what the methods do. It should avoid mentioning any details about how the methods carry out their tasks. Typically, each method carries out one of the tasks listed in the Responsibilities section. You may have noticed that CRC cards don't have much room on them. They are rather small 4 by 6 notecards. This enforces a useful rule of class design: A class that has more responsibilities than can be listed on a small note card is too large, and needs to be divided into smaller classes. Of course, software designers have been known to use larger notecards, or to staple several note cards together, but the general rule still applies. Even if you need more room than a 4 by 6 note card provides, you still should design your program so each class has a small, simple set of responsibilities. 1. An Object Design Example -- The PlayBalloon Applet. Here is a specific example of object design using CRC cards, the PlayBalloon Applet from Chapter 10 of "Java for Students" by Douglas Bell and Mike Parr. The actual code for the PlayBalloon Applet is contained in Bell and Parr's book, as is a detailed description of how the code works. By contrast, we shall simply write out CRC cards for the classes used by the Applet, something which Bell and Parr do not do. First, let us write out both sides of a CRC card for the Balloon class. Front side: --------------------------------------------------- | Class Name Balloon | Collaborators | | ----------------------------- | | | Responsibilities | | | Maintain size and position | | | Display itself | | | Move itself left and right | | | Shrink and grow itself | | | ---------------- | | Remark: Combined view and model | --------------------------------------------------- Back side: --------------------------------------------------- | Class Name Balloon | | ----------------------------- | | Variables | | int diameter diameter of balloon | | int xCoord, yCoord x and y coordinates of | | balloon | | Methods | | public void display(Graphics g) display | | itself on graphics object g | | public void left() move left | | public void right() move right | | public void grow() grow itself | | public vod shrink() shrink itself | | | --------------------------------------------------- Note that the list of collaborators on the front side is blank. This is because the Balloon class can be used by any other class, not just the PlayBalloon class. Note that the note card would probably be a little larger than 4 by 6 inches, but it would still be fairly small. The Remark on the front side needs some explanation. As Bell and Parr explain, many programs involving objects follow a pattern involving a "controller", a "view" and a "model". The controller object typically manages user input and sends appropriate messages to the other objects. The view object typically manages the display of the model. The model object typically stores data and does essential calculations with that data. A Ballool object is a view because it takes care of displaying itself, and it is also a model because it maintains the data a balloon needs to know to know how to be a balloon. Next, let us write out both sides of a CRC card for the PlayBalloon class. Front side: --------------------------------------------------- | Class Name PlayBalloon | Collaborators | | ----------------------------- | Balloon | | Responsibilities | | | Display Buttons for | | | user input. | | | When a button is clicked, | | | send appropriate messages | | | to Balloon object. | | | Repaint screen | | | Send display message to | | | Balloon object | | | ---------------- | | Remark: Controller | --------------------------------------------------- Back side: --------------------------------------------------- | Class Name PlayBalloon | | ----------------------------- | | Variables | | Button grow, shrink, | | left, right buttons for user input | | Balloon myBalloon the balloon object | | | | Methods | | public void init() add buttons to Applet | | panel | | public void actionPerformed(ActionEvent event) | | process user button clicks | | and call repaint() | | public void paint(Graphics g) send display | | message to myBalloon object | | | ---------------------------------------------------