I have an enumeration with the commands Play
, Stop
and Pause
for a media player. In two classes I do a switch-case over the received commands. The player runs in a different thread and I deliver the commands in a command queue to the thread.
If I generate class diagrams the enumeration has dependencies all over the place. Is there a nicer way to deal with the commands? If I would change/extend the enumeration, I would have to change several classes. (Its not super important to keep the player extensible, but I try to write nice software.)
Best Answer
A "nicer" way is to have three methods, one per command.
There is no need to collapse all those commands into one method and to use a switch later. Since those commands do different things, they deserve their own methods in the interface.
Instead of:
you would have:
Why?
Your current implementation using a switch will:
Play
,Stop
andPause
methods.In the first case, you break the rule which says that a method should do one and one only thing.
In the second case, KISS: don't write a method you really don't need and which doesn't bring anything useful to the API.