Frogs design

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
(Attempted answers)
Line 15: Line 15:
  
 
* [[Riel's heuristics]]
 
* [[Riel's heuristics]]
 +
** 2.1 [[Hide data within its class]]: The ''AdultFrog'' class exposes an array of eggs it has laid.
 
** 2.9 [[Keep related data and behavior in one place]]: ''Frog'' and ''FrogBrain'' are separate classes.
 
** 2.9 [[Keep related data and behavior in one place]]: ''Frog'' and ''FrogBrain'' are separate classes.
 
** 3.5 [[Interface should be dependent on model]]: The ''Move'' interface contains a method called ''display''; the model is dependent on the interface
 
** 3.5 [[Interface should be dependent on model]]: The ''Move'' interface contains a method called ''display''; the model is dependent on the interface
 
** 3.9 [[Avoid verb classes]]: The ''Move'' interface is a verb. It could be called ''Movable'' or similar (although that name is still not great for other reasons).
 
** 3.9 [[Avoid verb classes]]: The ''Move'' interface is a verb. It could be called ''Movable'' or similar (although that name is still not great for other reasons).
 +
** 3.10 [[Agent classes irrelevant]]: The ''Biologist'' class is an agent.
 
** 5.14 [[Avoid becomes]]: A frog will at various stages be an egg, tadpole and adult frog.
 
** 5.14 [[Avoid becomes]]: A frog will at various stages be an egg, tadpole and adult frog.
 +
*** Solution: State or strategy pattern
 
** 5.1 [[Inheritance for specialization]]: A toad is not a frog; it's [[Avoid_inheritance_for_implementation|inheritance for implementation]].
 
** 5.1 [[Inheritance for specialization]]: A toad is not a frog; it's [[Avoid_inheritance_for_implementation|inheritance for implementation]].
 
** 5.17 [[Avoid no-op overrides]]: The ''hop'' and ''swim'' methods of ''Egg'' do nothing.
 
** 5.17 [[Avoid no-op overrides]]: The ''hop'' and ''swim'' methods of ''Egg'' do nothing.
  
 
* The biologist should have the saute method, not the frog itself.
 
* The biologist should have the saute method, not the frog itself.

Revision as of 02:14, 20 July 2009

An OO model of frogs. This beautiful design appeared in the 2004 427 exam.

Frogs.gif

Design notes

  • This design models the lifecycles of Frogs.
  • A Frog begins life as an Egg, grows into a Tadpole and then an AdultFrog.
  • A FrogBrain belongs to a Frog and manages its behaviour.
  • There is one Biologist, Igor, who sometimes moves Frogs around. Igor has been known to fry their legs in white wine.
  • Toads don’t have the same lifecycle as Frogs, but they look and act a lot like Frogs.
  • Toads eat AdultFrogs.

Broken heuristics

  • The biologist should have the saute method, not the frog itself.
Personal tools