Designing software with reusable object-oriented components is a difficult task. As a designer coming up with a design from scratch for each and every problem you face is a nightmare. When designing a solution, first you need to solve the existing problem, then you need to generalize it as it may have to solve problems which are yet to come. Also when new requirements come in the future, your solution should accommodate those changes with minimal design changes. That’s why experienced designers reuse the solutions that have worked for them in the past for similar scenarios.
What is a design pattern?
A design pattern is a reusable and tested solution for commonly occurring object-oriented design problems. They make your software component loosely coupled, more flexible, reusable, and maintainable.
Why having knowledge of design patterns is important?
When you have to design a solution, you can check whether there is an existing solution for your problem as most of the problems are common for most cases. If not you can either combine them or change them and create the design that you need.
Gang Of Four Design Patterns (GoF)
In 1994, four authors, Addison-Wesley, by Erich Gamma, Richard Helm, Ralph Johnson, John Vissides wrote a book: Design Patterns: Elements of Reusable Object-Oriented Software. There they have included 23 object-oriented design patterns which I’m going to describe in this article series. Even it was published 20 years ago, still we are using those design patterns and it still continues to be an Amazon bestseller. This book wrote in C++ context so here I’m going to describe and explain them relevant to Java.
These 23 design patterns categorized into 3 groups: Creational, Structural, and Behavioral patterns.
Let’s discuss each one of them.
Object Oriented Design Patterns
- Abstract Factory – Abstract Factory pattern an interface is responsible for creating a factory of related objects without explicitly specifying their classes
- Builder – Builder design pattern is to separate the construction of a complex object from its representation
- Prototype – This is used for a scenario where object creation is more expensive. It allows us to create a new object by copying a similar existing object with the given prototype.
- Singleton – This provides a global point of access to an object by ensuring that the object has only one instance.
- Factory Method – Define an interface for creating an object, but let subclasses decide which class to instantiate.
- Adopter – It allows the interface of an existing class to be used as another interface
- Bridge – Decouples an abstraction so two classes can vary independently.
- Composite – It groups the objects that need to be treated in the same way as they are in the same type.
- Decorator – Allows the user to add additional functionalities to an object dynamically.
- Facade – Hide the complexity of the program all provides a unified high-level interface.
- Flyweight – Increase memory usage by sharing common attributes of a large number of objects.
- Proxy – Ensures the security of an object by providing placeholder for other objects.
- Chain of Responsibility – By giving more than one object a chance to handle the request, it decouples the sender with the receiver.
- Command – Turns a request into a stand-alone object that contains all information about the request.
- Iterator – provides a uniform way to access different collections of objects.
- Mediator – Allows loose coupling by handling communication between objects through a layer.
- Memento – It provides a way to restore the previous state of an object.
- Observer – If the state of one object changed, all the dependent objects will notify.
- State – Allos object to change it’s the behavior when it’s internal state changes.
- Strategy – Create objects that contain various strategies/algorithms, and allows to select one at run time.
- Visitor – Allows defining new methods to classes of different types with fewer changes.
- Interpreter – It allows to change one interpretation of data into another
- Template Method – Allows subclasses to change certain steps of an algorithm without changing the algorithm’s structure.
Once I posted detailed explanation and code samples, click on the design pattern you need to learn and it will redirect to the relevant page. You don’t need to go one by one. You can start from any pattern. My suggestion is to start with the Factory Method.