3 ms·
When Polymorphism Fails
- jsteele 17y agoThat example of coding an elfLikesMe attribute for 150 monster types, why to code it at all? My first hunch would be to use SQL table for monster attributes, then adding new attribute would be equivalent to adding a new SQL field, or table?
- thwarted 17y agoYou get the easy part: you need to decide what classes Willie is to construct the tree with. ... I won't give away the answer, but a Standard Bad Solution involves the use of a switch or case statment (or just good old-fashioned cascaded-ifs). A Slightly Better Solution involves using a table of function pointers, and the Probably Best Solution involves using polymorphism. If the requirement was to show which classes should be used and the result is a series of conditionals or function pointers (even if they are wrapped in a single "operator" class), I'd say the requirements have not been met. To me, "classes" implies doing it with a class hierarchy or polymorphism.