Saturday, August 19, 2023

Demystifying Design Patterns in Java

 It all began when a famous group called the "Gang of Four" wrote a book about Design Patterns. The Gang of Four included Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. They came up with 23 different patterns to solve problems that often appear when designing software.

These patterns are divided into three groups:

  1. Creational Patterns: These help us figure out the best way to create objects for different situations.

    • Singleton: Use this when you only want one instance of a class.
    • Factory Method: Creates objects from a group of classes based on certain conditions.
    • Abstract Factory: It's like a higher level of the factory method. It picks a factory based on certain conditions.
    • Builder: Builds complex objects step by step. It keeps the process of creating an object separate from how it's shown.
    • Prototype: Makes a copy of an existing object.

  2. Structural Patterns: These help us put classes and objects together to make new functions.


    • Adapter: Helps two different things work together even if they don't fit well at first.
    • Decorator: Adds or changes things about an object without changing it too much.
    • Composite: Helps build structures with parts inside other parts.
    • Facade: Makes a simple way to use a complicated system with many parts.
    • Flyweight: Saves time and memory by using objects that already exist.
    • Proxy: Acts as a stand-in for another object, often for controlling access or adding extra features.
    • Bridge: Separates how something works from how it looks so they can change without affecting each other.

  3. Behavioral Patterns: These are about how objects communicate with each other.


    • Chain of Responsibility: Keeps the sender of a request separate from who gets it.
    • Memento: Lets an object go back to an older state.
    • Template: A main class gives the basic steps of a plan, and special classes add the details.
    • Observer: Sets up a way for one object to tell many others about changes.
    • State: Makes an object behave differently when its situation changes.
    • Iterator: Helps go through a group of things without knowing how they're stored.
    • Mediator: Helps objects communicate without being too connected.
    • Command: Sets up a way to ask for things without knowing what happens behind the scenes.
    • Strategy: Chooses one way of doing something from a group of ways.
    • Visitor: Lets you make a new action without changing the things it works on. It moves the action to a different class.
    • Interpreter: Does things with a special language. It has rules for the language and a way to understand and do the rules.

These design patterns are like tools in a programmer's toolbox. Each pattern helps you solve a certain kind of problem in a smart and organized way. Just like following a recipe when cooking, using these patterns can make your code easier to understand and maintain.

Must Read Design Pattern Articles: 

No comments:

Spring Framework: A Comprehensive Guide

 The Spring Framework is a powerful and versatile framework for building robust and scalable Java applications. Over the years, it has gaine...