Java N Coffee

Monday, June 01, 2009

Annotation Vs. Xml

People talk about putting meta information about a POJO through annotations, but it makes me wonder, how closely related my meta information should be?

Taking dip into Servlet 3.0 specification, annotations here seems to add value to Servlet classes because as responsibility of a servlet class is to process request and send response, it is also close to server end. Many things I'll write in a servlet will be close to container. Even if I write it in annotations or external XML files, I am not concerned about polluting my class with annotation that I'll have to carry everywhere I move my classes.

Now, in hibernate or JPA for instance, my POJO gets polluted with all the meta information that well suits in an external XML file. It keeps my POJO's clean from annotations. If I have to use a portion of the designed model somewhere else, I not bothered what goes in annotatons, its clean and meta information is externalized.

Percenatage of me reusing a servlet code to a domain object is more and makes life easier to keep POJO devoid of all meta information that can cause problem in future.

This is my view, it keeps things cleaner for me to work with and the usuall rant, makes unit testing easy without carrying framewrok burden.

To conclude there should be balance between how much annotation to use when and where.

Labels: , ,

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: , ,

Tuesday, May 08, 2007

Spring


Recently started with Spring framework.
Initially found it difficult to find some good tutorial on it but found few which I am refering right now.



Hope this helps someone starting of with Spring.In case you have few links,tell me too :).

Of these articles,article by Martin Fowler is a good read before you start of with Spring (My Recommendation ;)).

Labels: , , ,