Monday, May 02, 2005

Configurator Pattern

Recently, I was refactoring some code which I was not happy with at all: there were many 'if instance of...' statements and I was looking for a way to get rid of them. Since time pressure was high (what a lame excuse), I coded faster than my mind could think about the problem. In the end, I had invented the Configurator Pattern, which can be applied to almost every problem:
public interface Configurator {
Object configure(Object inSource)
}
It allowed me to get rid of the 'instance of' checks and all I had to do is write some detailed JavaDoc for each implementation of the interface to make sure it's clear what the method expects as input parameter and what it returns as result.

Once I was done, I finally took the time to look at the design. I liked it for about 20 seconds (I'm not that fast). After this period of relieve about having found a solution, I entered a much longer period of being puzzled: how could I have gotten so carried away and end up with such an odd solution? I still don't know and I'm still uncomfortable about it... But it was only 9pm, therefore, plenty of time to make history undone and to come up with something to be proud of - which I did, after sitting back and thinking more deeply about what the problem and the context are and only then implementing a satisfying solution I could come up with (it turned out ot be the Visitor pattern, but that's not the essence of this story).

1 Comments:

At 03 May, 2005 03:33, Blogger rahel luethy said...

i'm continuously refactoring away even better variants of this anti-pattern:

public class Configurator {
private Object object;

public Configurator(Object object) {
this.object = object;
}

public void configure() {
// do everything and more...
}

}

don't even think about testability ;-)

 

Post a Comment

<< Home