![Page 1: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/1.jpg)
J2EE Design patterns
Sharath Sahadevan
August 8 , 2002
St Louis Java SIG
![Page 2: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/2.jpg)
Design Patterns ?
• What are Design Patterns ?
![Page 3: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/3.jpg)
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.
![Page 4: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/4.jpg)
Why study patterns ?
• Develop better products.
• Learn from others experience.
• Improve communication with others in the same field.
• Don’t reinvent the wheel.
![Page 5: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/5.jpg)
Brief History of Patterns
• In 1970, Christopher Alexander - documented patterns in Civil Engineering and architecture
• Software design patterns popularized by GOF ( Gang of Four )
![Page 6: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/6.jpg)
J2EE
• Java 2 Platform ,Enterprise Edition
• Provides a unified platform for developing distributed , server-centric applications.
![Page 7: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/7.jpg)
J2EE Patterns
Front Controller
View Helper
Dispatcher View
Service To Worker
Intercepting Filter
![Page 8: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/8.jpg)
J2EE patterns
Service Locator
Session Façade
Message Facade
Business Delegate
Value Object
![Page 9: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/9.jpg)
J2EE Patterns
Value List Handler
Primary key generation strategies
Data Access Object
Resource Adapter
![Page 10: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/10.jpg)
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
![Page 11: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/11.jpg)
Front Controller
• Advantages Promotes reuse of common code that is needed for all
requests .
Promotes flexibility
Easier to maintain
![Page 12: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/12.jpg)
Front Controller
Avoid fat controllers .
Do not restrict site to one controller.
Different subsystems could have their own controllers.
![Page 13: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/13.jpg)
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
![Page 14: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/14.jpg)
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
![Page 15: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/15.jpg)
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.
![Page 16: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/16.jpg)
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.
![Page 17: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/17.jpg)
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 …
![Page 18: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/18.jpg)
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.
![Page 19: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/19.jpg)
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>
![Page 20: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/20.jpg)
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.
![Page 21: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/21.jpg)
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
![Page 22: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/22.jpg)
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 .
![Page 23: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/23.jpg)
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
![Page 24: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/24.jpg)
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 .
![Page 25: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/25.jpg)
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
![Page 26: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/26.jpg)
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
![Page 27: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/27.jpg)
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
![Page 28: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/28.jpg)
Primary Key Generation strategies
• How can we generate primary keys for entity beans ?
Sequence Blocks
UUID for EJB
Stored Procedures for Autogenerated keys
![Page 29: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/29.jpg)
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.
![Page 30: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/30.jpg)
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 .
![Page 31: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/31.jpg)
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.
![Page 32: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/32.jpg)
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
![Page 33: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/33.jpg)
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.
![Page 34: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/34.jpg)
Resource Adapter
• Resource Adapters are packaged in a .rar file and deployed on the application server.
• J2EE connector architecture
![Page 35: J2EE Design patterns Sharath Sahadevan August 8, 2002 St Louis Java SIG](https://reader035.vdocuments.us/reader035/viewer/2022062515/56649cca5503460f94993116/html5/thumbnails/35.jpg)
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