I am reviewing some code(Java) and making changes based on a business logic flow chart. The current code relies on a large amount of if statement's - something I want to try and move away from. I have been reading about Polymorphism and being trying to wrap my head around how to apply it to my situation. I can make it work for a single level of conditionals, but struggling to extend it further across multiple conditional levels. The code will be executed at run time, with this 'Logic' method being passed the variables from a previous step.
Contrived Example: We have 2 Zoo's, 'Zoo A' and 'Zoo B', and 'Home'. Each of these is a 'Place'. In each Zoo we have 4 'locations', 'North', 'South', 'East' and 'West'. 'Home' only has one location. We want to assign a 'destination' to a person on where they should go based on a few variables. These variables are: 'Place', which correlate to our places (Zoo A, Zoo B, Home). 'Direction', which correlate to our locations, (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'
So If Person X has a Place of Zoo A and a Direction of South they should have a Destination of 'Zoo A South'
I have code that is currently pretty ugly using If statements:
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 could turn this in to nested switches with the variables as ENUMs. But I am trying to avoid the if - else / switch reliance as I have a bad habit of falling into it. I experimented with using a Factory Design to generate Place classes, then used Polymorphism on each location and destination but it started to get overly complicated in my head. Is it even worth moving away from if/switches? Am I just trying to over engineer it?
Any suggestions on how to tackle logic flows like this? Thanks
goNorth(), etc. methods in yourZooclass. I'm not entirely sure that this will solve your problem, though, because I'm not entirely clear on what you are trying to do.