I have a class that handles the state of a response, called StockResponse
.
The code has multiple ifs to handle each state of the stock. Most of the cases has a default behaviour, but some conditions need additional if-else inside to handle correctly the response.
The conditions are becoming hard to maintain, and looks bad to have a huge method with many if-else conditions.
I am guessing which will be the best way to handle them. Code now looks like this:
public Result ProcessStockState(StockResponse response)
{
if(response.state==StockStateEnum.Ok)
{
if(response.Sender==Sender1){ /* code....*/ }
else if ( response.Sender==Sender2){/*code ...*/ }
else {/**default handling code...*/}
}
else if(response.state==StockStateEnum.Unkown)
{
if(response.Sender==Sender5){ /* code....*/ }
else if ( response.Sender==Sender7){/*code ...*/ }
else if ( response.Sender==Sender10){/*code ...*/ }
else {/*default handling code ...*/}
}
else if(response.state==X)
{
/*Multiple conditions that ussually differ from the other ifs*/
/*It always have a default behaviour*/
}
}
Which design/pattern you would recommend me to avoid that cascading if-else ?