Introduction
If you haven’t heard of the words Design Pattern, you are not ready to design modern software system. At least, you are behind.
In the software engineering evolution history, reusability is always one of the golden goals. There are Object-oriented (OO) programming languages such as Small Talk, C++ and Java trying to achieve the goal at the source code level. There are component-based technologies such as DCOM, CORBA and EJB trying to reach the goal at the binary level. However, software engineering is about developing a system that has high level of usability, availability, reliability, maintainability, flexibility and security. The best practice should not just focus on code or binary reuse. The reusability of the practice itself becomes essential as well.
Here comes the Design Pattern. Design Patterns are focusing on application architecture and system architecture. They are the proven best practices that can be safely applied repeatedly. One classic design pattern reference book is the Design Patterns – Elements of Reusable Object-Oriented Software, written by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, so-called Gong of Four (GoF).
The Design Pattern is quickly recognized in the software engineering community as a powerful tool for a solid design. Its influence is rapidly spread to the new popular field, the Java world.
After Sun introduced Java 2 Enterprise Edition (J2EE), J2EE becomes widely adapted as the most promising enterprise application platform. Lead by Sun’s Java Center, a set of J2EE Design Patterns are cumulated and provided to public. Those Design Patterns are soon seen in many well-know J2EE application frameworks, from OpenSource to high-end commercial package.
In this article, along with an example, a set of most-used J2EE web-tier design patterns is introduced. The core is the Front Controller Pattern. Around it are Service to Worker Pattern, View Helper Pattern, and Dispatcher View Pattern.
J2EE Application Architecture
The J2EE application architecture is not a new invention by itself. It’s the typical post-Clent/Server web based architecture with J2EE ingredients. In theory, the architecture is the well-known Model-View-Controller (MVC). There are already enough of information about MVC these days so that we’ll get to the point here. In general, it’s a tiered architecture having clear logic and physical separation of data presentation, business logic and data sources.
The there J2EE application architecture tiers are:
o Client – the View
This is the place end user will use to interactive the J2EE based application. It could be HTML web based front-end or other type of application GUI front-end.
o Application Server – the Controller
Here resides the business logic and view-rendering module. On J2EE platform, the technologies involved are JSP/Servlet, EJB and other J2EE services such as Message Queue and Transaction Server.
o Data Source – the Model.
The data source could be just a RDBMS or large packaged systems such CRM, PDM or ERP.
J2EE Design Pattern Overview
Listed later are the J2EE design patterns that help the construction of web tier application framework. The detail definitions of these design patterns can be found at Sun J2EE Blueprint web site for design pattern:
http://java.sun.com/j2ee/blueprints/design_patterns/
A brief overview is provided for each of the patterns. We will indicate where they will be used in the sample web application framework.
Front Controller
Front controller handles all the user requests sent from client tier such a HTML form of a web page presented in browser. It’s the single point of connection between client and application server. Once the request is accept by the front controller, it will delegate the request to business logic module on the application server the handle the request, and then dispatch appropriate view back to client with data. A Front Controller can do all these internally or, by applying other design pattern, these tasks are partitioned to other pattern module on the application server, such as Service to Worker, Dispatcher View, etc.
Front Controller is recommended to be implemented as a Java Servlet.
Decorating Filter
Decorating Filter is helper design pattern that usually along with a major design pattern, such as Front Control, to achieve fine grain control and management on user request. The sample web application framework will apply this pattern to validate the authentication status of each request.
Decorating Filter can be implemented as either a Java Servlet Filter (check latest Java Servlet Specification) or internal function inside a Front Controller.
Service to Worker
As in the real world, we want assign tasks to each individual based on their skills and knowledge and expect the results coming back to a center place for organization and assembling. Service to Work pattern works the same way, user request will forward to certain module, a worker or a action module, which responses for certain task. The worker will work its way to backend data resource, directly or through other backend design pattern module, and gets the results back.
Service to Worker pattern usually works with Front Controller. The Front Controller is responsible to delegate the request to a worker module.
Service to Worker pattern is usually implemented as Java class. In the sample, we call it Action, in the meaning of an action triggered by a user request.
Dispatcher View
Dispatcher View pattern works the same way but the opposite direction of Service to Worker pattern. Once the user request has been processed, the Front Controller should return a view along with data back to user. The task can be delegated to the Dispatcher View module. Based on the user request, data or predefined dispatching strategy, the dispatcher will return an appropriate view back to user.
Dispatcher View is usually implemented as Java Class.
View Helper
View Helper is a loosely defined design pattern that can be applied many ways. In general, this pattern helps building dynamic contents for a view (JSP page) rather than let the view itself build the contents. In the sample web application framework, the View Helper will be implemented as general data bean that contains the data return by the Action (Service to Worker pattern). The view, JSP page, will present the data by accessing this data bean through the bean common interface (method).
Composite View
Utilizing the Composite View pattern is the extension of the sample web application framework. A technology representation of this pattern is to use JSP include to compose the view from different JSP source. Special dispatching strategy can be add to the Dispatcher View pattern implementation in this framework to allow the dispatcher determine how the final view will be constructed. By using this pattern, an application framework can provides more detail level of control of presentation tier.
Framework Design Goal
J2EE Design Patterns are very much framework-oriented (or software infrastructure-oriented). This is significantly different from the GoF design patterns, witch are typically applied to an application internally. The implementation of J2EE Design Patterns could easily involve multiple applications, platforms, physical servers and different type technologies. Not mention the consideration of network, Internet, security, etc.
The biggest difference between an application and an application framework is that an application can be easily build on a framework without worry about details such as how to response a user request and an framework can support many applications.
Since this article is not focusing on how to build an application framework, only several major framework design goals are discussed and demonstrated in the simple web application framework, as a sample.
Flexibility
An application framework should bed design without the ingredients of certain type of applications. A framework should be platform (OS, Application Server, etc.) independent. Since we are talking about J2EE, all these are given.
Extensibility
Building an application framework is not a one-time deal. It’s growing too, either for new functionalities or for adapting new technologies. The framework should be very easily extent without effecting the applications built upon it. By applying J2EE patterns, the robustness of the framework’s design is protected because patterns are proven repeatedly elsewhere.
Scalability
Not only an application framework supports different types of applications, but also it should support large number of applications and services on a distributed environment. The scalability issue is usually handled at the applications server and OS level. A J2EE base application framework will inherent such scalability from them.
Plugablebility
An application has its own requirements on flexibility, extensibility, etc. A framework should accommodate those requirements without forcing an application handles them by itself. The framework should allow an application to be configured to add/remove functions without rebuilding the binary. In the sample web application framework, the XML based J2EE application Deployment Descriptor is used to configure the application’s function modules.
Security
Security is becoming a essential element of architecture design in resent years because almost all applications are developed and deployed on networking environment. An application framework should provide a control point unified control of security. The J2EE pattern such the Front Controller with Decorating Filter can be applied to address this issue.