The question of architecture, but quite often I come across this, and rarely see a good solution.
Suppose we have a certain interface:
CoffeeMachine { doCoffee(); } And we have a whole bunch of implementations, LateCoffeeMachine, CapuchinoCoffeeMachine, etc. And everything is going well, only we suddenly need to output the machine parameters somewhere.
view(CoffeeMachine machine); But for each its own parameters, one operates only the amount of coffee, the other can also sugar, and the third also milk.
It seems to be the easiest and most logical option to change the interface of CoffeeMachine:
CoffeeMachine { doCoffee(); view(); } That's just this approach violates the principle of Single Responsibility, because the machine is now responsible not only for making coffee, but also for displaying itself, so you can add methods to infinity.
The second option that personally comes to me in the head is a visitor pattern. Which in my opinion is quite cumbersome.
The third option that comes to my mind is through the factory, to create objects in pairs. However, this method leads to the appearance of Generic (although it is possible without them, but type safety) and, again, some problems.
Viewer<T> { view(T); } Factory { createMachine() createViewer(); } In general, I myself can offer a few more solutions, but I would be interested to look at someone else's opinion and thoughts of others on this matter. Maybe I'm wildly stupid and have an elegant solution or a switch'a can not be avoided? )