A life comited to learning

How to violate the Interface Segregation Principle

In my last post, I gave away some slides where OO Design Principles are introduced. I have an though about the big-fail that was the J2EE (before JEE5), in terms of violating the Interface Segregation Principle.

If you you are fortunate enough to have experienced the pain of writing applications with J2EE, then you recall that, for example, when you define an Stateless EJB, you have to implement the SessionBean interface.

Well, this is a clear violation of the Interface Segregation Principle, your EJB component have to implement the EJB lifecycle callback methods, even if you don’t need them. Right?

For instance:

public class MyComponent extends SessionBean {
    public void someCoolBusinessMethod() {
         // Business knowledge is coded here
    }

    public void ejbCreate() {} //Don't have anything to do in this EJB lifecycle callback, but the interface forces me to implement the method

    public void ejbRemove()  {} //Don't have anything to do in this EJB lifecycle callback, but the interface forces me to implement the method

   public void ejbPassivate() { } //Don't have anything to do in this EJB lifecycle callback, but the interface forces me to implement the method
...
}

Latest versions of JEE (5 and 6), allows you to bind methods to component lifecycle events declaratively, with annotations of XML. For example:

public void MyOtheComponent {
    public String realBusinessMethod() {
        return "I do really cool stuff";
    }

@PreDestroy
   private void letMeGo() {
   //Any business that should run before the object is left to the GC
   }
}

They fixed it, but very behind time…

jpereira

http://jpereira.eu

View more posts from this author

Leave a Reply

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