Book Image

Learning Modular Java Programming

By : Tejaswini Mandar Jog
Book Image

Learning Modular Java Programming

By: Tejaswini Mandar Jog

Overview of this book

Modular programming means dividing an application into small parts and then developing it. It is an approach taken by developers to build applications and helps them add efficiency in their development process, thus making it more effective. The book starts with the fundamentals of Modular Programming. Then we move on to the actual implementation, where we teach developers how to divide an application into different modules or layers (such as presentation, execution, security, lifecycle, services, and so on) for better management. Once readers are well-versed in these modules and their development, the book shows how to create bindings in order to join these different modules and form a complete application. Next, the readers will learn how to manage these modules through dependency injection. Later, we move on to testing; readers will learn how to test the different modules of an application. The book ends by teaching readers how to maintain different versions of their application and how to modify it. By the end of the book, readers will have a good understanding of modular programming and will be able to use it to build applications with Java.
Table of Contents (15 chapters)

Tiers and layers in an enterprise application


An enterprise application is an application which has been developed to fulfill the requirements of a business. Being an enterprise application, it normally has huge code. Maintaining such huge code all together is a very complex task. Also, developing the code takes lots of time. So the code is been divided into small, maintainable modules which can be easily developed separately and later on combined to give a final product. All modules which provide similar kind of functionality will be grouped together to form a layer. These layers are the logical separation of modules. But sometimes, for better performance, one layer can be also spread over the network.

Layers are a logical separation of the code to increase maintainability. But when we physically move one typical layer and deploy it on another machine, then it will be called as a tier. At any one time, many users will be using the enterprise application simultaneously, so the use of a tiered application provides good performance.

Let's consider a web module for login. The user will open the browser and the login page will be rendered. The user will enter their credentials (username and password). After submitting the form, a request will be sent to the server to perform the authentication. Once the data is received on the server side, the business logic layer will process the data and put the result in the response. The result depends on whether the credentials are present in database or not.

Finally, the response will be generated and the result will be sent back to the browser. Here, the user interface, business logic, and database are the three distinct features involved. These are called as the presentation layer, business logic layer, and data storage layer, respectively.

Layers in an enterprise application

Each of these layers will talk with the above layer and exchange data. The process broadly takes place as follows:

  • The user will open the browser and hit the URL.

  • A login page will be rendered on the browser. The user will fill in the form and submit it to be processed by the business logic layer.

  • To check the data, the business logic layer will communicate with the data storage layer.

  • According to the result returned from the data storage layer, the business logic layer will now send the result to the presentation layer and the client's browser will render the results page.

Now we understand the difference between a tier and a layer, let's discuss tiers in detail. Depending on how many physical separations one application is using, it will be called a one-tier, two-tier, or multi-tier application.

One-tier applications

An enterprise application where all the components reside on a single computer will be called as a one-tier application. These applications can also be called single-tier applications. So we can broadly say that these are the applications which get installed and run on a single computer.

For example, an Eclipse application as software. When we install eclipse and launch it, it will be running on our personal computer. It doesn't require any network. The presentation layer, that is, Swing GUI, business logic, and storing information in a filesystem will be done on the same computer.

A one-tier application

Two-tier applications

When the enterprise application gets divided over two computers, it will be called a two-tier application. Generally the data storage, that is, the database, will be moved onto a separate, dedicated computer. This will work as a database host machine or database server. The presentation layer and business logic layer will be residing in one location and data layer will be residing in another.

An example of this is the Oracle database management system. When we want to use an Oracle database, we will install Oracle on a dedicated machine which can be called as the Oracle server. Now, on the user's machine, we can install the Oracle client. Whenever we want to fetch data from the table in Oracle, we will use the client application, which will connect to the server and give the required data.

A two-tier application

Three-tier applications

When in an application, the presentation layer, business layer, and data layer will be running on their dedicated servers and interact with each other through a network, it will be called a three-tier application. The web server is dedicated to the presentation layer, the middleware server is dedicated to the business layer, and the database server is dedicated to the database layer. The middleware server can also provide services such as transaction and connection polling.

