Welcome!

SOA & WOA Authors: Bernard Golden, Elizabeth White, Carmen Gonzalez, Ivan Antsipau, Paul Speciale

Related Topics: Java, XML, SOA & WOA, Websphere, Weblogic, AJAX & REA

Java: Article

Componentizing Applications with Layered Architecture

Componentization facilitates modularity and easy maintenance

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
Consider a Point-of-Sale (POS) application meant for tracking orders and payments in a restaurant. The POS is used to track the tables being occupied by the guests, orders being made from various tables, and to print bills. Apart from these operational features, the POS application can also be used for restaurant administration. Total number of tables in the restaurant, list of foods sold in the restaurant, their prices, and the associated tax rates can be managed. The use cases to be supported by the POS application are described in brief below:

  • Guests Check-in - This use case is invoked by the waiters when new guests arrive at the restaurant. POS displays a list of empty tables, and the waiter chooses an empty table and seats the guests in that table.
  • Place Order - When guests from a table order for food, waiter invokes this use case. POS prompts the waiter for the table number, food item, and quantity ordered. POS consolidates and maintains orders against each table.
  • Modify Order - Waiter can modify the quantity of any order already placed.
  • Cancel Order - Waiter can cancel any order already placed.
  • Pay Bill - Waiter invokes this use case to print the bill and collect payment for all items ordered from a table.
  • Guests Check-out - When guests from a table leave, waiter invokes this usecase to mark the table as empty.
  • Collections Report - At any point of time, the POS user can look at all the past payments collected.

Existing Implementation of POS
The existing implementation of POS uses a typical layered architecture pattern consisting of presentation layer, business layer, and data layer. The layered architecture is supported by the MVC (Model-View-Controller) design pattern. In the MVC paradigm, Model is responsible to capture real world business information through software objects. View is responsible to present the business information captured by Model visually for human consumption. Controller is responsible to handle user inputs and mediate between View and Model.

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.

Model Objects
Model objects carry the business domain relevant information. In the restaurant business domain, we have Food, Table, Order, Bill, and TableConfig model objects. In addition, there is a FoodCategory that each Food belongs to, an OrderItem, a collection of which makes up an order, and a BillLineItem, a collection of which belongs to a Bill.

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.

Presentation Layer
The presentation is based on a console-based UI in the current POS implementation. The presentation layer consists of Views and Controllers. Each UI view is responsible for showing one screen to the user and collecting input from the user interactively. The controller objects are responsible for processing the input gathered by the UI view objects. In addition, the controllers also control the UI screen flow - they direct the next UI screen to be shown after each screen based on user input. To process the input given by the users, the controller objects depend on business objects in the Business Layer.

Business Layer
The business layer contains objects that implement the business logic rules. The controllers from the presentation layer make use of the services offered by this layer. There are  four business objects in the business layer, which are presented below:

  • FoodBiz - FoodBiz is responsible for business logic associated with food creation, modification, and categorization.
  • OrderBiz - It implements all the business logic associated with maintaining orders placed by guests in different tables, addition and modification of order items, and cancellation of ordered items.
  • TableBiz - It is responsible for maintaining total number of tables based on configuration, blocking and releasing tables based on guests check-in and checkout.
  • BillBiz - This object is responsible for printing bill, and persisting bill details for future reference.

Business objects that need to persist the model data objects depend on the persistence layer.

Persistence Layer
The persistence layer is responsible for transfering the state information stored in the model objects to a persistent storage and for retrieving it back to in-memory objects. This layer consists of Data Access Objects (DAO). DAO interfaces are defined for TableConfig, Food, and Bill objects. Concrete DAO objects implement these interfaces specific to the database used. The example code that accompanies this article uses Db4o database. Each of the generic DAO is implemented by the Db4o specific concrete DAO object.

Componentization
Having analyzed the existing application in depth, which provides all the required functionalities, why should we componentize this application? Componentization in general helps in two different endeavors: (i) improve maintainability and (ii) extract reusable parts for storage and future reuse.

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
Let's split the business layer of the POS into four different components as shown in Figure 5. By splitting the application into different components, the Billing responsibility is isolated into the Bill component.

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.

Component

Objects

Layer

GuestUI

CheckInUI

CheckOutUI

CheckInCtrlr

CheckOutCtrlr

Presentation

AdminUI

AdminUI

FoodAdminUI

TableAdminUI

FoodAdminCtrlr

TableAdminCtrlr

Presentation

OrderUI

OrderUI

OrderCtrlr

Presentation

BillUI

PayBillUI

PayBillCtrlr

Presentation

Table

TableBiz

TableDAO

Business & Data

Food

FoodBiz

FoodDAO

Business & Data

Order

OrderBiz

OrderDAO

Business & Data

Bill

BillBiz

BillDAO

Business & Data

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.

Model Object Library

Objects

Model

Table

Food

Order

Bill

FoodCategory

OrderItem

BillLineItem

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
As discussed earlier, we have a business rule change request that requires an additional gratuity amount to be charged to those guest groups whose size is eight or larger. We had reasoned in the earlier analysis that the business logic change can be implemented in the BillBiz class in the Bill component, in the payBill() method. Part of the code from this method is presented in Figure 8.

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.

Conclusion
Componentization provides many benefits. In this article we demonstrated how a multi-layered POS application can be componentized. One of the important properties of a component is its pluggable nature. In the POS example, we saw how componentization facilitates modularity and easy maintenance through asimple replacement of pluggable components.

Acknowledgments
The authors would like to sincerely thank Anupama Nithyanand for her support and Nitin KL  for his valuable suggestions and reviewing this article. Authors are indebted to Soumya Bardhan, Shikhar Johari and Vishal Verma for helping in the development and testing efforts of the sample application.

More Stories By Piram Manickam

Piram Manickam works at Infosys Limited. He would like to acknowledge and thank Sangeetha S, a beloved colleague and friend, for her invaluable contributions in this work.

More Stories By Subrahmanya SV

Subrahmanya SV works at Infosys Limited. He would like to acknowledge and thank Sangeetha S, a beloved colleague and friend, for her invaluable contributions in this work.

More Stories By S Sangeetha

S Sangeetha is a Senior Technical Architect at the E-Commerce Research Labs at Infosys Limited. She has over 15 years of experience in architecture, design and development of enterprise Java applications. She is also involved in enhancing the technical skills of Architects at Infosys. She has co-authored a book on ‘J2EE Architecture’ and also has written numerous articles on Java for various online Java forums like JavaWorld, java.net, DevX.com and internet.com. She can be reached at [email protected]

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


