0

Consider the following code prev JAVA 7 code

initialProcessing(sourceObject, errorObject)
if (errorObject.errorCode ! = null or not blanks)
   stop processing
else
    Do procedure A (modifiedSourceObject, errorObject);

if ( errorObject.errorCode ! = null or not blanks   )
  stop processing
else 
   Do procedure B (anotherObject, errorObject);
if ( errorObject.errorCode ! = null or not blanks   )
  stop processing
else 
    Do procedure C (someotherObject, errorObject)
.............
.........

The common thread among these if - else is the checking of the error code. I am not familiar with design patterns but is there any pattern that I can use to refactor this code. For reasons outside my scope, preference is not given to creating SUBCLASSES. I know we can use Strategy pattern or something in those lines if subclasses were to be used. My dilemma is to refactor the code without creating subclasses. Can someone suggest a design pattern for the above code.

Forum Member
  • 163
  • 1
  • 6
  • 15
  • The Command Design Pattern is an obvious thought. – Hovercraft Full Of Eels Apr 10 '16 at 12:34
  • Also please have a look at [these similar questions and their answers](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=java+design+pattern+replace+if+else+site:http:%2F%2Fstackoverflow.com%2F). Good luck! – Hovercraft Full Of Eels Apr 10 '16 at 12:41
  • Please do not mark as duplicate. I have gone over the other similar questions but they involve subclasses or enums. My question does not fall under the category for enums. Also I do not want to use subclasses. I am looking for alternative solutions. – Forum Member Apr 11 '16 at 13:11

0 Answers0