devxlogo

Use an Adapter Class To Provide Empty Implementations For Javabean Interfaces

Use an Adapter Class To Provide Empty Implementations For Javabean Interfaces

Like many high-level, abstract, and portable concepts, EJB spec has itsdownsides. One of them is the very generality that makes beans so portable.Interfaces javax.ejb.EntityBean and javax.ejb.SessionBean declare quite a few methods and most of them are not used by simple bean implementation. Yet developers must provide empty implementations for all these methods. As with many generic interfaces (Swing, etc.), one possible solution is an adapter class. This class would “implement” the respective interface by providing emptyor default implementations for all the methods declared in the interface. For instance:

  	public class EntityBeanAdapter implements javax.ejb.EntityBean {		EntityContext myContext;public void ejbActivate() throws RemoteException {}public void ejbLoad() throws RemoteException {}public void ejbStore() throws RemoteException {}public void ejbPassivate() throws RemoteException {}public void ejbRemove() throws RemoteException {}public void setEntityContext(EntityContext _ctxt) {			myContext = _ctxt;		}public EntityContext getEntityContext() {			return myContext;		}public void unsetEntityContext() {			myContext = null;		}	}

size=3>
This process would be similar for the SessionBean.
Now, the implementations can just extend these adapters instead ofimplementing the EntityBean and SessionBean interfaces directly. Like:

 public class AccountBean extends EntityBeanAdapter {		... the code	}

size=3>
If any of the callback methods are needed, they can be overridden here.CAUTION: As with all adapters, if you misspell the method while attempting to override it, the compiler won’t bark, but the methods will never get invoked. So, check your spelling!

devx-admin

Share the Post: