IT behavioral pattern

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

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