@ThingsExpo Stories
Explosive growth in connected devices. Enormous amounts of data for collection and analysis. Critical use of data for split-second decision making and actionable information. All three are factors in making the Internet of Things a reality. Yet, any one factor would have an IT organization pondering its infrastructure strategy. How should your organization enhance its IT framework to enable an Internet of Things implementation? In his session at Internet of @ThingsExpo, James Kirkland, Chief Architect for the Internet of Things and Intelligent Systems at Red Hat, will describe how to revoluti...
The Internet of Things will greatly expand the opportunities for data collection and new business models driven off of that data. In her session at Internet of @ThingsExpo, Esmeralda Swartz, CMO of MetraTech, will discuss how for this to be effective you not only need to have infrastructure and operational models capable of utilizing this new phenomenon, but increasingly service providers will need to convince a skeptical public to participate. Get ready to show them the money! Speaker Bio: Esmeralda Swartz, CMO of MetraTech, has spent 16 years as a marketing, product management, and busin...
Samsung VP Jacopo Lenzi, who headed the company's recent SmartThings acquisition under the auspices of Samsung's Open Innovaction Center (OIC), answered a few questions we had about the deal. This interview was in conjunction with our interview with SmartThings CEO Alex Hawkinson. IoT Journal: SmartThings was developed in an open, standards-agnostic platform, and will now be part of Samsung's Open Innovation Center. Can you elaborate on your commitment to keep the platform open? Jacopo Lenzi: Samsung recognizes that true, accelerated innovation cannot be driven from one source, but requires a...
SYS-CON Events announced today that Red Hat, the world's leading provider of open source solutions, will exhibit at Internet of @ThingsExpo, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Red Hat is the world's leading provider of open source software solutions, using a community-powered approach to reliable and high-performing cloud, Linux, middleware, storage and virtualization technologies. Red Hat also offers award-winning support, training, and consulting services. As the connective hub in a global network of enterprises, partners, a...
P2P RTC will impact the landscape of communications, shifting from traditional telephony style communications models to OTT (Over-The-Top) cloud assisted & PaaS (Platform as a Service) communication services. The P2P shift will impact many areas of our lives, from mobile communication, human interactive web services, RTC and telephony infrastructure, user federation, security and privacy implications, business costs, and scalability. In his session at Internet of @ThingsExpo, Robin Raymond, Chief Architect at Hookflash Inc., will walk through the shifting landscape of traditional telephone a...
SYS-CON Events announced today that Matrix.org has been named “Silver Sponsor” of Internet of @ThingsExpo, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Matrix is an ambitious new open standard for open, distributed, real-time communication over IP. It defines a new approach for interoperable Instant Messaging and VoIP based on pragmatic HTTP APIs and WebRTC, and provides open source reference implementations to showcase and bootstrap the new standard. Our focus is on simplicity, security, and supporting the fullest feature set.
BSQUARE is a global leader of embedded software solutions. We enable smart connected systems at the device level and beyond that millions use every day and provide actionable data solutions for the growing Internet of Things (IoT) market. We empower our world-class customers with our products, services and solutions to achieve innovation and success. For more information, visit www.bsquare.com.
How do APIs and IoT relate? The answer is not as simple as merely adding an API on top of a dumb device, but rather about understanding the architectural patterns for implementing an IoT fabric. There are typically two or three trends: Exposing the device to a management framework Exposing that management framework to a business centric logic • Exposing that business layer and data to end users. This last trend is the IoT stack, which involves a new shift in the separation of what stuff happens, where data lives and where the interface lies. For instance, it’s a mix of architectural style...
SYS-CON Events announced today that SOA Software, an API management leader, will exhibit at SYS-CON's 15th International Cloud Expo®, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. SOA Software is a leading provider of API Management and SOA Governance products that equip business to deliver APIs and SOA together to drive their company to meet its business strategy quickly and effectively. SOA Software’s technology helps businesses to accelerate their digital channels with APIs, drive partner adoption, monetize their assets, and achieve a...
From a software development perspective IoT is about programming "things," about connecting them with each other or integrating them with existing applications. In his session at @ThingsExpo, Yakov Fain, co-founder of Farata Systems and SuranceBay, will show you how small IoT-enabled devices from multiple manufacturers can be integrated into the workflow of an enterprise application. This is a practical demo of building a framework and components in HTML/Java/Mobile technologies to serve as a platform that can integrate new devices as they become available on the market.
SYS-CON Events announced today that Utimaco will exhibit at SYS-CON's 15th International Cloud Expo®, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Utimaco is a leading manufacturer of hardware based security solutions that provide the root of trust to keep cryptographic keys safe, secure critical digital infrastructures and protect high value data assets. Only Utimaco delivers a general-purpose hardware security module (HSM) as a customizable platform to easily integrate into existing software solutions, embed business logic and build s...
Connected devices are changing the way we go about our everyday life, from wearables to driverless cars, to smart grids and entire industries revolutionizing business opportunities through smart objects, capable of two-way communication. But what happens when objects are given an IP-address, and we rely on that connection, sometimes with our lives? How do we secure those vast data infrastructures and safe-keep the privacy of sensitive information? This session will outline how each and every connected device can uphold a core root of trust via a unique cryptographic signature – a “bir...
Internet of @ThingsExpo Silicon Valley announced on Thursday its first 12 all-star speakers and sessions for its upcoming event, which will take place November 4-6, 2014, at the Santa Clara Convention Center in California. @ThingsExpo, the first and largest IoT event in the world, debuted at the Javits Center in New York City in June 10-12, 2014 with over 6,000 delegates attending the conference. Among the first 12 announced world class speakers, IBM will present two highly popular IoT sessions, which will take place November 4-6, 2014 at the Santa Clara Convention Center in Santa Clara, Calif...
Almost everyone sees the potential of Internet of Things but how can businesses truly unlock that potential. The key will be in the ability to discover business insight in the midst of an ocean of Big Data generated from billions of embedded devices via Systems of Discover. Businesses will also need to ensure that they can sustain that insight by leveraging the cloud for global reach, scale and elasticity.
WebRTC defines no default signaling protocol, causing fragmentation between WebRTC silos. SIP and XMPP provide possibilities, but come with considerable complexity and are not designed for use in a web environment. In his session at Internet of @ThingsExpo, Matthew Hodgson, technical co-founder of the Matrix.org, will discuss how Matrix is a new non-profit Open Source Project that defines both a new HTTP-based standard for VoIP & IM signaling and provides reference implementations.

