开发者

Design Pattern Need to remove a series of If.. elses in object initialization

I'm writing an application in which there will be multiple departments and for each department there will be separate processing class. Each department and department processing is represented by separate class.

so, now main method in java looks more like series of if else ladder.

Is there any way of making it more flexible so that i can add more departments and their processing classes late开发者_开发技巧r without modifying the originally written class much ??

I've read about Abstract Factory patterns but is there any other solution than it ??


Create an interface to hide the departments, like "Department". Write your main method like:

main() {
    String criteria = ...; // this is how we choose the department to use,
                           // and you probably don't want to use a String
                           // but some other, more expressive type
    for (Department department : departments) {
        if (department.supports(criteria)) {
            department.doWhatever();
        }
    }
}

Then use dependency injection to populate the departments collection. Depending on how you set it up, it could be pure configuration.


The Abstract Factory pattern is probably best suited to the scenario that you described. You will need a heirarchy of Department Processors and a matching heirarchy of Department classes. The Abstract factory will produce a concrete factory for each pair based on some discriminator, process the department for you and return the Department object.

The rest of your application does not need to be aware of the differences in the creation of the department objects since it will use the FActory to get a department simply passing the appropriate discriminator.

Adding a new department will require the new department clas, the processor class and updating the factory logic.


Alternatively, if the structure of the departments are all the same but the processing is different you may consider using something like the Strategy pattern. In this case you have a single Department, but the deccision about processing will be made for the Strategy instead. So the appropriate Strategy is injected into the department and the department's behaviour differs accordingly.


There is a whole web site devoted to the topic. You want to use polymorphism and possibly reflection depending on what you are trying to do.

http://www.antiifcampaign.com/


You can create enum with field for each state. Then create abstract method init() in the enum and implement it for each member.

In your code you can get state from for example properties file and then say State.valueOf(state).init()


Using the "Factory" or the "Abstract Factory" design pattern would be the first step. That would take the object initialization logic out of your main code and isolate it in the factory classes. In that way, your main code would be closed for modification and open for extension (a.k.a. Open Closed Principle). The changes you make will be isolated in the factory classes.

If you want to take this one step further, you may also use reflection. An example would be:

static Object createObject(String className) {
  Object object = null;
  try {
      Class classDefinition = Class.forName(className);
      object = classDefinition.newInstance();
  } catch (InstantiationException e) {
      System.out.println(e);
  } catch (IllegalAccessException e) {
      System.out.println(e);
  } catch (ClassNotFoundException e) {
      System.out.println(e);
  }
  return object;
}

With this code, you can simply create an object like this:

public static void main(String[] args) {
    NewDepartment dep = (NewDepartment) createObject("yourpackage.NewDepartment");
}

Of course, there is a trade-off when you use reflection. That is up your discretion to use it or not.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