What class structure allows for a base class and mix/match of subclasses? (Similar to Users w/ roles)

Posted by cdeszaq on Programmers See other posts from Programmers or by cdeszaq
Published on 2012-04-02T20:41:22Z Indexed on 2012/04/02 23:41 UTC
Read the original article Hit count: 130

Filed under:
|

I have a set of base characteristics, and then a number of sub-types. Each instance must be one of the sub-types, but can be multiple sub-types at once. The sub-types of each thing can change. In general, I don't care what subtype I have, but sometimes I do care.

This is much like a Users-Roles sort of relationship where a User having a particular Role gives the user additional characteristics. Sort of like duck-typing (ie. If my Foo has a Bar, I can treat it like a ThingWithABar.)

Straight inheritance doesn't work, since that doesn't allow mix/match of sub-types. (ie. no multi-inheritance).

Straight composition doesn't work because I can't switch that up at runtime.

How can I model this?

© Programmers or respective owner

Related posts about class-design

Related posts about groovy