IT Design patterns

From A Technological Wiki
Revision as of 21:54, 20 October 2025 by Root (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Design patterns are proven solutions to common problems that occur during software design. They represent best practices, evolved over time and refined by experienced practitioners. Rather than being specific code snippets, they are more like templates or general strategies to solve particular design issues.

Benefits

  • Reusability: They are tried and tested solutions that can be applied in many different scenarios.
  • Understandability: They provide a common vocabulary for developers.
  • Maintainability: Using known patterns can make the software more organized and easier to maintain.
  • Scalability: Many design patterns, when applied correctly, can help make systems more scalable.
  • Avoid Common Pitfalls: Since they are based on the collective experience of developers, they help avoid common mistakes.

When are they used?

  • During system design to decide the high-level architecture of the system.
  • During detailed design to decide how components will interact.
  • When refactoring code to improve its structure or to solve certain recurrent problems.

Fundamental design patterns

Creational Pattern (Singleton)

Ensures that a class has only one instance and provides a global point to access it.

class Singleton {
private static $instance;

    private function __construct() {}

    public static function getInstance() {
        if (null === static::$instance) {
            static::$instance = new static();
        }

        return static::$instance;
    }

    private function __clone() {}

    private function __wakeup() {}
}

$instance = Singleton::getInstance();

Structural Pattern (Adapter)

Allows incompatible interfaces to work together.

interface OldInterface {
    public function oldRequest();
}

class OldClass implements OldInterface {
    public function oldRequest() {
        return "Old Request";
    }
}

interface NewInterface {
    public function request();
}

class Adapter implements NewInterface {
private $oldClass;

    public function __construct(OldClass $oldClass) {
        $this->oldClass = $oldClass;
    }

    public function request() {
        return $this->oldClass->oldRequest();
    }
}

$old = new OldClass();
$adapter = new Adapter($old);
echo $adapter->request();

Behavioral Pattern (Observer)

Allows an object to publish changes to its state so other objects can react accordingly.

interface Observer {
    public function update($message);
}

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

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

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

    public function detach(Observer $observer) {
        foreach ($this->observers as $key => $obs) {
            if ($obs === $observer) {
                unset($this->observers[$key]);
            }
        }
    }

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

    public function setMessage($message) {
        $this->message = $message;
        $this->notify();
    }
}

class ConcreteObserver implements Observer {
    public function update($message) {
        echo "Observer received message: $message\n";
    }
}

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

$subject->attach($observer1);
$subject->attach($observer2);
$subject->setMessage("Hello, Observers!");