J2EE Design Patterns Overview
J2EE Design Patterns Overview
Chapter
T
he J2EE patterns are a collection of J2EE-based solutions to common prob-
lems. They reflect the collective expertise and experience of Java architects
at the Sun Java Center, gained from successfully executing numerous J2EE
engagements. The Sun Java Center is Sun’s consulting organization, focused on J2EE Patterns
Overview
architecting Java technology-based solutions for customers. The Sun Java Center
has been architecting solutions for the J2EE platform since its early days, focusing
on achieving Quality of Service (QoS) qualities, such as scalability, availability,
performance, securability, reliability, and flexibility.
These J2EE patterns describe typical problems encountered by enterprise appli-
cation developers and provide solutions for these problems. We have formulated
these solutions based on our ongoing work with numerous J2EE customers and on
exchanges with other Java architects experiencing similar problems. The patterns
capture the essence of these solutions, and they represent the solution refinement
that takes place over the course of time and from collective experience. To put it
another way, they extract the core issues of each problem, offering solutions that
represent an applicable distillation of theory and practice.
Our work has focused on the J2EE area, especially regarding such J2EE compo-
nents as Enterprise Java Beans (EJB), JavaServer Pages (JSP), and servlets. During
our work with J2EE customers implementing the various components, we have
come to recognize the common problems and difficult areas that may impede a
good implementation. We’ve also developed effective best practices and
approaches for using the J2EE components in combination.
117
[Link] Page 118 Friday, May 16, 2003 10:58 AM
The patterns presented here extract these “best practice” approaches and
present them to you in a way that enables you to apply the patterns to your own
particular application and to accommodate your own needs. The patterns clearly
and simply express proven techniques. They make it easier for you to reuse suc-
cessful designs and architectures. Simply put, you can use the patterns to design
your J2EE system successfully and quickly.
What Is a Pattern?
In Chapter 1, we discussed how different experts define a pattern. We also dis-
cussed some of the peripheral issues around patterns including the benefits of
using patterns. Here, we revisit this discussion in the context of the J2EE Pattern
Catalog.
As discussed in Chapter 1, some experts define a pattern as a recurring solu-
tion to a problem in a context.
These terms—context, problem, and solution—deserve a bit of explanation.
First, what is a context? A context is the environment, surroundings, situation, or
interrelated conditions within which something exists. Second, what is a prob-
lem? A problem is an unsettled question, something that needs to be investigated
J2EE Patterns
and solved. Typically, the problem is constrained by the context in which it
Overview occurs. Finally, the solution refers to the answer to the problem in a context that
helps resolve the issues.
So, if we have a solution to a problem in a context, is it a pattern? Not neces-
sarily. The characteristic of recurrence also needs to be associated with the defini-
tion of a pattern. That is, a pattern is only useful if it can be applied repeatedly. Is
that all? Perhaps not. As you can see, while the concept of a pattern is fairly sim-
ple, actually defining the term is more complex.
We point you to the references so that you can dig more deeply into the pat-
tern history and learn about patterns in other areas. In our catalog, a pattern is
described according to its main characteristics: problem, and solution, along
with other important aspects, such as forces and consequences. The section
describing the pattern template (see “Pattern Template” on page 129) explains
these characteristics in more detail.
Identifying a Pattern
We have handled many J2EE projects at the Sun Java Center, and over time we
have noticed that similar problems recur across these projects. We have also seen
similar solutions emerge for these problems. While the implementation strategies
[Link] Page 119 Friday, May 16, 2003 10:58 AM
varied, the overall solutions were quite similar. Let us discuss, in brief, our pat-
tern identification process.
When we see a problem and solution recur, we try to identify and document
its characteristics using the pattern template. At first, we consider these initial
documents to be candidate patterns. However, we do not add candidate patterns
to the pattern catalog until we are able to observe and document their usage mul-
tiple times on different projects. We also undertake the process of pattern mining
by looking for patterns in implemented solutions.
As part of the pattern validation process, we use the Rule of Three, as it is
known in the pattern community. This rule is a guide for transitioning a candidate
pattern into the pattern catalog. According to this rule, a solution remains a candi-
date pattern until it has been verified in at least three different systems. Certainly,
there is much room for interpretation with rules such as this, but they help provide
a context for pattern identification.
Often, similar solutions may represent a single pattern. When deciding how to
form the pattern, it is important to consider how to best communicate the solu-
tion. Sometimes, a separate name improves communication among developers. If
so, then consider documenting two similar solutions as two different patterns. On
the other hand, it might be better to communicate the solution by distilling the
similar ideas into a pattern/strategy combination.
J2EE Patterns
Patterns Versus Strategies Overview
When we started documenting the J2EE patterns, we made the decision to docu-
ment them at a relatively high level of abstraction. At the same time, each pattern
includes various strategies that provide lower level implementation details.
Through the strategies, each pattern documents a solution at multiple levels of
abstraction. We could have documented some of these strategies as patterns in
their own right; however, we feel that our current template structure most clearly
communicates the relationship of the strategies to the higher level pattern struc-
ture in which they are included.
While we continue to have lively debates about converting these strategies to
patterns, we have deferred these decisions for now, believing the current docu-
mentation to be clear. We have noted some of the issues with respect to the rela-
tionship of the strategies to the patterns:
• The patterns exist at a higher level of abstraction than the strategies.
• The patterns include the most recommended or most common implemen-
tations as strategies.
[Link] Page 120 Friday, May 16, 2003 10:58 AM
J2EE Patterns
Overview
Client Tier
This tier represents all device or system clients accessing the system or the appli-
cation. A client can be a Web browser, a Java or other application, a Java applet, a
WAP phone, a network application, or some device introduced in the future. It
could even be a batch process.
Presentation Tier
This tier encapsulates all presentation logic required to service the clients that
access the system. The presentation tier intercepts the client requests, provides
single sign-on, conducts session management, controls access to business ser-
vices, constructs the responses, and delivers the responses to the client. Servlets
and JSP reside in this tier. Note that servlets and JSP are not themselves UI ele-
ments, but they produce UI elements.
Business Tier
This tier provides the business services required by the application clients. The
tier contains the business data and business logic. Typically, most business pro-
cessing for the application is centralized into this tier. It is possible that, due to
legacy systems, some business processing may occur in the resource tier. Enter-
prise bean components are the usual and preferred solution for implementing the
business objects in the business tier. J2EE Patterns
Overview
Integration Tier
This tier is responsible for communicating with external resources and systems
such as data stores and legacy applications. The business tier is coupled with the
integration tier whenever the business objects require data or services that reside
in the resource tier. The components in this tier can use JDBC, J2EE connector
technology, or some proprietary middleware to work with the resource tier.
Resource Tier
This is the tier that contains the business data and external resources such as
mainframes and legacy systems, business-to-business (B2B) integration systems,
and services such as credit card authorization.
J2EE Patterns
We used the tiered approach to divide the J2EE patterns according to functional-
ity, and our pattern catalog follows this approach. The presentation tier patterns
[Link] Page 122 Friday, May 16, 2003 10:58 AM
contain the patterns related to servlets and JSP technology. The business tier pat-
terns contain the patterns related to the EJB technology. The integration tier pat-
terns contain the patterns related to JMS and JDBC. See Figure 5.2 on page 132.
J2EE Patterns
Overview
Terminology
Players in the enterprise computing area, and particularly establishments using
Java-based systems, have incorporated a number of terms and acronyms into their
language. While many readers are familiar with these terms, sometimes their use
varies from one setting to another. To avoid misunderstandings and to keep things
consistent, we define in Table 5-4 how we use these terms and acronyms.
Use of UML
We have used UML extensively in the pattern catalog, particularly as follows:
• Class diagrams – We use the class diagrams to show the structure of the
pattern solution and the structure of the implementation strategies. This
provides the static view of the solution.
• Sequence (or Interaction) diagrams – We use these diagrams to show
the interactions between different participants in a solution or a strategy.
This provides the dynamic view of the solution.
• Stereotypes – We use stereotypes to indicate different types of objects
J2EE Patterns and roles in the class and interaction diagrams. The list of stereotypes and
Overview
their meanings is included in Table 5-5.
Each pattern in the pattern catalog includes a class diagram that shows the
structure of the solution and a sequence diagram that shows the interactions for
the pattern. In addition, patterns with multiple strategies use class and sequence
diagrams to explain each strategy.
To learn more about UML, please see the Bibliography.
UML Stereotypes
While reading the patterns and their diagrams, you will encounter certain stereo-
types. Stereotypes are terms coined or used by designers and architects. We cre-
ated and used these stereotypes to present the diagrams in a concise and easy to
understand manner. Note that some of the stereotypes relate to the terminology
explained in the previous section. In addition to these stereotypes, we also use
pattern names and the main roles of a pattern as the stereotypes when it helps in
explaining a pattern and its strategies.
[Link] Page 129 Friday, May 16, 2003 10:58 AM
Stereotype Meaning
Custom Tag JSP custom tags are used to implement Helper objects, as are Jav-
aBeans. A Helper is responsible for such activities as gathering
data required by the View and for adapting this data model for
use by the View. Helpers can service requests for data from the
View by simply providing access to the raw data or by formatting
the data as Web content.
Pattern Template
The J2EE patterns are all structured according to a defined pattern template. The
pattern template consists of sections presenting various attributes for a given pat-
tern. You’ll also notice that we’ve tried to give each J2EE pattern a descriptive pat-
tern name. While it is difficult to fully encompass a single pattern in its name, the
pattern names are intended to provide sufficient insight into the function of the pat-
tern. Just as with names in real life, those assigned to patterns affect how the reader
will interpret and eventually use that pattern.
[Link] Page 130 Friday, May 16, 2003 10:58 AM
Intercepting Filter
Delegate Control
Centralize Control
Integration-tier
Application Controller
Front Controller
Create Create
Uses Locate
Application Service Business Delegate Service Locator
Services
Business Object
Invoke Value List Handler
Business
Processing Implement Send
Entity Beans Data
Transparently
Persist
Composite Entity Transfer Object Assembler
Service Activator
Chain
Asynchronous
Processing Domain Store Transfer Object
Delegate Uses for
Asynchronous Bean-managed
Processing Persistence Send Data
uses uses
Web Service Broker Data Access Object
vice (357) can invoke other Application Services (357) and thus create a layer of
services in your application.
Business Object (374) implements your conceptual domain model using an
object model. Business Objects (374) separate business data and logic into a sepa-
rate layer in your application. Business Objects (374) typically represent persistent
objects and can be transparently persisted using Domain Store (516).
Composite Entity (391) implements a Business Object (374) using local entity
beans and POJOs. When implemented with bean-managed persistence, a Com-
posite Entity (391) uses Data Access Objects (462) to facilitate persistence.
The Transfer Object (415) pattern provides the best techniques and strategies
to exchange data across tiers (that is, across system boundaries) to reduce the net-
work overhead by minimizing the number of calls to get data from another tier.
The Transfer Object Assembler constructs a composite Transfer Object (415)
from various sources. These sources could be EJB components, Data Access
Objects (462), or other arbitrary Java objects. This pattern is most useful when the
client needs to obtain data for the application model or part of the model.
The Value List Handler (444) uses the GoF iterator pattern to provide query
execution and processing services. The Value List Handler (444) caches the
results of the query execution and return subsets of the result to the clients as
requested. By using this pattern, it is possible to avoid overheads associated with
finding large numbers of entity beans. The Value List Handler (444) uses a Data
J2EE Patterns Access Object (462) to execute a query and fetch the results from a persistent
Overview
store.
Data Access Object (462) enables loose coupling between the business and
resource tiers. Data Access Object (462) encapsulates all the data access logic to
create, retrieve, delete, and update data from a persistent store. Data Access
Object (462) uses Transfer Object (415) to send and receive data.
Service Activator (496) enables asynchronous processing in your enterprise
applications using JMS. A Service Activator (496) can invoke Application Ser-
vice (357), Session Façade (341) or Business Objects (374). You can also use sev-
eral Service Activators (496) to provide parallel asynchronous processing for long
running tasks.
Domain Store (516) provides a powerful mechanism to implement transparent
persistence for your object model. It combines and links several other patterns
including Data Access Objects (462). Web Service Broker (557) exposes and bro-
kers one or more services in your application to external clients as a web service
using XML and standard web protocols. A Web Service Broker (557) can interact
with Application Service (357) and Session Façade (341). A Web Service Broker
(557) uses one or more Service Activators (496) to perform asynchronous pro-
cessing of a request.
[Link] Page 135 Friday, May 16, 2003 10:58 AM
Table 5-2 shows the functions handled by the business tier patterns and indi-
cates where you can find the particular pattern or patterns that may provide solu-
tions.
Isolate vendor and technology dependencies for Pattern Service Locator (315)
services lookup
Provide uniform method for business service Pattern Service Locator (315)
lookup and creation
Hide the complexity and dependencies for enter- Pattern Service Locator (315)
prise bean and JMS component lookup
Transfer data between business objects and cli- Pattern Transfer Object (415)
ents across tiers
Provide simpler uniform interface to remote cli- Pattern Business Delegate
ents (302)
Pattern Session Façade (341)
Pattern Application Service
(357)
J2EE Patterns
Overview Reduce remote method invocations by providing Pattern Session Façade (341)
coarse-grained method access to business tier
components
Manage relationships between enterprise bean Pattern Session Façade (341)
components and hide the complexity of interac-
tions
Protect the business tier components from direct Pattern Session Façade (341)
exposure to clients Pattern Application Service
(357)
Provide uniform boundary access to business tier Pattern Session Façade (341)
components Pattern Application Service
(357)
Implement complex conceptual domain model Pattern Business Object
using objects (374)
[Link] Page 139 Friday, May 16, 2003 10:58 AM
Table 5-3 shows the functions typically handled by the presentation tier pat-
terns and indicates which pattern provides a solution.
J2EE Patterns Table 5-3 Integration Tier Patterns
Overview
If you are looking for this Find it here
Summary 141
Expose a web service using XML and standard Pattern Web Service Broker
Internet protocol (557)
Aggregate and broker existing services as web Pattern Web Service Broker
services (557)
Summary
So far, we have seen the basic concepts behind the J2EE patterns, understood the
tiers for pattern categorization, explored the relationships between different pat-
terns, and taken a look at the roadmap to help guide you to a particular pattern. In
the following chapters, we present the patterns individually.
There are three chapters, for presentation, business, and integration tiers.
Refer to the appropriate chapter to find the pattern you’re interested in.
J2EE Patterns
Overview