Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Intermediate
Nov 20, 2000

Use an Adapter Class To Provide Empty Implementations For Javabean Interfaces


Like many high-level, abstract, and portable concepts, EJB spec has its downsides. 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 empty or 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 of implementing 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!
Slava Vaseykin
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap