Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command pattern with too many classes

Our legacy code has a long code of if else blocks that depend on events and object type

if(event == A && objectType == O1){
.....
}
else if (event == A && objectType == O2){
 ....
}
else if (....)
....
....

With more and more conditions introducing, I was thinking of replacing this logic with Command pattern for each condition. But the number of classes required would be (no. of events) * (no. of object types). Is there any simpler way to refactor this code?

like image 719
sidgate Avatar asked Jun 14 '13 11:06

sidgate


Video Answer


1 Answers

Create a class enclosing event and objectType, make it implement .equals() and .hashCode(). Create a generic class for each execution block too.

Then you'll be able to use a Map and a simple lookup will return what is needed to execute.

like image 71
fge Avatar answered Oct 07 '22 05:10

fge