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;		}	}

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	}

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!

See also  How College Students Can Shape the Future of Tech Responsibility

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist