Categories
technology

Static substitution (Fowler Style)

Martin Fowler has a neat little article on refactoring class statics using instance variables. Most languages can’t support polymorphism for static methods. e.g.

class A{
public void doInitStuff() { /*do stuff necessary for static init of B objects*/};
} ...
}
class B extends A{
public void doInitStuff() { /*do other stuff necessary for static init of B objects*/};
} ...
}
...
A a = new B();
A.doInitStuff(); /* but I'd quite like to polymorphically call B.doStuff(); Actually, could be trouble! */

Martin’s solution is very elegant.

Leave a Reply

Your email address will not be published. Required fields are marked *