Enterprise Design Patterns in Java

Enterprise design patterns are a set of best practices and reusable solutions for common problems that arise when developing large-scale applications in Java. These patterns help to improve the performance, scalability, and maintainability of code, making it easier for developers to build and manage complex software systems.

Some common enterprise design patterns in Java include:

  • Singleton Pattern: This pattern ensures that a class has only one instance and provides a global point of access to it.
  • Factory Pattern: This pattern provides a way to create objects without specifying the exact class of object that will be created.
  • Observer Pattern: This pattern allows objects to be notified of changes in the state of another object.
  • Decorator Pattern: This pattern allows objects to be added or wrapped around other objects dynamically, providing additional functionality.
  • Model-View-Controller (MVC) Pattern: This pattern separates the representation of information from the user’s interaction with it.
  • Data Access Pattern: The Data Access Pattern is a design pattern that provides a unified way to access data from various sources such as databases, file systems, and web services.
  • Facade Pattern: The Facade Pattern is a design pattern that provides a simplified interface to a complex system, hiding its internal complexity and providing a unified way to interact with it.
  • Dependency Injection Pattern: The Dependency Injection Pattern is a design pattern that enables a client to obtain its dependencies from an external source, rather than creating them directly. This pattern allows for more flexible and maintainable code, as well as easier testing.

This article, we’ll go through a summary of each of these Enterprise Java design Patterns.

 

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.

 

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.

 

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.

 

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

 

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.

 

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.

 

 

References:

GOF Design Patterns

Professional Java EE Design Patterns

Head First Design Patterns

© 2015 – 2023, 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: