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:
- Core java (or JSE) Design Patterns.
- 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:
- Structural patterns address concerns related to the high level structure of an application being developed.
- Computational patterns address concerns related to the identification of key computations.
- Algorithm strategy patterns address concerns related to high level strategies that describe how to exploit application characteristic on a computation platform.
- 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.
- 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:
- creational patterns;
- structural patterns;
- 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:
- 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.
- Abstract Factory Pattern
- Singleton Pattern
- Prototype Pattern
- 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:
- Adapter Pattern
- Bridge Pattern
- Composite Pattern
- Decorator Pattern
- Facade Pattern
- Flyweight Pattern
- 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:
- Chain Of Responsibility Pattern
- Command Pattern
- Interpreter Pattern
- Iterator Pattern
- Mediator Pattern
- Memento Pattern
- Observer Pattern
- State Pattern
- Strategy Pattern
- Template Pattern
- Visitor Pattern
Java Enterprise Edition Patterns
Presentation Tier Patterns in java:
- Intercepting Filter;
- Front Controller;
- View Helper;
- Composite View;
- Service to Worker;
- Dispatcher View.
Business Tier Patterns in java:
- Business Delegate;
- Value Object;
- Session Facade;
- Composite Entity;
- Value Object Assembler;
- Value List Handler;
- Service Locator.
Integration Tier Patterns in java:
- Data Access Object;
- Service Activator.
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 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:
- lazy initialization;
- a virtual proxy;
- a ghost, and
- a value holder.