Enterprise Design Patterns in Java

This article provides an introduction to Enterprise Java design Patterns.

 

MVC Pattern

Model represents application’s data and business logic.

View is the visual representation of data in model.

Controller links the view to the model and directs application flow.

 

In Java EE, model is located in business layer, usually in form of an EJB module.

Controller and view are in web tier.

View is likely constructed in html, jsp, jsf etc.

Controller is normally a servlet that receives HTTP requests from user.

 

Data Access Pattern

This pattern uses Data Access Objects to abstract all access to data source. All data access operations happen through the DAO layer.

Dao pattern has following components :

  • DAO Interface
  • DAO interface implementation
  • DAO factory
  • Data Transfer Object (DTO)

DTO is also called the value object. It carries data from data access layer to web layer.

In Java EE, Java persistence API(JPA) is used for data access using Object relational mapping.

The tables are mapped to a POJO annotated as @Entity. The members of this POJO class are also annotated and mapped to fields(columns).

 

Façade pattern:

Façade provides the full power of subsystem via as easy to use interface

Façade pattern is commonly used in following scenarios:

  • provide interface to legacy backend system
  • to reduce n/w calls. Façade makes many calls to subsystems, but remote client makes only one call to façade
  • to encapsulate flow and inner details for data security

Facades can be implemented using Stateful/Stateless session beans .

The façade class can be injected with the actual services using @Inject and it can have the logic to call the appropriate service/EJB.

 

Singleton pattern:

Ensure a class has only instance and provide a global point of access to it.

Used in following scenarios:

  • to access shared data across whole application, such as configuration data
  • to create logger instance.. usually only one instance is needed in application
  • to implement cache that can cache static data/resources and improve performance

Spring creates singleton beans by default.

Java uses singleton pattern in the implementation of runtime class.

In Java EE, just by adding @Singleton annotation to a class, you can turn it into a singleton bean.

 

Dependency Injection:

In DI, instead of creating hard dependencies and creating new objects using new or lookups, the required resource is injected to the object through xml configuration or annotations.

Spring provides Dependency injection.

Java EE and EJB 3 provides several annotations for this :

@Resource for injecting data source, url etc

@EJB for injecting EJB

@WebServiceRef for injecting webservices

EJB 3.1 added a new annotation @Inject. This provided a common interface for injection between other J2EE frameworks and Java env.

 

Factory pattern

Abstracts the logic of object creation from where the objects are used

Often factory is implemented as a singleton or static class because normally only one instance of factory is required.

In Java EE, you can use @Produces annotation to create an object and @Inject to inject the cretated object where its required.

 

Decorator pattern

In decorator pattern, purpose is to wrap an object so that you can dynamically add responsibilities at runtime.

Java BufferedInputsteam is a good example of decorator.

Decorator implementation in Java EE introduces annotations @Decorator & @Delegate.

@Decorator annotates the Decorator class.

@Delegate annotates delegate injection point where the decorated class is injected

 

Observer pattern

In Observer pattern, the object that changes its state informs other objects about it. The objects that are notified are called observers and the object that notified them is called Subject.

Example of this is chat room application. Observer is the chat server and each client is a subject.

Java EE offers observer implementation using @Observes annotation and Events interface.

Any method annotated with @Observes listens for certain type of events.

 

 

References:

GOF Design Patterns

Professional Java EE Design Patterns

Head First Design Patterns

© 2015, https:. All rights reserved. On republishing this post, you must provide link to original post

One comment

  1. […] Article 17: Enterprise Design Patterns in Java […]

Leave a Reply.. code can be added in <code> </code> tags

%d bloggers like this: