Becomes problem

From CSSEMediaWiki
Jump to: navigation, search

The "becomes" problem

You want an object to change its behavior but stay the 'same' object.

An example of this is given in the Frogs design: we have a frog object that must somehow change from egg to tadpole to frog (and ultimately to dead frog), with different ways of moving, but stay a frog. The problem is that inheritance is not dynamic. This is one example of the weakness of 'model the real world' as a design maxim: in the real world we easily accept that the tadpole is the "same" as the adult frog but our notions of type in the real world are much richer and more complex than interface and behaviour. The "becomes" problem is often indicated by type switches (see Beware type switches). See Riel's heuristic "Do not model the dynamic semantics of a class with inheritance".

Personal tools