IT Design patterns: Difference between revisions
Jump to navigation
Jump to search
Added category IT backend patterns |
No edit summary |
||
| Line 25: | Line 25: | ||
Ensures that a class has only one instance and provides a global point to access it. | Ensures that a class has only one instance and provides a global point to access it. | ||
<syntaxhighlight>class Singleton { | <syntaxhighlight lang="php">class Singleton { | ||
private static $instance; | private static $instance; | ||
| Line 49: | Line 49: | ||
Allows incompatible interfaces to work together. | Allows incompatible interfaces to work together. | ||
<syntaxhighlight>interface OldInterface { | <syntaxhighlight lang="php">interface OldInterface { | ||
public function oldRequest(); | public function oldRequest(); | ||
} | } | ||
| Line 55: | Line 55: | ||
class OldClass implements OldInterface { | class OldClass implements OldInterface { | ||
public function oldRequest() { | public function oldRequest() { | ||
return | return "Old Request"; | ||
} | } | ||
} | } | ||
| Line 67: | Line 67: | ||
public function __construct(OldClass $oldClass) { | public function __construct(OldClass $oldClass) { | ||
$this- | $this->oldClass = $oldClass; | ||
} | } | ||
public function request() { | public function request() { | ||
return $this- | return $this->oldClass->oldRequest(); | ||
} | } | ||
} | } | ||
| Line 77: | Line 77: | ||
$old = new OldClass(); | $old = new OldClass(); | ||
$adapter = new Adapter($old); | $adapter = new Adapter($old); | ||
echo $adapter- | echo $adapter->request();</syntaxhighlight> | ||
<span id="behavioral-pattern-observer"></span> | <span id="behavioral-pattern-observer"></span> | ||
== Behavioral Pattern (Observer) == | == Behavioral Pattern (Observer) == | ||
| Line 83: | Line 83: | ||
Allows an object to publish changes to its state so other objects can react accordingly. | Allows an object to publish changes to its state so other objects can react accordingly. | ||
<syntaxhighlight>interface Observer { | <syntaxhighlight lang="php">interface Observer { | ||
public function update($message); | public function update($message); | ||
} | } | ||
| Line 98: | Line 98: | ||
public function attach(Observer $observer) { | public function attach(Observer $observer) { | ||
$this- | $this->observers[] = $observer; | ||
} | } | ||
public function detach(Observer $observer) { | public function detach(Observer $observer) { | ||
foreach ($this- | foreach ($this->observers as $key => $obs) { | ||
if ($obs === $observer) { | if ($obs === $observer) { | ||
unset($this- | unset($this->observers[$key]); | ||
} | } | ||
} | } | ||
| Line 110: | Line 110: | ||
public function notify() { | public function notify() { | ||
foreach ($this- | foreach ($this->observers as $observer) { | ||
$observer- | $observer->update($this->message); | ||
} | } | ||
} | } | ||
public function setMessage($message) { | public function setMessage($message) { | ||
$this- | $this->message = $message; | ||
$this- | $this->notify(); | ||
} | } | ||
} | } | ||
| Line 123: | Line 123: | ||
class ConcreteObserver implements Observer { | class ConcreteObserver implements Observer { | ||
public function update($message) { | public function update($message) { | ||
echo | echo "Observer received message: $message\n"; | ||
} | } | ||
} | } | ||
| Line 131: | Line 131: | ||
$observer2 = new ConcreteObserver(); | $observer2 = new ConcreteObserver(); | ||
$subject- | $subject->attach($observer1); | ||
$subject- | $subject->attach($observer2); | ||
$subject- | $subject->setMessage("Hello, Observers!");</syntaxhighlight> | ||
<!-- From bodyContent 1543099.mraw --> | <!-- From bodyContent 1543099.mraw --> | ||
[[Category:IT backend patterns]] | [[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!");