SUNNYVALE, Calif., Oct. 20, 2014 /PRNewswire/ -- Spansion Inc. (NYSE: CODE), a global leader in embedded systems, today added 96 new products to the Spansion® FM4 Family of flexible microcontrollers (MCUs). Based on the ARM® Cortex®-M4F core, the new MCUs boast a 200 MHz operating frequency and support a diverse set of on-chip peripherals for enhanced human machine interfaces (HMIs) and machine-to-machine (M2M) communications. The rich set of periphera...

SYS-CON Events announced today that Aria Systems, the recurring revenue expert, has been named "Bronze Sponsor" of SYS-CON's 15th International Cloud Expo®, which will take place on November 4-6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Aria Systems helps leading businesses connect their customers with the products and services they love. Industry leaders like Pitney Bowes, Experian, AAA NCNU, VMware, HootSuite and many others choose Aria to power their recurring revenue business and deliver exceptional experiences to their customers.
The Internet of Things (IoT) is going to require a new way of thinking and of developing software for speed, security and innovation. This requires IT leaders to balance business as usual while anticipating for the next market and technology trends. Cloud provides the right IT asset portfolio to help today’s IT leaders manage the old and prepare for the new. Today the cloud conversation is evolving from private and public to hybrid. This session will provide use cases and insights to reinforce the value of the network in helping organizations to maximize their company’s cloud experience.
The Internet of Things (IoT) is making everything it touches smarter – smart devices, smart cars and smart cities. And lucky us, we’re just beginning to reap the benefits as we work toward a networked society. However, this technology-driven innovation is impacting more than just individuals. The IoT has an environmental impact as well, which brings us to the theme of this month’s #IoTuesday Twitter chat. The ability to remove inefficiencies through connected objects is driving change throughout every sector, including waste management. BigBelly Solar, located just outside of Boston, is trans...
SYS-CON Events announced today that Matrix.org has been named “Silver Sponsor” of Internet of @ThingsExpo, which will take place on November 4–6, 2014, at the Santa Clara Convention Center in Santa Clara, CA. Matrix is an ambitious new open standard for open, distributed, real-time communication over IP. It defines a new approach for interoperable Instant Messaging and VoIP based on pragmatic HTTP APIs and WebRTC, and provides open source reference implementations to showcase and bootstrap the new standard. Our focus is on simplicity, security, and supporting the fullest feature set.