OOP Design Patterns – All you need to know.

OOP Design Patterns - All you need to know. - prasadct.com

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

Creational Patterns

Structural Patterns

  • AdopterIt allows the interface of an existing class to be used as another interface
  • BridgeDecouples an abstraction so two classes can vary independently.
  • CompositeIt 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.
  • FacadeHide 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.
  • ProxyEnsures the security of an object by providing placeholder for other objects.

Behavioral Patterns

  • Chain of Responsibility By giving more than one object a chance to handle the request, it decouples the sender with the receiver.
  • CommandTurns a request into a stand-alone object that contains all information about the request.
  • Iteratorprovides a uniform way to access different collections of objects.
  • MediatorAllows loose coupling by handling communication between objects through a layer.
  • Memento It provides a way to restore the previous state of an object.
  • ObserverIf 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.
  • InterpreterIt allows to change one interpretation of data into another
  • Template MethodAllows 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.

5 thoughts on “OOP Design Patterns – All you need to know.

  1. Thanks for another great post. Where else could anybody get that type of info in such a perfect way of writing? I’ve a presentation next week, and I am on the look for such info.

Leave a Reply

Your email address will not be published. Required fields are marked *