Home > Article > Backend Development > PHP design patterns: relationship to behavioral programming
PHP design patterns implement behavioral programming principles to create repeatable and loosely coupled code through well-defined behaviors. Specific patterns include: Observer pattern: defines a subscription-publishing relationship to facilitate objects to monitor and respond to events. Strategy mode: allows switching between different algorithms and performing different operations as needed. Command mode: Encapsulate requests into objects and execute them in a parameterized manner.
Behavioral programming creates repeatable and loosely coupled code by defining well-defined behaviors. PHP provides many design patterns for behavioral programming, let’s explore them.
The Observer pattern defines a subscription-publish relationship in which one object (topic) can issue notifications and other objects (observers) can listen to them.
interface Observer { public function update($subject); } class ConcreteObserver1 implements Observer { public function update($subject) { echo "ConcreteObserver1 received update from $subject\n"; } } class ConcreteObserver2 implements Observer { public function update($subject) { echo "ConcreteObserver2 received update from $subject\n"; } } class Subject { private $observers = []; public function attach(Observer $observer) { $this->observers[] = $observer; } public function detach(Observer $observer) { $key = array_search($observer, $this->observers); if ($key !== false) { unset($this->observers[$key]); } } public function notify() { foreach ($this->observers as $observer) { $observer->update($this); } } } // 实战案例 $subject = new Subject(); $observer1 = new ConcreteObserver1(); $observer2 = new ConcreteObserver2(); $subject->attach($observer1); $subject->attach($observer2); $subject->notify(); // 输出:"ConcreteObserver1 received update from Subject" 和 "ConcreteObserver2 received update from Subject"
Strategy mode allows you to switch between different algorithms as needed.
interface Strategy { public function doOperation($a, $b); } class ConcreteStrategyA implements Strategy { public function doOperation($a, $b) { return $a + $b; } } class ConcreteStrategyB implements Strategy { public function doOperation($a, $b) { return $a - $b; } } class Context { private $strategy; public function __construct(Strategy $strategy) { $this->strategy = $strategy; } public function doOperation($a, $b) { return $this->strategy->doOperation($a, $b); } } // 实战案例 $context = new Context(new ConcreteStrategyA()); echo $context->doOperation(10, 5); // 输出:15 $context = new Context(new ConcreteStrategyB()); echo $context->doOperation(10, 5); // 输出:5
Command mode encapsulates requests into objects, allowing you to execute them in a parameterized manner.
interface Command { public function execute(); } class ConcreteCommand implements Command { private $receiver; public function __construct($receiver) { $this->receiver = $receiver; } public function execute() { $this->receiver->action(); } } class Receiver { public function action() { echo "Receiver action executed\n"; } } class Invoker { private $commands = []; public function setCommand(Command $command) { $this->commands[] = $command; } public function invoke() { foreach ($this->commands as $command) { $command->execute(); } } } // 实战案例 $receiver = new Receiver(); $command = new ConcreteCommand($receiver); $invoker = new Invoker(); $invoker->setCommand($command); $invoker->invoke(); // 输出:"Receiver action executed"
By using behavioral design patterns in PHP, you can create code that is reusable, maintainable, and flexible.
The above is the detailed content of PHP design patterns: relationship to behavioral programming. For more information, please follow other related articles on the PHP Chinese website!