An app I'm working on is designed with MVC. The components often interacts with each other by passing int constants, which then have to be interpreted with if
statements. I'm wondering if this is considered bad OO design. (Not specifically with MVC, but in general).
For example:
When the user clicks a specific button, the controller is told by the view controller.somethingHappened(Controller.SOME_INT_VALUE);
The view uses a constant int value in the controller as a message to tell what happened. The controller then has to have a lot of if
statements to interpret the message from the view. E.g.:
public void somethingHappened(int message){
if(message == AN_INT_VALUE) doSomething();
if(message == ANOTHER_INT_VALUE) doSomeThingElse();
if(message == A_DIFFERENT_INT_VALUE) doAnotherThing();
}
As a result the controller passes another int value into the model, as a message to do something. I.e. model.doSomething(Model.SOME_INT_VALUE);
.
Which forces to model to also have a bunch of if
statements:
public void doSomething(int message){
if(message == AN_INT_VALUE) doSomething();
if(message == ANOTHER_INT_VALUE) doSomeThingElse();
if(message == A_DIFFERENT_INT_VALUE) doAnotherThing();
}
This results in both the controller and model having a lot of if
s to interpret the messages the receive.
Is this considered ugly and/or bad design? Should I try to pass actual objects into methods, or make more specific method calls - instead of passing constants that have to be interpreted with a bunch of if
s? Or is this practice of using a lot of constants as messages considered reasonable?
If it is considered bad design, how can I avoid this?
(Please note: For the purpose of this question I'm also referring to enums).