1 / 35

J2EE Design patterns

J2EE Design patterns. Sharath Sahadevan August 8 , 2002 St Louis Java SIG. Design Patterns ?. What are Design Patterns ?. Design Patterns ?.

normandy
Download Presentation

J2EE Design patterns

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. J2EE Design patterns Sharath Sahadevan August 8 , 2002 St Louis Java SIG

  2. Design Patterns ? • What are Design Patterns ?

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

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

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

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

  7. J2EE Patterns • Front Controller • View Helper • Dispatcher View • Service To Worker • Intercepting Filter

  8. J2EE patterns • Service Locator • Session Façade • Message Facade • Business Delegate • Value Object

  9. J2EE Patterns • Value List Handler • Primary key generation strategies • Data Access Object • Resource Adapter

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

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

  12. Front Controller • Avoid fat controllers . • Do not restrict site to one controller. • Different subsystems could have their own controllers.

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

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

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

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

  17. 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 …

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  34. Resource Adapter • Resource Adapters are packaged in a .rar file and deployed on the application server. • J2EE connector architecture

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

More Related