Bodies with Color
The idea: Make a separate class “Color” and let the Body class have a pointer to an object of class “Color”.
- we can get a “standalone” Color class right off the shelves (the Web in this case);
- we can use the same Color class for other objects as well;
- we can replace the Color class in the future with a better one with very little work;
- we can now change the color of an object at any time;
- we can extend any of the classes independently.
In general, the association (“has a”-relationship) allows for much more flexibility both at compile time and at runtime, and makes the classes you write much more re-useable.
This is the toolbox approach:
Make simple classes which do one job well, and use them in many different contexts.
Let’s make some colorful bodies...
“The use of associations and shallow inheritance trees are the hallmarks of good OO programmers.”