For example, any online shopping application can be considered a three-tier application. Let's see how. In this application, the products will be displayed on a browser in presentation pages. The business logic part, such as the calculation of discounts, the total amount which the buyer has to pay, and so on, using transaction or messaging services, will be provided by the application server. The buyer's information, product information, bank details, delivery address, and so on, will be saved in the tables on the database server for further reference. That means the presentation tier, application tier and data tier are the three tiers which play roles in this application.

A three-tier application

N-tier applications

With the increase in the use of the Internet, it's very important for an application to be capable of serving many requests at the same time. This puts a burden on the server. In terms of performance, it's a better solution to take away the presentation layer from the business logic and deploy it separately. It can be deployed on one dedicated web server or may be on different servers. In the same way, the business logic and database layers can be separated on different servers partially or completely residing in one or more machines.

The client tier, presentation tier, business tier, and database tier are separated on separate machines. They interact with each other through a network and perform their services. This will be called an N-tier application.

An N-tier application

Now, these tiers which consist of layers will be used to create an enterprise application. But just by making parts of application, we cannot be sure of having a complete solution. Each application has its own challenges, but if we keenly observe them we will find that numerous functionalities are common, irrespective of their problem statement. That means instead of a new team of architect designers fighting for the solution to their problem every time, it will be good to have an answer which is a generic solution for reference. These references will be used to build applications called design patterns.

Christopher Alexander says, Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solutions to that problem, in such a way that you can use this solution a million times over without ever doing it the same way twice.

Each pattern provides a solution to a sort of problem and gives a result quickly. Let's have a quick overview of design patterns. The design patterns are normally classified as creational, structural, and behavioral patterns, which are subclassified as shown in the following table:

Creational design patterns

Structural design patterns

Behavioral design patterns

Singleton pattern

Adapter pattern

Observer pattern

Factory pattern

Composite pattern

Interpreter pattern

Builder pattern

Façade pattern

Chain of responsibility pattern

Prototype pattern

Decorator pattern

Visitor pattern

  

Mediator pattern

Frameworks such as Struts and Spring have been built upon these design patterns. Struts uses the front controller design pattern and Spring uses the MVC design pattern for ease of development. The use of frameworks has made the developer's life easy.

Java Enterprise architecture

High performance, faster processing, and good look and feel are the keys to success for enterprise applications. Due to dedicated servers, the tasks of designing, developing, and data storing will be leveraged to handle specialized tasks, as discussed above in the N-tier applications section. In spite of providing just basic things, the N-tier enterprise application needs a bit more. Let's have a look into what else an enterprise application may need.

It's a festive season. The bank has consecutive holidays. I need to withdraw some amount from the ATM, say, for example, x amount. I enter the password and all other required details. Now I am just waiting for the money. I get a message of withdrawal on my mobile as well but… as the ATM doesn't have any money, I haven't received it. The money has been deducted from the account but not received by me. Now what??? Am I going to be in loss? Is there any way to revert what went wrong? Yes, certainly!! Here, we need to take into consideration transaction management. A transaction is a bunch of consecutive operations which take place one after another; either all of these should be completed successfully or none of them. The transaction helps the developer to maintain data integrity. Using this transaction management, the logic has been developed to rollback all such unsuccessful operations. Using this concept, the debited amount can be reversed and credited to my account again. Thank God, I got my money back!!!!

There are many such ATM centers in the city. And the same banking application will be used by many users at the same time. So the application will have multiple requests. All these requests have to be handled in sync. This is possible only if the application supports multithreading, which technically we call concurrency.

The ATM is one of the ways to perform banking operations, but today we can even use the Web for these tasks. On the Internet, the request will be sent to the server and further processing happens. As this is a remote process, authentication and authorization is important for recognizing that the user is genuine. This is normally done by using a unique username/password pair which the user enters. The sensitive data will now be transferred through the network; this can be hacked. So such applications should provide secure service layers, which are the URLS with the prefix https. We are doing all this to achieve a very important service, that is, security. Security helps to minimize hazardous attacks on the web application.

Do all such services have to be developed by the development team? Well, not completely: in N tier applications, we do have a middleware server. This server can also be called a container. This container will provide services such as transaction management, security, connection pooling, caching, and concurrency.

Enterprise architecture