April 22, 2025

There are many types of design patterns, but they can be broadly categorized into three main categories:

  1. Creational Patterns:

Creational patterns are concerned with object creation mechanisms, trying to create objects in a manner suitable to the situation. They help abstract the process of object creation and make the code more flexible and reusable.

Examples of creational patterns include:

  • Singleton Pattern
  • Factory Method Pattern
  • Abstract Factory Pattern
  • Builder Pattern
  • Prototype Pattern
  1. Structural Patterns:

Structural patterns are concerned with object composition, i.e., how objects are made up and relate to each other. They help simplify the relationships between objects and make the code more flexible and adaptable.

Examples of structural patterns include:

  • Adapter Pattern
  • Bridge Pattern
  • Composite Pattern
  • Decorator Pattern
  • Facade Pattern
  • Flyweight Pattern
  • Proxy Pattern
  1. Behavioral Patterns:

Behavioral patterns are concerned with communication between objects, i.e., how objects communicate with each other and how they cooperate to perform a task. They help simplify the communication between objects and make the code more flexible and reusable.

Examples of behavioral patterns include:

  • Chain of Responsibility Pattern
  • Command Pattern
  • Interpreter Pattern
  • Iterator Pattern
  • Mediator Pattern
  • Memento Pattern
  • Observer Pattern
  • State Pattern
  • Strategy Pattern
  • Template Method Pattern
  • Visitor Pattern

Each pattern has its own set of benefits and drawbacks, and the choice of which pattern to use will depend on the specific problem being solved. By using design patterns, developers can create flexible and adaptable software that is easier to maintain and extend over time.

Design patterns are reusable solutions to common software design problems that have been discovered and documented by software developers. These patterns provide a standard way of solving problems and can help improve code quality, maintainability, and scalability. Here are some examples of design patterns:

  1. Singleton Pattern:

The Singleton pattern is a creational pattern that ensures only one instance of a class is created and provides a global point of access to that instance. It is often used in situations where there should only be one instance of a particular class, such as a database connection or configuration settings.

class Singleton {
    private static $instance;

    private function __construct() {
        // Private constructor to prevent instantiation from outside the class
    }

    public static function getInstance(): Singleton {
        if (!self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function showMessage() {
        echo "Hello, World!";
    }
}

$singleton = Singleton::getInstance();
$singleton->showMessage();
  1. Observer Pattern:

The Observer pattern is a behavioral pattern that allows objects to be notified of changes to another object’s state. This pattern is often used in event-driven systems or in situations where one object needs to know about changes in another object without coupling the two objects tightly together.

interface Subject {
    public function attach(Observer $observer);
    public function detach(Observer $observer);
    public function notify();
}

class ConcreteSubject implements Subject {
    private $observers = [];

    public function attach(Observer $observer) {
        $this->observers[] = $observer;
    }

    public function detach(Observer $observer) {
        $key = array_search($observer, $this->observers, true);
        if ($key !== false) {
            unset($this->observers[$key]);
        }
    }

    public function notify() {
        foreach ($this->observers as $observer) {
            $observer->update($this);
        }
    }
}

interface Observer {
    public function update(Subject $subject);
}

class ConcreteObserver implements Observer {
    public function update(Subject $subject) {
        // Do something with the updated state of the subject
    }
}

$subject = new ConcreteSubject();
$observer1 = new ConcreteObserver();
$observer2 = new ConcreteObserver();

$subject->attach($observer1);
$subject->attach($observer2);

$subject->notify();
  1. Factory Method Pattern:

The Factory Method pattern is a creational pattern that provides an interface for creating objects, but allows subclasses to decide which class to instantiate. This pattern is often used in situations where the creation of an object involves complex or conditional logic, or when there are multiple classes that implement a common interface.

interface Logger {
    public function log(string $message);
}

class FileLogger implements Logger {
    public function log(string $message) {
        // Log the message to a file
    }
}

class DatabaseLogger implements Logger {
    public function log(string $message) {
        // Log the message to a database
    }
}

abstract class LoggerFactory {
    abstract public function createLogger(): Logger;
}

class FileLoggerFactory extends LoggerFactory {
    public function createLogger(): Logger {
        return new FileLogger();
    }
}

class DatabaseLoggerFactory extends LoggerFactory {
    public function createLogger(): Logger {
        return new DatabaseLogger();
    }
}

$factory = new FileLoggerFactory();
$logger = $factory->createLogger();
$logger->log("Hello, World!");

These are just a few examples of the many design patterns that exist in software development. Each pattern has its own strengths and weaknesses, and the appropriate pattern to use will depend on the specific problem being solved.

Design Patterns have been divided into 3 categories which are listed below:

  1. Creational Design Patterns:
  • Abstract Factory: families of product objects
  • Builder: how a composite object gets created
  • Factory Method: subclass of an object that is instantiated
  • Prototype: class of object that is instantiated
  • Singleton: the sole instance of a class
  • 2. Structural Design Patterns:
  • Adapter interface to an object
  • Bridge: implementation of an object
  • Composite: structure and composition of an object
  • Decorator: responsibilities of an object without subclassing
  • Facade: interface to a subsystem
  • Flyweight: storage costs of objects
  • Proxy: how an object is accessed; its location
  • 3. Behavioral Design Patterns:
  • Chain of Responsibility: an object that can fulfill a request
  • Command: when and how a request is fulfilled
  • Interpreter: grammar and interpretation of a language
  • Iterator: how an aggregate’s elements are accessed, traversed
  • Mediator: how and which objects interact with each other
  • Memento: what private information is stored outside an object, and when
  • Observer: number of objects that depend on another object; how the dependent objects stay up to date
  • State: states of an object
  • Strategy: an algorithm
  • Template: Method steps of an algorithm
  • Visitor: operations that can be applied to object(s) without changing their class(es)

About The Author

Leave a Reply

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