suppose have item class base class of composite pattern.
i have 2 specific subclasses movie , music containing different data :
- movie : director, producer, list of actors ...
- music : chanteur, number of record sold ...
if want access movie data (for example) in client code, need downcast item corresponding because getproducer() not method of item class.
i stuck between 3 different approach :
- put subclass methods in item interface
- downcast item in client code
- use object extract data different item (using kind of visitor pattern)
the third proposition seems interesting because hides item subclasses client, don't know how let client update item (for example change producer of movie).
the item subclasse types supported application grow through releases, , want design facilitate type additions.
maybe totally going in wrong way , there different , better solution.
option 1 bad idea, because getproducer() not belong item.
option 3 work if you'd proved setters in addition getter methods planning implement there.
option 2 sounds reasonable me. casting common task in polymorphic solutions , in case adequate.