Professional Documents
Culture Documents
Introduction
Sun Microsystems core J2EE design patterns are drawn from proven,
prior experience. They are full of wisdom and best practices that work to
make developers lives easier. OptimalJ is Compuwares model-driven,
pattern-based J2EE development environment and it implements Suns
John Crupi
Distinguished Engineer core J2EE design patterns. Sun has validated this implementation:
Chief Java Architect
Sun Microsystems
The way how OptimalJ works and makes use of patterns is impressive
All patterns in the reviewed version of OptimalJ are implemented correctly
in compliance with the Sun J2EE Pattern Catalog.
Frank Baerveldt In the real world, you need more than reliable designs; OptimalJ will
Director of Software Architecture
Compuware Corporation take you from a reliable design to a reliable implementation.
CompuwareCorporation
OJ11625.qxd 3/11/04 6:48 AM Page 2
1 Model Driven Architecture (MDA) is an Object Management Group standard for model-
based application architectures.
2
OJ11625.qxd 3/11/04 6:48 AM Page 3
3
OJ11625.qxd 3/11/04 6:48 AM Page 4
Patterns in general
Traditional application development, using 3GL languages, consists of writing
A pattern is a common solution
a number of source code files. This is a long and tedious process, requiring
to a common recurring problem
hard coding of every single line of code. With the introduction of the object-
in a given context.3 oriented programming paradigm, a major productivity improvement has been
achieved because of the ability to reuse and refine recurring components
throughout applications. Integrated Development Environment (IDE) vendors
address this development behavior by providing a set of templates, as the basis
for development of new components.
Platform
I ndepen d e n t
Model
Platform
Specific Model
Code Model
5
OJ11625.qxd 3/11/04 6:49 AM Page 6
Figure 2
6
OJ11625.qxd 3/11/04 6:49 AM Page 7
The Domain Model defines the business domains without any specific
application detail.
The Application Model defines the application, based on a certain
technology, but without any coding detail (there is no implementation
yet). The Application Model describes what is to be generated to
implement the application, by providing a logical overview of the
components contained within each layer that makes up the application.
For example, the DBMS layer contains a relational data framework that
consists of tables, columns, keys and other database-related elements. The
EJB layer consists of Entity Components, Data schemas, Key classes and
other EJB-related components. The web layer contains Data schemas,
which are used by web components to exchange data with the EJB layer
on one end and the user at the other end.
The Code Model defines the application actually implemented in code.
Figure 3
7
OJ11625.qxd 3/11/04 6:49 AM Page 8
Figure 4
Once the Application Model is in place for the DBMS, EJB and web layer,
the code implementations of defined components can be generated (i.e.,
the Java classes, SQL scripts and other technical items that are needed
for the components in the models).
A designer/business analyst will create the Domain Model in OptimalJ. Based
on this Domain Model, OptimalJ will automatically generate the Application
Model and the Code Model, using so-called Transformation Patterns, which
are explained in the following paragraphs.
Technology Patterns
Implementation Patterns.
8
OJ11625.qxd 3/11/04 6:49 AM Page 9
Domain Patterns
Application Patterns
Code Patterns.
Figure 5
Transformation Patterns
Transformation Patterns are used to transform the elements of one model into
elements in a lower-level model. A good example of a Transformation Pattern
is the transformation of component definitions into the actual source code
that implements that component.
9
OJ11625.qxd 3/11/04 6:49 AM Page 10
Functional Patterns
Functional Patterns are used to increase productivity by reusing parts of
models at a single model level. This way, people can build a library of sub-
models that can be reused in other projects. The unique feature of OptimalJ
is that multiple sub-models (source) can be woven into a new model (target).
The concept of model weaving is based on Roles and Inheritance. This gives
the user maximum flexibility in merging Classes and maintaining/controlling
changes in the model hierarchy. There are three types of Functional Patterns,
which are targeted to solution reuses:
Domain Patterns are used on the Domain Model level. A Domain Pattern
is a Domain (UML-class) Model that can be reused. A new target Domain
Model can be constructed by merging Domain Patterns from Domain
Pattern libraries. In this way a new Domain Model can be rapidly
constructed based on existing knowledge, like a set of building blocks.
Domain Patterns are always application- and implementation-independent.
An example of the application of a Domain Pattern is the merging of
two existing Domain Models (for example: The Contract Model and the
6 XTPL (eXtended Template Pattern Language) is geared to translate MOF-based models to
syntax-based languages. This language is currently used for generating Java, JSP, XML and
SQL from the MOF-based application model. The XTPL compiler compiles a TPL (Template
Pattern Language) script into plain Java classes. These Java classes take an MOF model as
input and generate, for example, XML documents (deployment descriptors) as output.
10
OJ11625.qxd 3/11/04 6:49 AM Page 11
Customer Model) into one new Domain Model. By default, all elements of
the Domain Pattern are copied to new elements in the target model. But it
is also possible to assign an element from the existing model to an element
in the pattern. When the pattern is copied, the element from the pattern
is merged with the element in the target model. This technique is called
pattern weaving, extended with some new attributes. Note that the link
to the original sources is maintained, allowing later changes to the sources
to be propagated to the merged target.
Application Patterns are used on the Application Model level. Application
Patterns follow the same principle as Domain Patterns, however, now
applied to Application Models.
Code Patterns are used on the Code Model level and they describe
implementation strategies. Code Patterns are used to create code that is
not directly related to the component definitions in the Application Model.
OptimalJ provides a wizard-driven 3GL template mechanism to reuse Java
implementation strategies. An example of such an implementation strategy
is the set of patterns described by the Gang of Four (GoF patterns, such as
ChainOfResponsibility, Decorator, FactoryMethod, etc).7
Code Patterns are closest to the traditional use of templates, as they
directly result in the creation of one or a few class files with pre-defined
programming structures. The developer has to extend these programming
structures to implement the desired functionality. Code Patterns are applied
using Wizards that request the required parameters from the user.
7 Gamma, Erich, Richard Helm, Ralph Johnson and John Vlissides, Design Patterns: Elements
of Reusable Object-Oriented Software, (New York: Addison-Wesley) 1994. The authors are
collectively known as the Gang of Four (GoF).
11
OJ11625.qxd 3/11/04 6:49 AM Page 12
The sources of static frameworks are available and ready-to-use because they
are preinstalled as Jar files. In short, the Altura framework provides utility
and ease-of-use classes. It doesnt affect the way custom code is added.
Finally, custom business logic needs to be written. This code can be inserted
into the application framework via free blocks (editable). In the free blocks,
classes from the generic and specific framework can be used.
Pre-installed Manual
code
Dynamic framework
Generic framework
Figure 6
12
OJ11625.qxd 3/11/04 6:49 AM Page 13
Presentation Layer
Business Layer
Integration Layer
13
OJ11625.qxd 3/11/04 6:49 AM Page 14
Thus the navigation between pages is fully handled by the Controller. The
navigation, however, is not hard-coded in the Controller, but is stored in a
separate XML configuration file.
Request Servlet
Controller Ref
Action
JavaBean
XML
JavaServer conf
Page
Ref
Data
Data
14
OJ11625.qxd 3/11/04 6:49 AM Page 15
Session Beans (Session Faade) are used as Task components. A Session Bean
can handle a business transaction or a query that spans multiple Entity Beans
(Composite Entity). Session Beans are also used to guard task integrity. The
majority of the task integrity rules are owned by the Session Beans.
Entity Beans are used as persistency components (an alternative is the use
of DAOs). Entity Beans are also used to guard data integrity. The majority
of the data integrity rules are owned by Entity Beans. The clients of the EJB
components, most notably the web layer, communicate coarse grain with these
components. Since these layers may very well be connected via a network,
network traffic is optimized this way. However, the clients need an easy way
to manipulate these coarse grain components.
request
perform( )
retrieve( )
forward getObject( )
object
perform( )
setObject( )
forward
perform( )
getObject( )
Html
request
perform( )
update( )
forward
perform( )
store( )
setObject( )
Figure 8
15
OJ11625.qxd 3/11/04 6:49 AM Page 16
16
OJ11625.qxd 3/11/04 6:49 AM Page 17
Compound Dataclasses
An Attribute that is being served by the component can be derived
(read/write). A pre-defined business rule Assembly exists that assembles this
compound Dataclass from primitive Dataclasses, served by other components
and vice versa.
In OptimalJ, Entity Beans are also used to guard data integrity. The majority
of the data integrity rules are owned by the Entity Beans.
In OptimalJ, Session Beans are also used to guard task integrity. The majority
of the activity integrity rules are owned by the Session Beans.
Since the components use a coarse grain communication pattern, the get/set
methods in components handle compound objects.
17
OJ11625.qxd 3/11/04 6:49 AM Page 18
For each EJB, a Business Faade is created that provides easy access to the
corresponding Bean. The Business Faade resides on the web-layer and
provides access to the EJB layer (explaining the term remote). The
Business Faade serves the following purposes:
Page Iterator
The OptimalJ Page Iterator accesses a Data Access Object (DAO) to execute
a query and stores the result-set. The client of the Page Iterator can request
chunks of data as needed.
Maintenance Pattern
The Maintenance (a.k.a CRUD) Pattern is the OptimalJ default pattern. It is
by far the most used pattern in administrative business applications, since the
CRUD represents the most common actions on data. For an average business
18
OJ11625.qxd 3/11/04 6:49 AM Page 19
a.k.a: CRC.
Invoker Patterns
While the CRUD pattern deals with data maintenance, the Invoker Pattern
handles transactions.
The Invoker Pattern is used to create components that invoke the serving
methods of transaction components. OptimalJ provides connectors to external
systems such as CORBA, CICS and similar integration technologies. These
connectors are based on the Java Connector Architecture (JCA). OptimalJ
uses the Invoker Pattern to generate integration components, based on the
imported interface definitions of these services.
Intercepting Filter
In the generated web application, the Intercepting Filter pattern is used to
set the correct character set encoding in the HTTP response. This is done
via filters as defined in section SRV.6 of the Servlet 2.3 specification.
Front Controller
In the generated web application, Jakarta-Apaches Struts (v1.0.1) is used,
which implements the Front Controller.
View Helper
This pattern is also supplied by Struts, in the form of taglibs and Form Beans.
19
OJ11625.qxd 3/11/04 6:49 AM Page 20
Composite View
This pattern is used for the JSP generation. There are multiple options for
JSP generation. Two of them are JSP Template, which generates JSPs using
Struts taglibs, and JSP Include, which generates JSPs using JSP Includes.
Service to Worker
This pattern is used for the web application. Basically, it is implemented using
Struts (Front Controller + Actions) and the Business Faade (or Business
Delegate in J2EE terminology).
Dispatcher View
This pattern is not implemented because Service to Worker is being used
and these two patterns are mutually exclusive. Given this situation, it is a
valid choice to use Service to Worker in favor of Dispatcher View.
Business Delegate
This pattern is used to generate a Business Delegate for each EJB and DAO
in the Application Model. The interface of the Business Delegate is the same
for both the EJB and DAO implementation. This Business Delegate can keep
track of the state (stateful) when the backend (i.e., Session Bean) does not
support state (stateless).
Session Faade
This pattern is used in the mapping from the Domain Model to a
Session Bean.
Service Locator
For this pattern, a generic (static) implementation is supplied in the altura-
lib. The service locator is being used to get access to EJBs, queues and topics.
Transfer Object
For every data structure that can be generated from domain classes and
domain views in domain services, a transfer object is generated.
Composite Entity
This pattern can be found in the EJB 2.0 generation (local interfaces and
Container Managed Relations). It can also be found in the EJB with Bean
Managed Persistence generation, when aggregation is being used.
20
OJ11625.qxd 3/11/04 6:49 AM Page 21
DAO
The DAO pattern is used as an alternative to Entity Beans. The end user
Compuware products has the choice to select per component how it should be modeled in the
and professional services Application Model.
delivering quality
applications Service Activator
Compuware is a leading global This pattern is used when EJB 2.0 Message-driven Beans are generated. These
provider of software products Message-driven Beans implement the Service Activator pattern.
and professional services which
IT organizations use to develop, Mediator View
integrate, test and manage the Although this is not a specific J2EE pattern, it is available in Struts as the
performance of the applications Controller/Action.
that drive their businesses. Our
software products help optimize Other J2EE patterns
every step in the application life The Synchronizer Token, which protects against double submits, is being
cyclefrom defining requirements used. Struts provides the framework for this pattern.
to supporting production service
levelsfor web, distributed and Conclusion
mainframe platforms. Our services The way in which OptimalJ makes use of patterns is impressive. Although
professionals work at customer the patterns are not directly visible to the end user, they make the generated
sites around the world, sharing code easy to understand. All patterns in the reviewed version of OptimalJ
their real-world perspective and are implemented correctly in compliance with Suns Core J2EE
experience to deliver an Pattern Catalog.
integrated, reliable solution.
Frank Baerveldt
Compuware
Baerveldt is Director of Software Architecture at Compuwares Application
Development and Integration Centre in Amsterdam, the Netherlands. He is
All Compuware products and services listed within are responsible for driving the core architecture of Compuwares Java development
trademarks or registered trademarks of Compuware and integration solution, Compuware OptimalJ, to ensure that new
Corporation. Java and all Java-based marks are trademarks
or registered trademarks of Sun Microsystems, Inc. in the development concepts are continuously applied, such as models, patterns and
United States and other countries. All other company or
supported application architectures. He has worked for the company for over
product names are trademarks of their respective owners.
2004 Compuware Corporation 10 years, primarily focusing on delivering productive, adaptable technology
solutions. Baerveldt is Compuwares J2EE application architecture expert,
specializing in meta-modeling and pattern-based development. He holds a
Computer Sciences degree from the University of Delft.
3/04 21
OJ11625.qxd 3/11/04 6:49 AM Page 22
MVC front-end
Intent
Separate presentation aspects, navigation aspects, request dispatch, and
handling and application data aspects.
Motivation
If the aspects above are separated, each can be changed independently of
each other. Also, reuse of these aspects is greatly simplified.
Structure/participants
Request
Request from the user with request info
Dispatcher
Dispatches the request to the action-handlers
Dispatching is done via a dispatch table that is changeable at runtime
Dispatch table
Holds the mapping/routing info from the request to the action-handler
and the responder
The destination (action-handler/responder) depends on the source
(action-handler) and the result code
Action-handler
Handles the request fully or partially and may forward the request via the
dispatcher to another action-handler or responder
The action-handler handles the logic
Responder
Handles the request from the action-handler and creates a response
The responder handles the presentation layout
Response
Responds with response info to the user
Consequences
The dispatch table isolates the navigation between presentations (responses)
The presentation is isolated from the logic
A central dispatcher handles all user requests
Related patterns
The well-known Model-View-Control pattern is closely related to this pattern.
Known usage
This pattern is defined and implemented in the Struts framework in the
Apache project and is freely available under the Apache license.
22
OJ11625.qxd 3/11/04 6:49 AM Page 23
Coarse-grain communication
Intent
Enable component assembly for enterprise-level components.
Motivation
Reuse of components is very important. To be able to reuse components,
one needs to know which services are offered/served by the component
and which services are required/used by the component.
Structure/participants
Component
Provides methods and public attributes to other components
Methods
Have a name, input/output/return parameters and exceptions
Offer an action service
Use methods of other components
Parameters
Types: Input/output/return
Data types can be primitive or composite
Mapped to parameters of used methods
Attributes
Offer a data service
Data types are composite
Use attributes of other components
Consequences
Enabling of component assembly
Overview of component dependency
Decoupling of component specification and component implementation
Related patterns
The well-known producer/consumer pattern is more or less related.
23
OJ11625.qxd 3/11/04 6:49 AM Page 24
Intent
To separate data integrity definition from data integrity implementation.
This is a central place to define data integrity.
Motivation
Data integrity is critical to businesses. Sometimes data integrity is defined
in the database, introducing business-critical dependencies on an
implementation layer. It will be hardly possible to change databases.
Sometimes data integrity is defined on the user interface level, which is
a problem when other user interfaces are introduced.
Structure/participants
Domain class
A data entity in the business domain
Holds domain attributes
Has domain associations with other domain classes
Domain association
Defines referential integrity (RI)
Defines behaviors rules to ensure RI (insert/delete rules)
Domain attribute
Defines the characteristics of a domain class
Domain attribute constraint
Defines constraints on the values of an attribute
Consequences
Provides a clear overview of defined data integrity
Provides a central place to maintain data integrity
24
OJ11625.qxd 3/11/04 6:49 AM Page 25
Intent
To ensure data integrity via a common business data access layer.
Motivation
An application engineer needs to know whether he needs to take care of data
integrity issues or that other components have already dealt with the issues.
By checking the data integrity on a business data access layer, the application
engineer knows that as long as he accesses data via the business data access
layer, data integrity is guaranteed.
Structure/participants
Business data access layer
Provides access to business data
Guarantees data integrity
Handles both immediate and deferred checks
Business data access component
Provides access to certain data sources
Guarantees data integrity for these data sources
Consequences
Provides a common layer that guarantees data integrity
25
OJ11625.qxd 3/11/04 6:49 AM Page 26
Intent
To separate activity constraint definition and activity constraint
implementation. This provides a central place to define activity constraints.
Motivation
An activity defines the behavior aspect of the business. Activity integrity
defines what is allowed by an activity. For example, an activity may have
read/write access on certain data classes or may cause certain data transitions.
Activity integrity is business-critical. Sometimes activity integrity is defined
in the activity itself; sometimes it is defined in the user interface layer.
Structure/participants
Activity
A behavior business entity in the business domain
Has Domain Class views with certain W/R permissions
Has Domain Class transition view
Domain Class View
Defines a set of Domain classes on which activity has certain privileges
Domain Class Transition View
Defines a set of Domain classes transition on which activity has
certain privileges
Consequences
Provides a clear overview of defined activity integrity
Provides a central place to maintain activity integrity
26
OJ11625.qxd 3/11/04 6:49 AM Page 27
Intent
To ensure activity access via a common business activity layer. Provides a
faade to the client.
Motivation
An application engineer needs to know whether he needs to take care of
activity integrity issues or that other components already have dealt with the
issues. By checking the activity constraints on a business activity layer, the
application engineer knows that as long as he accesses business behavior via
the business activity layer, activity integrity is guaranteed.
Structure/participants
Business activity layer
Provides access to business behavior
Guarantees activity integrity
Handles business transactions
Business activity component
Provides access to certain business behavior
Guarantees activity integrity
Consequences
Common layer that guarantees activity integrity
27
OJ11625.qxd 3/11/04 6:49 AM Page 28
Intent
To reduce the coupling between presentation tier clients and business services.
Enables easy access on client tier to enterprise components.
Motivation
Enterprise components usually use coarse-grain communication to reduce
communication overhead. On the client tier, developers need fine-grain
access to the data objects. The Business Faade provides fine-grain access
to the coarse-grain data object.
Another usability issue is the fact that some enterprise components handle
single objects, while other enterprise objects handle collections of objects.
The Business Faade provides facilities to handle collections if needed.
Structure/participants
Business Faade
Manages all Update Objects
Provides the high-level interface of the enterprise component to the clients
Handles object collections
Runs on the client/web tier
Update Object
An editable data object with fine-grain interface
Data Object
The coarse-grain object that acts like a transport object
To edit Data Objects, Data Objects are converted to Update Objects
Consequences
Fine-grain access for usability, while coarse-grain communication for
performance
Business Faade handles collections regardless whether this is supported
by enterprise components.
28
OJ11625.qxd 3/11/04 6:49 AM Page 29
Intent
Provides easy use of one of the most popular application protocols. CRUD
is used to maintain persistent data (e.g, base tables). Disconnected
collections are supported as well.
Motivation
The normal protocol for data maintenance is:
Structure/participants
Business Faade
See Update Objects and Business Faade pattern (paragraph 5.7)
Update Object
An editable data object with fine-grain interface
Has all the modified objects, including inserted and deleted objects
Consequences
Optimized data transport
Disconnected collections supported
29
OJ11625.qxd 3/11/04 6:49 AM Page 30
Invoker
Intent
Provides easy use of one of the most popular application protocols.
Invoker is used to start transactions.
Motivation
The normal protocol for transactions is:
1. Collect (user) data, optionally via fill-in form
2. Pass the data as input parameters to transaction and invoke transaction
3. Return the result of transaction.
This is a frequently used protocol that is very suitable for reuse.
Structure/participants
Business Faade
Provides methods that map to transaction
Update Object
An editable data object that collects input data
Data Object
Acts as parameter to method
Consequences
Simplify transaction invocation
30
OJ11625.qxd 3/11/04 6:49 AM Page 31
31