Avoid nesting if else / switches – Java
I'm reviewing some code (Java) and making changes according to the business logic flowchart The current code relies on a lot of if statements - I want to try and stay away from it I've been reading about polymorphism and trying to focus on how to apply it to my situation I can make it applicable to a single condition level, but try to further expand it at multiple condition levels The code will be executed at run time, and the 'logic' method will pass the variables of the previous step
Controlled example: we have 2 zoos, 'zoo a' and 'zoo B', and 'home' Every is a 'place' In each zoo, we have four 'locations',' North ',' South ',' East 'and' West '‘ S only one location We want to assign a "destination" to a person based on some variables These variables are 'place', which is related to our place (zoo a, zoo B, home)‘ Direction 'is related to our position, (n, s, e, w) flow chart:
|----- | 'HOME' |Place?| ----- > *destination = 'home'* |----- | Zoo A | Zoo B |---------------|----------------------------------------| |----------| |----------| |Direction?| |Direction?| |----------| |----------| | North | North ----------- *destination = 'Zoo A North' ----------- *destination = 'Zoo B North' | East | East ----------- *destination = 'Zoo A East' ----------- *destination = 'Zoo B East' | South | South ----------- *destination = 'Zoo A South' ----------- *destination = 'Zoo B South' | West | West ----------- *destination = 'Zoo A West' ----------- *destination = 'Zoo B West'
Therefore, if character X has zoo a and south direction, they should have a destination of 'zoo a South'
The code I use the if statement is currently very ugly:
if(Place = 'HOME') destination = 'HOME' if(Place = 'Zoo A') if(Direction = North) destination = 'Zoo A North') if(Direct = East) destination = 'Zoo A East') ... if(Place = 'Zoo B') if(Direction = North) destination = 'Zoo B North') if(Direct = East) destination = 'Zoo B East') ...
I can convert this to a nested switch with the variable enums But I try to avoid if – else / switch dependency because I have a bad habit I tried to use factory design to generate the place class, and then use polymorphism on each location and target, but it began to become too complex Is it worth staying away from if / switches? Am I just trying to over design?
Any suggestions on how to handle logical flows like this? thank you
Solution
This can be modeled as follows:
>Use the root class place of the method calculatedestination (person) Places can include other places. > Create place subclasses for zoo and zooquadrant (of course, because they are actual locations). > The person object has values for currentplace and currentdirection
You will now instantiate the objects of these classes to represent your situation:
zooA = new Zoo("ZooA"); zooA.addChild(new ZooQuadrant(Direction.soUTH)); ... so on for every quadrant ... ... same for zooB ... home = new Place("Home"); world = new Place("World"); world.addChild(home); world.addChild(zooA); world.addChild(zooB);
When you want to get the destination, you will call the world calculateDestination(myPerson)
Calculatedestination (person) is a polymorphic method Each level in the inheritance hierarchy overrides the class according to its specific semantics
>Place will have a general implementation that will test whether the person instance is currently on the node (by testing the person value of currentplace). If not, it will call calculatedestination on each child node and return it. > The zoo will need to check currentplace = = this, and if so, call calculatedestination on each quadrant and combine any positive results with itself to return this name quadrantResult. > Each zooquadrant only needs to check whether the currentdirection is equal to its own direction and return a value accordingly
Note: This is just to show how polymorphism may work, and there may be a better implementation In addition, here we use polymorphism and recursion, which are independent
Edit:
As for whether you need to add complexity, it depends on! Here, we use a simple example of a very small object graph Once you have dozens of zoos, you must add more quadrants to these zoos, or you need to make additional decisions (if each quadrant has sub parameters). Nested if else if methods (programs) are really hairy and fast, while object-oriented methods are still maintainable and understandable
Like everything else, if you foresee that the decision will become so complex, use the OO method Otherwise, keeping it simple is better than beauty every time: use the right tools to solve problems