J2EE Design Pattern Notes


These are notes I made while preparing for SCEA 5. I would like to thank my friend Sajitha for helping in scribing these notes.

1.     J2EE Design Patterns

1.1.             Application Controller

Centralize retrieval and invocation of request processing components such as commands and views

Advantages

  • Front controller acts as the centralized access point and controller for incoming requests, whereas application controller is a component for identifying and invoking commands / actions and dispatching to views.
  • Improves modularity – Action and view management code in its own class
  • Better reusability and extensibility

Disadvantages

  •  

Example              Struts 1.x RequestProcessor ,  ActionServlet to some extent

1.2.             Application Service

Provides a central location to implement business logic that encapsulates Business Object and services

  •  

Advantages

  • Centralized reusable business and workflow logic that acts upon multiple Business objects
  • Better reusability by encapsulating inter-business object operations
  • Avoids duplication of code
  • Simplifies façade implmentation

Disadvantages

  • Additional layer introduced in the business tier

Example

1.3.             Business Delegate

Encapsulates access to a business service – Acts like client side business abstraction

Advantages

  • Reduces coupling, better maintainability as client does not directly depend on business service
  • Translates business service exceptions
  • Improves availability as it can implement automatic recovery [in case of service failure] without exposing error to client
  • Exposes simpler uniform interface to the service
  • Can improve performance by caching into for common service requests
  • Hides remoteness, lookup etc

Disadvantages

  • Introduces another layer

Example

1.4.             Business Object

Separates business data and logic using an object model.  Business objects encapsulate and manage business data, behavior and persistence

  •  

Advantages

  • Object oriented approach to business model implementation
  • Centralized business behavior and state promotes reuse
  • Separates persistence logic from business logic

Disadvantages

  • Plain old Java object [POJO, i.e. not JPA or EJB] implementation can induce and are susceptible to stale data [when used in distributed multi tier applications]
  • Adds extra layer of indirection.  May not be suitable when
    • Business model and business logic are trivial
    • Data model is a sufficient representation of the business model and letting presentation components access data directly using DAO is simpler
  • Can result in bloated objects when more and more use-case specific behavior is implemented in BO

Example

1.5.             Composite Entity

Implements persistent Business objects using local Entity Beans and POJOs. Aggregates a set of related BOs into coarse grained Entity Bean implementations

  •  

Advantages

  • BOs can be implemented as parent objects and dependent objects
  • A parent object is reusable, independently deployable and manages its relation to other objects [May contain dependent objects[DOs]]
  • A dependent object can be a simple self-contained object or may contain other dependent objects [DOs]
  • Increases maintainability as number of fine grained entity beans is reduced
  • In EJB 2.x or later, dependent objects can be implemented as local entity beans to take advantage of Container Managed Persistence [CMP] and Container Managed Relationships [CMR]
  • Improves network performance by coarse grain entities
    • Improves overall performance in EJB 1.1
    • In EJB 2.x implementing BO As composite entity with local entity beans has the same benefit. 
  • Facilitates composite Transfer Object creation

Disadvantages

  • Local entity beans could impact performance – POJOs are faster than local entities due to container services

Example

1.6.             Composite View

View is composed of multiple atomic sub-view.  Each sub-view can be included dynamically into the whole and the layout of the page can be managed independent of the contents

Advantages

  • Improves modularity and reuse [e.g. header & footer]
  • Adds role based or policy based control as composite view and can conditionally include sub-views
  • Managing changes to portions of a template is more easy

Disadvantages

  • Aggregating atomic sub-view may result in potential display errors which can become a maintainability issue
  • Runtime inclusion of sub-views may have minimal performance hit

Example      Tiles

1.7.             Context Object

Encapsulate state in a protocol independent way to be shared throughout the application.

Advantages

  • Improves reusability and maintainability [application is not polluted with protocol specific data types]
  • Easy testing as context encapsulates protocol information
  • Reduces constraints on evolution of interfaces [ one context as parameter instead of numerous objects as parameters]

Disadvantages

  • Reduces performance because state is transferred from one object to another

Example      ServletContext used by Servlets.   Protocol specific into is in concrete subclasses like HTTPServletContext

1.8.             Data Access Object

Used to abstract and encapsulate all access to persistence store.  DAO manages connection with datasource to obtain and store data. 

  • Stateless
  • Does not cache data

Advantages

  • Enables transparency for clients [to the location and implementation of persistent storage mechanism]
  • Provides Object oriented view and encapsulates database schema
  • Easier migration to different databases as only DAO needs to change
  • Organizes all data access code into a separate layer

Disadvantages

  • Adds extra layer
  • Needs class hierarchy design – Prefer factory method strategy over Abstract Factory strategy
  • Introduces complexity to enable object oriented design

Example     

1.9.             Dispatcher View

When minimal / limited business processing required, use Dispatcher view with views as the initial access point being a request.  The minimal business processing is managed by view.

  • Differs from ‘Service to worked’ – Business processing is done after control is transferred to the view

Advantages

  • Leverages frameworks and libraries like JSTL
  • Separates processing logic from view [to View Helper]

Disadvantages

  • Poor separation of View from Model and Control logic

Example              A direct link to a static HTML page or JSP that displays existing presentation model [data already in session]

1.10.        Domain Store

Used to transparently persist an object model. Unlike CMP or BMP [in EJB 2.0], where persistence code is in object model, Domain store’s persistence mechanism us separate from object model

  •  

Advantages

  • Separates Bean Management from persistence logic
  • Improves testability, Bean management can be tested without actually persisting
  • Improves understanding of 3rd party persistence framework

