IT behavioral pattern: Difference between revisions
Jump to navigation
Jump to search
m Maintenance script moved page UserDocumentation:Knowledge Central/KC IT/IT backend team/IT backend policies/IT backend patterns/IT behavioral pattern to IT behavioral pattern: Cleaned title and categorized |
No edit summary |
||
| (One intermediate revision by one other user not shown) | |||
| Line 14: | Line 14: | ||
Define a family of algorithms, encapsulate each one, and make them interchangeable. | Define a family of algorithms, encapsulate each one, and make them interchangeable. | ||
<syntaxhighlight>interface SortingStrategy { | <syntaxhighlight lang="php">interface SortingStrategy { | ||
public function sort(array $data): array; | public function sort(array $data): array; | ||
} | } | ||
| Line 36: | Line 36: | ||
public function __construct(SortingStrategy $strategy) { | public function __construct(SortingStrategy $strategy) { | ||
$this- | $this->strategy = $strategy; | ||
} | } | ||
public function setStrategy(SortingStrategy $strategy) { | public function setStrategy(SortingStrategy $strategy) { | ||
$this- | $this->strategy = $strategy; | ||
} | } | ||
public function sort(array $data): array { | public function sort(array $data): array { | ||
return $this- | return $this->strategy->sort($data); | ||
} | } | ||
} | } | ||
$sorter = new Sorter(new BubbleSort()); | $sorter = new Sorter(new BubbleSort()); | ||
$sortedData = $sorter- | $sortedData = $sorter->sort([4, 2, 8, 5]);</syntaxhighlight> | ||
<span id="observer-pattern"></span> | <span id="observer-pattern"></span> | ||
== Observer Pattern == | == Observer Pattern == | ||
| Line 55: | Line 55: | ||
One-to-many dependency between objects so when one object changes state, all its dependents are notified. | One-to-many dependency between objects so when one object changes state, all its dependents are notified. | ||
<syntaxhighlight>interface Observer { | <syntaxhighlight lang="php">interface Observer { | ||
public function update($message); | public function update($message); | ||
} | } | ||
| Line 69: | Line 69: | ||
public function attach(Observer $observer) { | public function attach(Observer $observer) { | ||
$this- | $this->observers[] = $observer; | ||
} | } | ||
public function detach(Observer $observer) { | public function detach(Observer $observer) { | ||
$key = array_search($observer, $this- | $key = array_search($observer, $this->observers, true); | ||
if ($key) { | if ($key) { | ||
unset($this- | unset($this->observers[$key]); | ||
} | } | ||
} | } | ||
public function notify($message) { | public function notify($message) { | ||
foreach ($this- | foreach ($this->observers as $observer) { | ||
$observer- | $observer->update($message); | ||
} | } | ||
} | } | ||
| Line 88: | Line 88: | ||
class ConcreteObserver implements Observer { | class ConcreteObserver implements Observer { | ||
public function update($message) { | public function update($message) { | ||
echo | echo "Observer received: $message\n"; | ||
} | } | ||
} | } | ||
| Line 94: | Line 94: | ||
$subject = new ConcreteSubject(); | $subject = new ConcreteSubject(); | ||
$observer = new ConcreteObserver(); | $observer = new ConcreteObserver(); | ||
$subject- | $subject->attach($observer); | ||
$subject- | $subject->notify('Hello, Observers!');</syntaxhighlight> | ||
<span id="command-pattern"></span> | <span id="command-pattern"></span> | ||
== Command Pattern == | == Command Pattern == | ||
| Line 101: | Line 101: | ||
Encapsulate a request as an object, allowing for parameterization and other operations. | Encapsulate a request as an object, allowing for parameterization and other operations. | ||
<syntaxhighlight>interface Command { | <syntaxhighlight lang="php">interface Command { | ||
public function execute(); | public function execute(); | ||
} | } | ||
| Line 107: | Line 107: | ||
class Light { | class Light { | ||
public function turnOn() { | public function turnOn() { | ||
echo | echo "Light is ON\n"; | ||
} | } | ||
public function turnOff() { | public function turnOff() { | ||
echo | echo "Light is OFF\n"; | ||
} | } | ||
} | } | ||
| Line 119: | Line 119: | ||
public function __construct(Light $light) { | public function __construct(Light $light) { | ||
$this- | $this->light = $light; | ||
} | } | ||
public function execute() { | public function execute() { | ||
$this- | $this->light->turnOn(); | ||
} | } | ||
} | } | ||
| Line 131: | Line 131: | ||
public function __construct(Light $light) { | public function __construct(Light $light) { | ||
$this- | $this->light = $light; | ||
} | } | ||
public function execute() { | public function execute() { | ||
$this- | $this->light->turnOff(); | ||
} | } | ||
} | } | ||
| Line 141: | Line 141: | ||
class RemoteControl { | class RemoteControl { | ||
public function submit(Command $command) { | public function submit(Command $command) { | ||
$command- | $command->execute(); | ||
} | } | ||
} | } | ||
| Line 149: | Line 149: | ||
$turnOff = new TurnOffLightCommand($light); | $turnOff = new TurnOffLightCommand($light); | ||
$remote = new RemoteControl(); | $remote = new RemoteControl(); | ||
$remote- | $remote->submit($turnOn); // Output: Light is ON | ||
$remote- | $remote->submit($turnOff); // Output: Light is OFF</syntaxhighlight> | ||
[[Category:IT backend patterns]] | |||
Latest revision as of 21:59, 20 October 2025
Behavioral Patterns are design patterns that are concerned with communication between objects. They deal with how objects interact and how responsibilities are assigned. Essentially, they help objects cooperate.
When to Use
Behavioral patterns are used when there's a need to define clear channels of communication between objects, streamline how responsibilities are allocated, or simplify complex control flows.
Fundamental behavioral patterns
Strategy Pattern
Define a family of algorithms, encapsulate each one, and make them interchangeable.
interface SortingStrategy {
public function sort(array $data): array;
}
class BubbleSort implements SortingStrategy {
public function sort(array $data): array {
// Bubble sort logic...
return $data;
}
}
class QuickSort implements SortingStrategy {
public function sort(array $data): array {
// Quick sort logic...
return $data;
}
}
class Sorter {
private $strategy;
public function __construct(SortingStrategy $strategy) {
$this->strategy = $strategy;
}
public function setStrategy(SortingStrategy $strategy) {
$this->strategy = $strategy;
}
public function sort(array $data): array {
return $this->strategy->sort($data);
}
}
$sorter = new Sorter(new BubbleSort());
$sortedData = $sorter->sort([4, 2, 8, 5]);
Observer Pattern
One-to-many dependency between objects so when one object changes state, all its dependents are notified.
interface Observer {
public function update($message);
}
interface Subject {
public function attach(Observer $observer);
public function detach(Observer $observer);
public function notify($message);
}
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) {
unset($this->observers[$key]);
}
}
public function notify($message) {
foreach ($this->observers as $observer) {
$observer->update($message);
}
}
}
class ConcreteObserver implements Observer {
public function update($message) {
echo "Observer received: $message\n";
}
}
$subject = new ConcreteSubject();
$observer = new ConcreteObserver();
$subject->attach($observer);
$subject->notify('Hello, Observers!');
Command Pattern
Encapsulate a request as an object, allowing for parameterization and other operations.
interface Command {
public function execute();
}
class Light {
public function turnOn() {
echo "Light is ON\n";
}
public function turnOff() {
echo "Light is OFF\n";
}
}
class TurnOnLightCommand implements Command {
private $light;
public function __construct(Light $light) {
$this->light = $light;
}
public function execute() {
$this->light->turnOn();
}
}
class TurnOffLightCommand implements Command {
private $light;
public function __construct(Light $light) {
$this->light = $light;
}
public function execute() {
$this->light->turnOff();
}
}
class RemoteControl {
public function submit(Command $command) {
$command->execute();
}
}
$light = new Light();
$turnOn = new TurnOnLightCommand($light);
$turnOff = new TurnOffLightCommand($light);
$remote = new RemoteControl();
$remote->submit($turnOn); // Output: Light is ON
$remote->submit($turnOff); // Output: Light is OFF