j2ee design patterns sharath sahadevan august 8, 2002 st louis java sig
TRANSCRIPT
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 )
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 Façade
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 .
Controller sequence diagram
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>
• </filter>
• <filter-mapping>
• <filter-name>Image Filter</filter-name>
• <url-pattern>/*</url-pattern>
• </filter-mapping>
Session Facade
A façade is usually provided to hide the underlying complexity from the client.
A session bean is used as a session façade to perform coarse grained functionality .
The session bean will probably interact with two or more entity beans .
A session façade 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.
• 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
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 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
Use a Data Access Object ( DAO ) to abstract all access to a data source.
The DAO will help to hide details of access to the data source from the client.
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 .
Abstracts complexity
Provides uniform service access to Clients
Improves performance
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 ?
Sequence Blocks
UUID for EJB
Stored Procedures for Autogenerated keys
Primary Key Generation strategies
• Sequence blocks Uses a stateless session bean and a CMP entity bean .
The CMP entity bean represents a sequence in the database.
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
Stored Procedures for Autogenerated key
Stored procedures are used to insert the data and return the generated key . The stored procedure is called from the entity beans ejbCreate() method.
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
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 Monson-Haefel