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.´-

GOF in Design Patterns - Elements of Reusable Object Oriented Software.

Why study patterns ?
‡ Develop better products. ‡ Learn from others experience. ‡ Improve communication with others in the same field. ‡ Don¶t reinvent the wheel.

Brief History of Patterns
‡ In 1970, Christopher Alexander documented patterns in Civil Engineering and architecture ‡ Software design patterns popularized by GOF ( Gang of Four )

‡ Java 2 Platform ,Enterprise Edition ‡ Provides a unified platform for developing distributed , server-centric applications.

J2EE Patterns
y Front Controller y View Helper y Dispatcher View y Service To Worker y Intercepting Filter

J2EE patterns
y Service Locator y Session Façade y Message Facade y Business Delegate y Value Object

J2EE Patterns
y Value List Handler y Primary key generation strategies y Data Access Object y Resource Adapter

Front Controller
y Provides a centralized controller for managing the handling of a request . y The front controller will look at the request and forward it on to the right handler or jsp . y Good place to have the licensing and security code . y It can be either a jsp or servlet. Preferably a servlet . y Controller sequence diagram

Front Controller
‡ Advantages
y Promotes reuse of common code that is needed for all requests . y Promotes flexibility y Easier to maintain

Front Controller
y Avoid fat controllers . y Do not restrict site to one controller. y 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 .
y Introduced in Servlet specification 2.3 y Filters allow on the fly transformations of payload and header of both the request into a resource and the response from a resource. y 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
y Related to the decorator ( GOF ) pattern y Front controller provides similar functionality , but is better suited to handling core processing. y Examples of filter use - authentication filters , logging & auditing , Image conversion , data compression , encryption «

Intercepting filter
‡ How to write a filter ?
y Implement the javax.servlet.Filter interface y Container will call the doFilter() method. y 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>

‡ </filter> ‡ <filter-mapping> ‡ ‡ <filter-name>Image Filter</filter-name> <url-pattern>/*</url-pattern>

‡ </filter-mapping>

Session Facade
y A façade is usually provided to hide the underlying complexity from the client. y A session bean is used as a session façade to perform coarse grained functionality . y The session bean will probably interact with two or more entity beans . y A session façade combined with a Data Access Object can be used for read only data.

Session Facade
‡ Advantages:
y Improved transaction control y Exposes fewer remote interfaces to the client. y Improves performance by reducing the number of fine grained method calls from the client.

‡ Session Façade sequence diagram

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
y Asynchronous communication - The client can send the message on the JMS destination and is free to continue processing y 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 . y MDB's do not have return values y 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 . y Message Façade 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 façade 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 .

‡ Value Object Sequence

Data Access Object
y Use a Data Access Object ( DAO ) to abstract all access to a data source. y The DAO will help to hide details of access to the data source from the client. y Promotes easier migration from one data source to another .

‡ Data Access Object Sequence

Service Locator
‡ Is a Singleton that is used to reuse code performing the JNDI lookup .
y Abstracts complexity y Provides uniform service access to Clients y Improves performance y Sometimes referred to as the EJBHomeFactory ( EJB design patterns ) .

‡ Service Locator sequence

Primary Key Generation strategies
‡ How can we generate primary keys for entity beans ?
y Sequence Blocks y UUID for EJB y Stored Procedures for Autogenerated keys

Primary Key Generation strategies
‡ Sequence blocks
y Uses a stateless session bean and a CMP entity bean . y The CMP entity bean represents a sequence in the database. y A session façade will front the sequence entity bean .It will get blocks of integers at a time and cache them locally.

Primary Key Generation strategies
‡ UUID for EJB ‡ Create primary keys in memory by creating a universally unique
identifier (UUID ) that combines enough system information to make it unique . ‡ Very fast .

Primary Key Generation strategies
y Stored Procedures for Autogenerated key y Stored procedures are used to insert the data and return the generated key . The stored procedure is called from the entity beans ejbCreate() method. y Uses JDBC CallableStatement to call the stored procedure.

Value List Handler
‡ Used to retrieve large amounts of data ‡ Provides alternatives to EJB Finders for large queries. ‡ Cache query results on server side. ‡ Value List Handler sequence

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

‡ 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

Sign up to vote on this title
UsefulNot useful