Java Design Patterns

Common design problems faced by seasoned and experienced developers helped in the creation of ‘Design Patterns’. One can reuse patterns across multiple applications.  There are three types of design patterns which are Creational, Structural and Behavioral patterns. There is also fourth design pattern which is the J2EE design pattern and which is the basis of our post. We will discuss the Model-View-Controller, Front Controller and Intercepting Filter which are all part of the Web Component developer exam.

 

Model-View-Controller:

Model-View-Controller is the most popular design pattern. It has been around since the beginning of object oriented times. This design pattern involves separating the application into three components.

  • The ‘model’ holds the data of the application. The ‘model’ is independent from the ‘Controller’ and ‘View’.
  • The ‘View’ is the visible component of the web application. It displays the model data to the user and responds to user events – like click of a button.
  • The ‘Controller’ reacts to user events generated by the user and is responsible for passing the model data to the ‘View’.

The key advantage of using the Model-View-Controller is that it reduces complexity and it makes “model class reusable without modification”. (Model View Controller Explained)

JavaDesignPatterns-1

 

Front Controller:

The Front Controller is most often used to handle requests and responses. Since most of the client server architecture is based on requests and responses the Front Controller plays a crucial role in diverting traffic to appropriate resources.  It is like the traffic policeman directing traffic across an application.

An application may have one Front Controller or multiple Controllers. Most often the Front Controller is implemented as a Servlet. Each ‘Servlet Front Controller’ contains the list of URLs to be serviced mapped in its deployment descriptor. The Front Controller dispatches the request to appropriate resources.

In short, Front Controllers are used to:

  • Control page flow and navigation
  • Reduce duplication in JSP pages
  • Handle business processing (Design Patterns and Frameworks)

Intercepting Filter:

After having seen the two design patterns – let us move onto the next pattern – the ‘Intercepting Filter’.The Intercepting Filter is one of the simplest design patterns based on the concept of ‘Filters’. Filters are the preferred method for functions similar to ‘RequestDispatchers’. Filters stand between the request and the resource and intercept the request and responses.  They smoothen out the request/response and pass it to the resource or the client. They are also a class like servlets and implement the javax.servlet.Filter interface.

Since the ‘Intercepting Filter’ is based on the concept of Filters, it is also used when there is a lot of pre and post processing. One example may be to do some pre-processing before forwarding a request to a resource. Some common pre-processing of a client requests may be related to the authentication of the client, whether the client has a valid session, if the client’s IP address is coming from a trusted network and so on.

The primary advantage of using Intercepting Filters is that they are pluggable components to which code can be added or removed without affecting the main application in any way.

JavaDesignPatterns-2

(Picture courtesy: http://www.javaskool.com/codeResources/J2EEDesignPatternChapters/j2eepatternPics/interceptor3.png)

We saw three popular design patterns which are related to the Web component developer certification in this post.

Bibliography
Design Patterns and Frameworks. (n.d.). Retrieved from docs.oracle.com: http://docs.oracle.com/cd/E19818-01/817-2334/03_design_issues.html 
Model View Controller Explained. (n.d.). Retrieved from http://www.tomdalling.com/blog/software-design/model-view-controller-explained/

About Aditi Malhotra

Aditi Malhotra is the Content Marketing Manager at Whizlabs. Having a Master in Journalism and Mass Communication, she helps businesses stop playing around with Content Marketing and start seeing tangible ROI. A writer by day and a reader by night, she is a fine blend of both reality and fantasy. Apart from her professional commitments, she is also endearing to publish a book authored by her very soon.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top