Many business processes involve complex manipulations of business classes. Business classes often participate in multiple business processes or workflows. Complex processes that involve multiple business objects can lead to tight coupling between those classes, with a resulting decrease in flexibility and design clarity. Complex relationships between low-level business components make clients difficult to write.
The Session Facade pattern defines a higher-level business component that contains and centralizes complex interactions between lower-level business components. A Session Facade is implemented as a session enterprise bean. It provides clients with a single interface for the functionality of an application or application subset. It also decouples lower-level business components from one another, making designs more flexible and comprehensible.
Fine-grained access through remote interfaces is inadvisable because it increases network traffic and latency. The "before" diagram in Figure 1 below shows a sequence diagram of a client accessing fine-grained business objects through a remote interface. The multiple fine-grained calls create a great deal of network traffic, and performance suffers because of the high latency of the remote calls.
|
| Figure 1. Sequence diagram before and after adding Session Facade |
Introducing a Session Facade, as shown in the "after" diagram
in Figure 1, decreases network traffic and
latency, because all access to fine-grained business
objects is local. The Session Facade also acts as
a Mediator [
GHJV95
] between the business objects,
decoupling their APIs from one another.
Core J2EETM Patterns
The admin facade.
The sample admin application of the Pet Store enterprise is responsible for management functionalities of the Pet Store order processing workflow. The admin application is a Swing client with JavaTM Web Start that communicates with the backend order processing center application to approve large purchase orders and view financial data. To interact with the order processing center application, the admin application uses the Session Facade pattern in the interface
OPCAdminFacade
,
whose interface appears below.
public interface OPCAdminFacade extends EJBObject {
public OrdersTO getOrdersByStatus(String status)
throws RemoteException, OPCAdminFacadeException;
public Map getChartInfo(String request,
Date start,
Date end,
String requestedCategory)
throws RemoteException, OPCAdminFacadeException;
}
The admin facade's implementation class is
OPCAdminFacadeEJB
.
The admin application is a client of the OPCAdminFacade
in the order processing application.
A structure diagram of how the Session Facade interacts with other classes in the
order processing application appears in Figure 2 below.
The greyed-out class in the diagram indicates the transfer object
created by the facade class; see the
Transfer Object pattern for details.
|
| Figure 2. OPCAdminFacade provides an interface to complex interactions between components |
The key point to notice here, is that the client only interacts
with the OPCAdminFacade, and the other Enterprise Beans
are hidden behind the facade. So the client has a simple interface
and is not exposed to the complexity of the application logic that
occurs to process the request. One of OPCAdminFacade's methods,
getOrdersByStatus, builds a summary of purchase information
for display by the sample application's admin client. This method uses a
ProcessManagerLocal
stateless session
bean and
ManagerLocal
local entity beans and iterates over a collection of
PurchaseOrderLocal
entity beans of the requested status. The method creates a serializable
Collection
of
OrderDetails
transfer objects that describe
PurchaseOrderLocal beans found. (See the
Transfer Object
pattern for details). The method then returns the collection of objects
to the client.
ShoppingClientFacadeLocal
is a local stateful session bean that centralizes
the services that the Web site provides to shoppers. Its
local interface appears below:
public interface ShoppingClientFacadeLocal extends EJBLocalObject {
public ShoppingCartLocal getShoppingCart();
public void setUserId(String userId);
public String getUserId();
public CustomerLocal getCustomer() throws FinderException;
public CustomerLocal createCustomer(String userId);
}
The shopping facade provides a single interface to the client
for all client shopping functionality: the shopping cart,
storing the user's id, and finding and creating the
Customer data associated with the user.
The facade is a stateful session bean because it stores
information (such as the user id) that is specific to
an individual user in the context of a session.