In the previous article "Understanding the Adapter Pattern in PHP" we introduced the adapter pattern in PHP. This article will take you to understand the observer pattern in PHP.
#Observer, it seems that this character appears in many science fiction works. For example, one of my favorite American TV series, "Fringe", in this series, observers constantly travel through time and space to record various people or things. However, the observer in the design pattern does not just stand on the sidelines and watch. The observer here takes corresponding actions according to the state changes of the subject.
GoF definition: Define a one-to-many dependency relationship between objects. When the state of an object When a change occurs, all objects that depend on it are notified and automatically updated
GoF Class Diagram:
Code implementation
interface Observer { public function update(Subject $subject): void; }
There is nothing much to say about the abstract interface of the observer, it just allows you to implement a specific Update
class ConcreteObserver implements Observer { private $observerState = ''; function update(Subject $subject): void { $this->observerState = $subject->getState(); echo '执行观察者操作!当前状态:' . $this->observerState; } }
Specific observers implement the update() method. Here we get the Subject class, so that we can get the status of it.
class Subject { private $observers = []; private $stateNow = ''; public function attach(Observer $observer): void { array_push($this->observers, $observer); } public function detach(Observer $observer): void { $position = 0; foreach ($this->observers as $ob) { if ($ob == $observer) { array_splice($this->observers, ($position), 1); } ++$position; } } public function notify(): void { foreach ($this->observers as $ob) { $ob->update($this); } } }
Subject parent class maintains an observer array, and then adds, deletes and loops through this The purpose of the array method is to conveniently manage all observers. The implementation class of
class ConcreteSubject extends Subject{ public function setState($state) { $this->stateNow = $state; $this->notify(); } public function getState() { return $this->stateNow; } }
Subject only updates the status. When the status changes, the observer traversal method is called to update all observations. ()Operation
Full code: https://github.com/zhangyue0503/designpatterns-php/blob/master/06.observer/source/observer.php
Example
model. This mode can be said to be the upgrade mode of observers. This series of articles will not go into details, but you can take a look at thePublish and SubscribeandEvent Monitoringaspects in Laravel.
Order sold class diagramFull source code: https://github.com/zhangyue0503/designpatterns- php/blob/master/06.observer/source/order-observer.php
Descriptioninterface Observer { public function update($obj); } class Message implements Observer { //.... function update($obj) { echo '发送新订单短信(' . $obj->mobile . ')通知给商家!'; } //.... } class Goods implements Observer { //.... public function update($obj) { echo '修改商品' . $obj->goodsId . '的库存!'; } //.... } class Order { private $observers = []; public function attach($ob) { $this->observers[] = $ob; } public function detach($ob) { $position = 0; foreach ($this->observers as $ob) { if ($ob == $observer) { array_splice($this->observers, ($position), 1); } ++$position; } } public function notify($obj) { foreach ($this->observers as $ob) { $ob->update($obj); } } public function sale() { // 商品卖掉了 // .... $obj = new stdClass(); $obj->mobile = '13888888888'; $obj->goodsId = 'Order11111111'; $this->notify($obj); } } $message = new Message(); $goods = new Goods(); $order = new Order(); $order->attach($message); $order->attach($goods); // 订单卖出了!! $order->sale();Copy after login
- We do not completely comply with the GoF class diagram. Although GoF is the Bible, it does not necessarily mean that we must comply with it completely. We can tailor it appropriately for specific business situations and use
- Orders After the status changes through the sale() method, directly call the notify method to call the observer.
- Sending text messages and sending push notifications can be separated and implemented by observers one by one. These observers do not necessarily only have this A method, but it only needs to implement a common interface
- Product inventory and message sending are actually two completely unrelated classes, but they only need to implement the same interface
- PHP's SPL extension has prepared a set of observer interfaces for us. You can try it. Using the natively supported observer mode can save a lot of trouble!
SPL extension implements observer pattern - complete source code: https://github.com/zhangyue0503/designpatterns-php/blob/master/06.observer/source/spl_observer.php
This article is reproduced from: https://juejin.cn/post/6844903930262978574
Author: Hardcore Project Manager
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of An in-depth analysis of the observer pattern in PHP. For more information, please follow other related articles on the PHP Chinese website!