Disadvantages

  • Creating a custom persistence framework is complex
  • Creating custom persistence might be an overkill for a small object model
  • Multi layer object tree loading and storing requires optimization techniques

Example      EJB3 – EntityManager, Hibernate – SessionManager

1.11.        Front Controller

Centralized access point for presentation tier request handling

Advantages

  • Initial point of contact for handling all related requests
  • Improves manageability
  • Improves reusability [ common code moves to controller]
  • Improves role separation

Example      Struts 1.x  ActionServlet

1.12.        Intercepting Filter

Intercept and manipulate a request and a response before and after the request is processed in a centralized way.

Advantages

  • Centralized common processing across requests
  • Pre and post processing components loosely coupled
  • Filter manager combines loosely coupled filters in a chain
  • Improves reusability
  • Declarative and flexible configuration

Disadvantages

  • Sharing into among filters may be costly as they are loosely coupled

Example              Servlet Filters

1.13.        Service Activator

Used to receive asynchronous requests and invoke one or more business services.  It is implemented as a JMS listener

  •  

Advantages

  • Integrates JMS into enterprise application
  • Provides asynchronous processing for any business tier component
  • Enables standalone JMS listener [no container support needed]

Disadvantages

  • EJB 2.x or later can use MDBs instead

Example     

1.14.        Service Locator

Implements and encapsulates service and component lookup. Hides the details of lookup mechanism and encapsulates related dependencies

  •  

Advantages

  • Abstracts complexity of lookup from the client
  • Uniform service access to clients
  • Facilitates adding new components (as client is not aware of EJBHome objects or JMS connection factories, new components can be added without impacting client
  • Can improve network performance by aggregating network calls required to lookup and create components
  • Can improve performance by caching[e.g. using InitialContext or Factory objects]

Disadvantages

  •  

Example     

1.15.        Service to Worker

Centralize control and request handling to retrieve a presentation model before turning control over to the view. The view generates a dynamic response based on presentation model. 

This pattern involves multiple patterns.

  • Application Controller – Request Handling
  • Front Controller – Centralized control
  • View Helper – View creation

Advantages

  • Better modularity, reusability, maintainability
  • Better role separation

Disadvantages

  •  

Example      ActionServlet,    RequestProcessor, and Action

Retrieve / construct ActionForm and process business logic before forwarding to JSP.

1.16.        Session Façade

Encapsulates business tier components by exposing a coarse-grained service to remote clients.

Benefits:

  • Manageability
  • Centralized logic
  • Flexibility

Advantages

  • Session Façade implemented as Session Bean interacts with business components such as Business objects and application service
  • Reduces coupling between tiers
  • Reduces complexity
    • Using application service reduces complexity of Session Façade
    • Using business delegate to access session façades reduces complexity of client
  • Improves performance by reducing fine grained remote calls
  • Centralizes transaction control with the coarse grained method
  • Exposes fewer remote interfaces to client

Disadvantages

  •  

Example     

1.17.        Transfer Object

It is used to carry multiple data elements across a tier. Transfer object is pass-by-value between tiers [if they are remote]

  •  

Advantages

  • Reduces network traffic
  • Simplifies remote interface as it can use coarse grained getData() type methods
  • Can reduce code duplication by using Entity inheritance TOs
  • With JPA entities are POJOs with annotation, once detached from entity manager, they are like TO – However for better control, validation etc, it may still be useful to have TOs separate

Disadvantages

  •  

Example      ActionForm

1.18.        Transfer Object Assembler

Used to build an application model as a composite Transfer Object.  It aggregates multiple Transfer Objects from various business components and services and returns it to the client

  •  

Advantages

  • Reduces coupling between clients and the application model
  • Improves network performance by reducing number of remote calls [i.e. helps in getting many Transfer Objects at once]
  • Potential to introduce stale data

Disadvantages

  •  

Example

1.19.        Value List Handler

Value list handler is used to search, cache [the result]. And allow client to tranverse and select items from the result

  •  

Advantages

  • Provides search and iteration functionality. Uses DAO to get results for a query from database
  • Efficient alternative to EJB finders
  • Caches search results
  • Better network performance – only a subset of results is sent to the client instead of the entire list
  • Allows deferring entity bean transaction

Disadvantages

  • Creating a large list of Transfer Objects can be expensive – Mitigate this by creating Transfer object for a limited number of records from the query result

Example     

1.20.        View Helper

Separates view from its processing logic. Use views to encapsulate formatting and helpers to encapsulate view processing logic

  • Helpers are adapters between View and Model [ like in the Model-View-Controller]

Advantages

  • Better application partitioning, reuse and maintainability
  • Eases testing as processing logic is in helper
  • Helper usage mirrors scriptlets

Examples    Tag classes [e.g. to handle if-else check]
Action & ActionForm classes in Struts

1.21.        Web Service Broker

Used to expose one or more services using XML and web protocols.  A web service broker is a coarse-grained service exposed as a web service.  It co-ordinates interactions among one or more services, aggregates responses and may demarcate and compensate transactions

  •  

Advantages

  • Introduces a layer between client and service
  • Existing remote session façade need to be refactor-ed to support local access for the broker
  • Network performance may be impacted due to web protocols

Disadvantages

  •  

Examples

Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

One Response to “J2EE Design Pattern Notes”

  1. desain kaos Says:

    desain kaos…

    […]J2EE Design Pattern Notes « The Tech Tips Blog[…]…

Leave a comment