The business logic layer for J2EE applications combines data with business rules, constraints, and activities. I usually separate them from DAOs, VOs, and deployment wrappers, such as enterprise beans or Web services, to maximize the possibility of reuse. It's common for business objects (BOs) to use and coordinate the activities of multiple data access objects. Figure 13.1 illustrates how business objects function in a layered architecture.
Business objects are instantiated and invoked by other business objects or classes in the deployment layer, such as enterprise beans or Web services. They often instantiate and use classes in the data access layer, such as those discussed in the previous two chapters. This chapter provides coding guidelines used for business objects and presents examples taken from ProjectTrak.
As an illustration, consider a business object in a purchasing application. Class PurchaseOrder is in the business logic layer and is used by the deployment layer to provide the ability to view and submit purchase orders. PurchaseOrder has the following methods:
public PurchaseOrderVO getPurchaseOrderVO ();
public void setPurchaseOrderVO(PurchaseOrderVO po);
public void setPurchaseOrder (int ordered);
public void record ()
throws InsufficientCreditException;
public void cancel ();
public PurchaseOrderVO[] getCustomerPurchaseOrders(
String custId);
Business objects are responsible for transaction management. Because business objects are the only classes that understand context, they should determine where transactions begin and when they are committed or rolled back. Business objects understand, for instance, which database inserts, updates, and deletes are necessary to perform a business function, such as adding a customer, defining a purchase order, or deactivating a retail product. These business functions can be saved together to form a composite transaction or can be issued individually.