This discussion is archived
4 Replies Latest reply: Feb 29, 2012 10:08 AM by 796440 RSS

Replace Ifs using design Pattern

920854 Newbie
Currently Being Moderated
Hi Guys,

I have an class that receives an error code and set an error message associated. So, to avoid nested ifs, im looking for a solution maibe using a Pattern.

if(returncode.equals("052")){
     errors.add("error", new ActionError("error.msg", applicationResources.getMessage("errors.myerror52")));

if(returncode.equals("055")){
     errors.add("error", new ActionError("error.msg", applicationResources.getMessage("errors.myerro55")));

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

For each new errorCode and your respective message, i need to add a new if in the class....

I accept some suggestions to make this better !!

Thanks a lot !
  • 1. Re: Replace Ifs using design Pattern
    796440 Guru
    Currently Being Moderated
    There are a number of approaches you could take. The validity and relative merits of some of them depend on whether the return code is truly a string, or a number that's formatted as a string; on whether you are always simply calling errors.add(), just with different arguments, or if you need to take completely different actions in some cases; on how many return codes there are; on how often codes will be added, changed, or removed.

    1. A Map from return code --> object encapsulating the args to errors.add()

    2. Command Pattern, which might be realized as a Map of return code --> instance of a class implementing how to handle that code, or return code --> Class object or class name, which then gets instantiated and the new object executed. In either case, the object that does the processing will implement some interface that you define.

    3. An array whose index is the return code and whose value is an object, Class object, or class name that handles that particular code.

    4. An enum, with each enum value having either a different set of fields to be passed to errors.add() or a different implementation of a method, if you don't simply call errors.add() for each code.

    5. A switch statement on the return code, either as an int or as an enum, or, if you're on Java 7, as a String.

    Somewhat independently of which of the above approaches you take, you need to decide whether you will hardcode the code --> action mapping, or if you want to put it in a config file or database.
  • 2. Re: Replace Ifs using design Pattern
    796440 Guru
    Currently Being Moderated
    Also, note that you shouldn't be thinking, "I wanna use a design pattern for this!" That's not the point of design patterns. Rather, you should study up on the common patterns and what problems they solve, and learn to recognize when your situation potentially fits one of them. Then when you come across a problem that's a potential fit, you can say, "Aha! I think others have solved this before, and here's how I think they did it. Let me review that pattern to determine its suitability for this situation."
  • 3. Re: Replace Ifs using design Pattern
    920854 Newbie
    Currently Being Moderated
    Hi Jverd, thanks for the suggestions ...

    First, Im using Java 1.4 its an Legacy application. :( .... And the errorCode is a String.

    Inittialy in think to use the Resources.Properties to keep the messages, but i dont know if its necessary altought im using an Typesafe Enum :

    //The typesafe enum pattern
    public class ReturnCode {
    private final String errorMessage;

    public static final Suit Error52=new ErrorCode("Some message or resources.properties here");
    public static final Suit Error53=new ErrorCode("Some message or resources.properties here");
    public static final Suit Error53=new ErrorCode("Some message or resources.properties here");


    private ErrorCode(String name){
    this.name =name;
    }
    public String toString(){
    return errorMessage;
    }

    }
  • 4. Re: Replace Ifs using design Pattern
    796440 Guru
    Currently Being Moderated
    917851 wrote:
    Inittialy in think to use the Resources.Properties to keep the messages
    None of my suggested approaches excludes that. There are a couple of independent parts. One part is mapping from the return code to the action. Another part is providing the string(s) for a given action.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points