Java N Coffee

Wednesday, May 06, 2009

old evil new

Well its been long I have written in the Java space. Lately I realized, I had an old foe disguised as a true friend lurking by my back, making mockery of my work :X. This friend of mine is "new", surprised, hmmm well how?

Every time I used his service, I had to pay the price. Let me show you.

public interface Sexy{
String wink();
}

public class Handsome implements Sexy {

// properties

String wink(){
BeautyFinder(this).getBeauty().sendWink();
}
}

Somewhere in the middle of heat.... working hard

class DateMaker{

Sexy sexy;

public void setSexy(Sexy sexy){
this.sexy = sexy;
}

public Sexy getSexy(){
return this.sexy;
}

public boolean fixDate(){
//complex logic for finding a beauty ;)
}

}

// the MAIN or core business process logic

public static void main(String[] args){
Sexy me = new Handsome();
DateMaker dateLord = new DateMaker();
dateLord.setSexy(me);
if(dateLord.fixDate()){
me.wink();
}
}


The problem might not be evident, but as the job of DateMaker increases coz of popularity ;), need arises to handle different types of sexy beings..living .. non living any thing..The creation of relevant object becomes akin to feeding data for information.

How long can we rely on new XXX(); all over code base, and the famous cliche pain in ass "change is the only constant" sooner or latter it will become hell. A part of it can be solved by using patterns like facotry and abstract factory pattern, but not long so.

If we can move out this creation burden from our code base and delegate it to some other source. We can achieve much better flexibility and change requests.

Needless to say for Java world there is Spring and another cool framewrok Guice for the dependency injection.

Labels: , ,