Software Design patterns in java are a custom set of best practices that are reusable in solving common programming issues. They solve the most common design-related problems in software development. The basic idea for creating the Design patterns is creating the relationship between classes and objects in a different way resulting in fast development along with a more flexible, reusable, and maintainable set of code.
Design patterns are always implemented for a need in project development which solves the most common problems with in the project. To decide when to go with which pattern one must have an idea about the structure of all the design patterns and their usages.
For example, we do not want to create multiple duplicated objects as it can lead to some serious issues like memory leaks and low-performance in the application. As a solution to this serious and most-common problem, we can implement the prototype pattern which uses the clone() method to duplicate the objects that can be used as a prototype.
Types of Design Patterns
There’s plenty of design patterns available and we are also familiar with some of them already. Being more related to them can help us to increase the speed and quality of the work.
There are three main design pattern categories, Creational Design patterns, Structural Patterns, and Behavioural patterns.
1. Creational Design Patterns
The Creational patterns provide solutions by class instantiation and object creations. They make use of inheritance and delegation effectively to implement a correct solution. Details of each of the creational design patterns are listed below.
- Abstract Factory – Sets of methods to make various objects.
- Builder – Make and return one object in various ways.
- Factory Method – Methods to make and return components of one object in various ways.
- Prototype – Make new objects by cloning the objects which you set as prototypes.
- Singleton Pattern – A class distributes the only instance of itself.
- Object Pool Design Pattern – Use for a significant performance boost.
An example, the most famous Spring Framework is the best example of the creational design pattern in which IOC works on a Factory pattern. Here the creation logic of the objects is not known to the client itself. The client also uses the common interface for the creation of a new object.
2. Structural Design Patterns
The Structural patterns provide solutions by composing the classes and objects resulting to form larger structures. This helps to ease the design by identifying the simplest way to understand the relationships between entities.
- Adapter Pattern – Match interfaces of different classes
- Bridge – Separates an object’s interface from its implementation
- Composite – A tree structure of simple and composite objects
- Decorator – Add responsibilities to objects dynamically
- Facade – A single class that represents an entire subsystem
- Flyweight – A fine-grained instance used for efficient sharing Private Class Data Restricts accessor/mutator access
- Proxy – An object representing another object
An example, to add new functionality within an existing object without changing its original structure by acting as a wrapper if the existing class. This pattern is called the decorator pattern which wraps the original class and provides the additional functionality.
3. Behavioural Design Patterns
The Behavioral Patterns provide solutions by creating object interactions like how object communicates and how they are dependent on others along with how to segregate them to be both dependent and independent and provide both flexibility and testing capabilities.
- Chain of responsibility – A way of passing a request between a chain of objects
- Command – Encapsulate a command request as an object
- Interpreter – A way to include language elements in a program
- Iterator – Sequentially access the elements of a collection
- Mediator – Defines simplified communication between classes
- Memento – Capture and restore an object’s internal state
- Null Object – Designed to act as a default value of an object
- Observer – A way of notifying a change to a number of classes
- State – Alter an object’s behavior when its state changes
- Strategy – Encapsulates an algorithm inside a class
- Template method – Defer the exact steps of an algorithm to a subclass
- Visitor – Defines a new operation to a class without change
For example, an object guiding other objects about their responsibilities. A linked list of handlers, which is able to process the requests. once the request is processed to the list, it is automatically transferred to the first handler available in the list which will process the request further. This is an example of a chain of responsibility design pattern.
You will learn all the above discussed Designed Patterns in Java. All the code examples are available on GitHub.