Identify Common Logic

Many applications will have common logic that appears on many pages. For example, an e-commerce system may have a shopping cart, and have many different places where an item can be added to the cart.

In many cases, the logic for this can be centralized in the visit object. The visit object may implement methods for adding products to the shopping cart. This could take the form of Java methods, component listeners.

In addition, most web applications have a concept of a 'user'. The object representing the user should be a property of the visit object, making it accessible to all pages and components.

Most Tapestry applications will involve some interaction with Enterprise JavaBeans. The code to lookup home interfaces, or to gain access to ession beans, is typically located in the visit object.

Listener code, on various pages, will cast the visit object to the appropriate actual class and invoke methods.

The following example demonstrates this idea. Visit is a hypothetical visit object that uses EJBs.

public void exampleListener(IRequestCycle cycle)
{
  Visit visit; 1
  ISomeHomeInterface home;

  visit = (Visit)getVisit();

  home = visit.getSomeHomeInterface();

  try
  {
  	// etc.
  }
  catch (RemoteException ex)
  {
    throw new ApplicationRuntimeException(ex);
  }
}

1

Each application can freely define the type of the visit object, and its is common to call the class "Visit". Another option is to create a subclass for the engine and store home interfaces there.