Software Design patterns in java are a custom set of practices that are reusable and used in programming. 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 the idea about the structure of all the design patterns and the uses for the same.
For instance, 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 as follow:
- Creational Patterns: Creational patterns provide solutions by class instantiation and object creations. They use of inheritance effectively and delegation effectively to implement a correct solution, We will look at all the creational patterns available and the descriptions of those in further related posts.
The most famous framework spring is the best example for the creational design pattern in which IOC works on a Factory pattern, where 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.
- Structural Patterns: 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.
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.
- Behavioral Patterns: 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.
An object guiding other objects about their responsibilities. For example, 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.
List of Patterns
1. Creational Patterns
- 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 – A class distributes the only instance of itself.
2. Structural patterns
- Adapter – 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
3. Behavioral patterns
- 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
You will learn all the above discussed Designed Patterns in Java. All the code examples are available on GitHub.