[Tutor] OOP design
alan.gauld@bt.com
alan.gauld@bt.com
Thu, 25 Apr 2002 18:02:49 +0100
> When using OOP practices for the first time, but understand
> how to use it. How do you design how it should work?
The easiest way to start is using CRC cards.
C = Class
R = Responsibilities
C = Collaborators
Use a postit note (or index card) and draw a big 'T'
shape such that there is a top line and two panels
on the postit.
ASCII art example:
-----------------------------------+
class name |
-----------------+-----------------+
responsibilities | collaborators |
| |
-----------------+-----------------+
In the first line(above the T) write the class name.
Then in the Left hand box write the classes responsibilities
- what the class is supposed to do(forget data at this point
- you should NEVER design classes based on data.
Finally decide which other classes you need to collaborate
with to achieve the responsibilities. Write these down in
the Right hand panel.
You can then arrange the postits on a whitboard or table
top and work through some scenarios for your system.
Imagine the objects as living actors on a stage, passing
information between each other to accomplish a task
- in fact invite some friends around to play the parts
of the objects - can be fun! :-)
This will determine what messages need to be passed, what
data needs to be stored to meet the responsibilities(and
which objects should hold (ie own as a responsibility)
that data and what parameters should be passed in the
messages.
This is a fun and simple way of designing OO systems and
works for small to medium sized jobs(say 20-30 top level
classes).
You can then document the outcome of the excercise in a more
compact way using UML if you wish.
Alan g.
Author of the 'Learning to Program' web site
http://www.freenetpages.co.uk/hp/alan.gauld