One of the problems was the large number of branches in a factory method so Nelutzu has refactored it in a very nice way:
class FooFactory {
private static HashMap items = new HashMap();
static {
items.put("foo1", new Foo1());
items.put("foo2", new Foo2());
items.put("foo3", new Foo3());
}
public static Foo getFooProduct(String fooName) {
return items.get(fooName).getClass().newInstance();
}
}
Voila, no if or switch while creating the objects. Nice work, Nelutzu!
No comments:
Post a Comment