IT Design patterns: Difference between revisions

From A Technological Wiki
Jump to navigation Jump to search
imported>Unknown user
No edit summary
 
No edit summary
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
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.


<span id="benefits"></span>
= 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.
<span id="when-are-they-used"></span>
= 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.
<span id="fundamental-design-patterns"></span>
= Fundamental design patterns =
<span id="creational-pattern-singleton"></span>
== Creational Pattern (Singleton) ==
Ensures that a class has only one instance and provides a global point to access it.
<syntaxhighlight lang="php">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();</syntaxhighlight>
<span id="structural-pattern-adapter"></span>
== Structural Pattern (Adapter) ==
Allows incompatible interfaces to work together.
<syntaxhighlight lang="php">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();</syntaxhighlight>
<span id="behavioral-pattern-observer"></span>
== Behavioral Pattern (Observer) ==
Allows an object to publish changes to its state so other objects can react accordingly.
<syntaxhighlight lang="php">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!");</syntaxhighlight>
  <!-- From bodyContent 1543099.mraw -->
  <!-- From bodyContent 1543099.mraw -->


[[Category:IT backend patterns]]

Latest revision as of 21:54, 20 October 2025

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!");