The design patterns are strategies for solving common object-oriented design problems. Using the design patterns you can make your code more flexible, reusable and maintainable. We must use the design patterns during the analysis and requirement phase of SDLC (Software Development Life Cycle).

A design pattern is a general reusable solution to a commonly occurring problem in software design.

It is a description or template for how to solve a problem that can be used in many different situations.

Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.

Basically, java design patterns are categorized into two parts:

  1. Core java (or JSE) Design Patterns.
  2. JEE (Java Enterprise Edition) Design Patterns.

At a higher level there are architectural patterns that are larger in scope, usually describing an overall pattern followed by an entire system.

Java has many different types of design patterns:

  1. Structural patterns address concerns related to the high level structure of an application being developed.
  2. Computational patterns address concerns related to the identification of key computations.
  3. Algorithm strategy patterns address concerns related to high level strategies that describe how to exploit application characteristic on a computation platform.
  4. Implementation strategy patterns address concerns related to the realization of the source code to support how the program itself is organized and the common data structures specific to parallel programming.
  5. Execution patterns address concerns related to the support of the execution of an application, including the strategies in executing streams of tasks and building blocks to support the synchronization between tasks.

Design patterns were originally grouped into the next categories:

  1. creational patterns;
  2. structural patterns;
  3. behavioral patterns.

Core Java Design Patterns

In core java, there are mainly three types of design patterns, which are further divided into their sub-parts:

1. Creational Design Pattern

Creational design patterns are concerned with the way of creating objects.

These design patterns are used when a decision must be made at the time of instantiation of a class (i.e. creating an object of a class).

A list of creational design patterns in java:

  1. Factory Pattern. In Factory pattern, we create object without exposing the creation logic to the client and refer to newly created object using a common interface.
  2. Abstract Factory Pattern
  3. Singleton Pattern
  4. Prototype Pattern
  5. Builder Pattern.

2. Structural Design Pattern

Structural design patterns are concerned with how classes and objects can be composed, to form larger structures.

The structural design patterns simplifies the structure by identifying the relationships. These patterns focus on, how the classes inherit from each other and how they are composed from other classes.

A list of structural design patterns in java:

  1. Adapter Pattern
  2. Bridge Pattern
  3. Composite Pattern
  4. Decorator Pattern
  5. Facade Pattern
  6. Flyweight Pattern
  7. Proxy Pattern

3. Behavioral Design Pattern

Behavioral design patterns are concerned with the interaction and responsibility of objects. In these design patterns, the interaction between the objects should be in such a way that they can easily talk to each other and still should be loosely coupled.

A loosely coupled system is one in which each of its components has, or makes use of, little or no knowledge of the definitions of other separate components. Sub-areas include the coupling of classes, interfaces, data, and services.

That means the implementation and the client should be loosely coupled in order to avoid hard coding and dependencies.

A list of behavioral design patterns in java:

  1. Chain Of Responsibility Pattern
  2. Command Pattern
  3. Interpreter Pattern
  4. Iterator Pattern
  5. Mediator Pattern
  6. Memento Pattern
  7. Observer Pattern
  8. State Pattern
  9. Strategy Pattern
  10. Template Pattern
  11. Visitor Pattern

Java Enterprise Edition Patterns

Presentation Tier Patterns in java:

  1. Intercepting Filter;
  2. Front Controller;
  3. View Helper;
  4. Composite View;
  5. Service to Worker;
  6. Dispatcher View.

Business Tier Patterns in java:

  1. Business Delegate;
  2. Value Object;
  3. Session Facade;
  4. Composite Entity;
  5. Value Object Assembler;
  6. Value List Handler;
  7. Service Locator.

Integration Tier Patterns in java:

  1. Data Access Object;
  2. Service Activator.

Model-View-Controller pattern

Another classification has also introduced the notion of architectural design pattern that may be applied at the architecture level of the software such as the Model-View-Controller pattern.

DTO is an abbreviation for Data Transfer Object. 

DTO is used to transfer the data between classes and modules of your application.

Data Transfer Object should only contain private fields for your data, getters, setters and constructors. It is not recommended to add business logic methods to such classes, but it is OK to add some util methods.

DAO is an abbreviation for Data Access Object.

DAO should encapsulate the logic for retrieving, saving and updating data in your data storage (a database, a file-system, whatever).

Data Access Object Pattern or DAO pattern is used to separate low level data accessing API or operations from high level business services.

The Model-View-Controller is a wider pattern. The DTO/DAO would be your model in the MVC pattern. It tells you how to organize the whole application, not just the part responsible for data retrieval.

The MVC (Model View Controller) design pattern is a pattern/architecture that can be used by GUI’s. It seperates the application’s data, user interface and control logic into three separate entities.

This ensures the system will be more maintainable in the future as changes to one component will not have an affect on the others.

The MVC design pattern consists of three modules - model, view and controller.

  • Model - The model represents the state (data) and business logic of the application.
  • View - The view module is responsible to display data i.e. it represents the presentation.
  • Controller - The controller module acts as an interface between view and model. It intercepts all the requests i.e. receives input and commands to Model / View to change accordingly.

Lazy loading

Lazy loading is a design pattern commonly used in computer programming to defer initialization of an object until the point at which it is needed.

There are four common ways of implementing the lazy load design pattern:

  1. lazy initialization;
  2. a virtual proxy;
  3. a ghost, and
  4. a value holder.