Most Read This Week
Componentizing Applications with Layered Architecture
Componentization facilitates modularity and easy maintenance
Mar. 3, 2013 02:15 PM
A component is a reusable software entity that is developed and deployed independently. Component based software development has many architectural advantages. In the previous article Componentizing a Monolithic Application in Java, we learnt the need for componentizing applications for getting the benefits of reusability and modularity. In this article let us look at how multi layered application can be componentized. We take the example of a multi-layered POS (Point-Of-Sale) application and understand how the application can be componentized at various layers like presentation, business and persistence layers.
Point of Sale - An Example Application
Existing Implementation of POS
Figure 1 - Existing POS Application Architecture
Figure 2 - Objects in POS Application Layers
In the layered architecture of POS, Views and Controllers belong to the Presentation layer. Information exchange across the layer borders is enabled by the Model objects carrying business domain data. The multiple layers of POS are shown in Figure 1. The arrow directions indicate the control flow, or in other words, direction of invocation across layers. Figure 2 expands on Figure 1 and provides the list of objects present in each layer.
Figure 3 - Model Objects
All the model objects are presented in Figure 3. Food object is responsible for carrying information such as food name, price, tax rate, and the food category. The table object is responsible for storing the table number and the status on whether the table is occupied or empty. If the table is occupied, the table stores an Order object associated with the table. The Order object captures items ordered from the table. Each OrderItem is an order for multiple quantities of a food item. The payment toward all the items ordered from a table is captured and persisted in the form of a Bill object. Each OrderItem in Order has a corresponding BillLineItem in the Bill.
Business objects that need to persist the model data objects depend on the persistence layer.
Let's investigate the maintainability aspect. Assume that there is a new business rule imposed in the POS application. The POS is supposed to charge a gratuity of 15% for any guest group consisting of eight or more members. On analysis of the existing application architecture, the BillBiz is the right object that can shoulder this new responsibility, because BillBiz implements the business logic that calculates the Bill amount. The BillBiz class diagram is presented in Figure 4.
Figure 4 - BillBiz Class Diagram
The BillBiz object has a payBill(Table table):Bill method. This method implements the business logic for billing. This method can be modified to accommodate the gratuity-related business change. In addition to this change, the calculated gratuity for each bill needs to be captured in some model object and persisted. The PayBillUI class in the presentation layer also needs to be changed to display the gratuity amount.
If we make these changes in the existing application as is, we need to recompile and redeploy the whole application, even though the application has a layered architecture. This is because these layers are logical and not physical. Moreover, to isolate the impact of the new business requirement, we need to isolate the Billing functionality from other functionality such as Order Management and Food Management functionality. Let's see how componentization can address this maintenance issue.
Componentized POS Application
Figure 5 - Functional Componentization of POS
In the componentized structure in Figure 5, when a new business rule for Billing is required, the Bill component can be replaced with a new Bill component without affecting rest of the application. In order to achieve the component structure proposed in Figure 6, we package the objects from the original implementation into different component packages as per Table 1.
Table 1 - Mapping of New Components to Old Objects and Layers
As a general pattern, it can be observed that each UI component consists of necessary view and controller objects. Each business component consists of necessary business objects and DAO objects for persistence. Apart from these components, the objects from the Model are packaged together as an object library, which is referred by each of these components. The objects inside the Model library are listed in Table 2. These are the same model objects that were presented in Figure 3.
Table 2 - Objects inside the Model Object library
Once we repackage the objects from the existing implementation into components as discussed above, we get component architecture for the POS application as shown in Figure 7. In the diagram the connector with a lollipop and a receptacle represents a component assembly between two components. In a component assembly, one component exposes a service and another component consumes that service. For example, the Table component exposes TableBiz service which is consumed by all the other components.
Figure 6 - Functional Componentization of POS
Figure 8 provides an expanded view of Figure 7, providing inside details of each component.
Figure 7 - Inside individual component
Replacing a Bill Component
Figure 8 - Code Snippet from payBill(Table table):Bill method of BillBiz
As can be seen from the code snippet in Figure 8, the payBill() method obtains the Order object associated with the Table for which the Bill has to be generated. For each OrderItem in the Order, a BillLineItem is generated. For each BillLineItem, the base price, tax, and total price are calculated. All BillLineItems are kept in a collection that becomes part of the generated Bill object. The Bill object also has a total base price, total tax, and total payable amount. These values are calculated as sums of corresponding price components of the constituent bill line items. In the business logic change request, the total price of the bill should have an additional component called gratuity, if the number of guests is equal to or more than 8.
The current total price of the bill is given by:
Total Price = Base Price + Tax
With the introduction of gratutity, this would have to be changed to
Total Price = Base Price + Tax + Gratuity
Gratuity = 0.15 * Base Price (if number of guests >=8)
= 0 (otherwise)
The payBill() method code snippet shown in Figure 8 can be modified to accommodate the above change. However, we need to capture the new gratuity element in the model objects. In order to give least interference to other components, we introduce a new model object called as Gratuity. This object is responsible for storing the gratuityAmount and the Bill object to which the gratuityAmount is applicable. The class diagram of Gratuity is presented below.
Figure 9 - Gratuity Object in the Model
We shall call the modified Bill component as Bill2, and the modified BillUI component as BillUI2. The modified payBill method that implements the addition of a gratuity component to the bill is shown in Figure 10.
Figure 10 - Modified Code Snippet from payBill(Table table):Bill method of BillBiz
As can be seen in Figure 8 and Figure 10, the new code has created a Gratuity object if the number of guests is equal to or more than eight. It has also added a necessary amount to the total amount in the Bill object. The BillUI2 component would retrieve the Gratuity object associated with the bill and display the gratuity amount if it is non-zero. To facilitate this retrieval, a new method called getGratuityForBill() is added to BillBiz class in the Bill2 component. The modified BillBiz class is shown in Figure 11.
Figure 11 - Modified BillBiz Class in Bill2 Component
We reassemble the POS application by substituting Bill and BillUI components with the Bill2 and BillUI2 components. Of course, the new Model object library is to be used. With these changes, the component architecture of the application is presented in Figure 12.
Figure 12 - Modified Component Architecture of POS Application
At any point, the Bill2 and BillUI2 components can be replaced with the old Bill and BillUI components to change the behavior of the application back to the old. Thus evolution of the application with the insertion and removal of new functionalities can be done by changing components in the application assembly without changing the application code.
Reader Feedback: Page 1 of 1
Subscribe to the World's Most Powerful Newsletters
Today's Top Reads