java - Head First OOA&D - implementing properties in a hierarchy tree of entities -


i reading book "head first ooa&d". there examples of code. have problem accepting next solution:

if there hierarchy of entities in system, there proposed use map of properties instead of writing entire tree of classes.

for example: unit map of properties:

public class unit {     private unittype type;     private map<string, object> properties; } 

where unittype enum.

tree of units:

public class unit {     private int healthvolume; }  class soldier extends unit{     private int strength; }  class tank extends unit{     private int armoramount; } 

yep, there arguments in first case there possibility add new units no effort , additional classes. example in case have 100 unit types:

first case: have write 1 class.

second case: have write @ least 101 classes.

but string map<string, object> don't seem nice @ because hard coding of properties name isn't style. adding enum improve code, there object seems strange when there many generics around.

do have ideas how can design improved? thank in advance.

but string map<string, object> don't seem nice @ because hard coding of properties name isn't style.

you right , false. think author(s) want think design , in cases adequate not build longish class hierarchy. if need strict typing , access properties getter/setter style using properties map bad idea. if customer needs flexible, easy extent hierarchy not containing complex inheritance trees property map fast way productive.

and using getter/setter complex type hierarchy hard coded property names!


Comments