Professional Documents
Culture Documents
J2EE Design Patterns: Sharath Sahadevan August 8, 2002 ST Louis Java SIG
J2EE Design Patterns: Sharath Sahadevan August 8, 2002 ST Louis Java SIG
Design Patterns ?
What are Design Patterns ?
Design Patterns ?
Design Patterns capture solutions that have developed
and evolved over time . They reflect untold redesign and recoding as developers have struggled for greater reuse and flexibility in their software.-
J2EE
Java 2 Platform ,Enterprise Edition Provides a unified platform for developing distributed , server-centric applications.
J2EE Patterns
Front Controller View Helper
Dispatcher View
Service To Worker Intercepting Filter
J2EE patterns
Service Locator
Session Faade Message Facade Business Delegate Value Object
J2EE Patterns
Value List Handler
Primary key generation strategies Data Access Object Resource Adapter
Front Controller
Provides a centralized controller for managing the handling of a request . The front controller will look at the request and forward it on to the right handler or jsp . Good place to have the licensing and security code . It can be either a jsp or servlet. Preferably a servlet .
Front Controller
Advantages
Promotes reuse of common code that is needed for all requests . Promotes flexibility Easier to maintain
Front Controller
Avoid fat controllers .
Do not restrict site to one controller. Different subsystems could have their own controllers.
View Helper
View Helpers are Java beans or custom tags that are used to get the data that needs to be presented. Do not use Servlets for views. Improves reuse and maintainability. Reduces scriptlet code. View Helper sequence
Dispatcher View
Dispatcher is responsible for view management and navigation . Can be encapsulated within a controller, a view or as a separate component. Dispatcher view suggests deferring content retrieval to the time of view processing. Dispatcher sequence
Service To Worker
Similar to dispatcher view , but the dispatcher is more sophisticated. In Service To Worker the dispatcher will call upon a helper to determine the next view. Controller takes on significant responsibility. It manages content retrieval , validation, authorization etc. The data retrieved is stored in a value object for use by the view.
Intercepting Filter
Create Pluggable filters to process common services in a standard manner , without requiring changes to the core request .
Introduced in Servlet specification 2.3 Filters allow on the fly transformations of payload and header of both the request into a resource and the response from a resource. Filters do not generally create a response or respond to a request as servlets do , rather they are used to modify the request or the response.
Intercepting filters
Related to the decorator ( GOF ) pattern
Front controller provides similar functionality , but is better suited to handling core processing. Examples of filter use - authentication filters , logging & auditing , Image conversion , data compression , encryption
Intercepting filter
How to write a filter ?
Implement the javax.servlet.Filter interface Container will call the doFilter() method.
The doFilter method will modify the request or response and then call the next filter in the filter chain.
Intercepting filter
Configuring a filter in the deployment descriptor ( web.xml ) :
<filter> <filter-name>Image Filter</filter-name> <filter-class>com.acme.ImageFilter</filter-class>
<url-pattern>/*</url-pattern>
</filter-mapping>
Session Facade
A faade is usually provided to hide the underlying complexity from the client. A session bean is used as a session faade to perform coarse grained functionality . The session bean will probably interact with two or more entity beans . A session faade combined with a Data Access Object can be used for read only data.
Session Facade
Advantages:
Improved transaction control Exposes fewer remote interfaces to the client.
Improves performance by reducing the number of fine grained method calls from the client.
Message Facade
Use a message driven bean (MDB) for asynchronous communication .
The client can submit a message on a Java Message Service ( JMS ) Queue or a Topic . The MDB is configured to listen for any messages . When a message is received , the MDB will pick it up and process the message .
Message Facade
Asynchronous communication - The client can send the message on the JMS destination and is free to continue processing
Guaranteed delivery of message - If some part of the system is down the JMS destination can be configured so that all the messages are persistent . MDB's do not have return values MDB's do not propagate exceptions back to the clients . Usually an e-mail is generated to inform the client of success or failure of the use-case . Message Faade sequence diagram
Business Delegate
Plain Java classes that hide EJB API complexity by encapsulating code required to discover, delegate to and recover from invocations on the session and message faade EJB layers. Use on large projects where the web team is separate from the EJB team .
Value Object
A value object is an object that encapsulates all the data required by a client . The client can then call get methods on the value object to get all the data needed by the client . When a client requests an Entity or a Session bean for business data , the bean should construct a value object and return it to the client .
Service Locator
Is a Singleton that is used to reuse code performing the JNDI lookup .
Abstracts complexity Provides uniform service access to Clients Improves performance Sometimes referred to as the EJBHomeFactory ( EJB design patterns ) .
Resource Adapter
J2EE Connector Architecture Deploy the Resource Adapter on the application server. Vendors develop adapters for their systems Application developers can take advantage of the connection pooling managed by the application server.
Resource Adapter
Resource Adapters are packaged in a .rar file and deployed on the application server.
J2EE connector architecture
References
Design Patterns , Elements of Reusable Object-Oriented Software - GOF Core J2EE Patterns, Best Practices and Design Strategies. - Deepak Alur, John Crupi , Dan Malks EJB Design Patterns - Floyd Marinescu Enterprise Java Beans - Richard MonsonHaefel