An in-depth analysis of the observer pattern in PHP

青灯夜游
Release: 2023-04-10 10:00:01
forward
3156 people have browsed it

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.

An in-depth analysis of 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 class diagram and explanation

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:

An in-depth analysis of the observer pattern in PHP

Code implementation

interface Observer { public function update(Subject $subject): void; }
Copy after login

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; } }
Copy after login

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); } } }
Copy after login

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; } }
Copy after login

Subject only updates the status. When the status changes, the observer traversal method is called to update all observations. ()Operation

  • Observer actually makes an update on itself, and Subject can execute observers in batches. Please note that we do not need to modify any code in the target class. , you only need to add it from the outside, so the target and observer are decoupled and do not need to care about each other's situation.
  • The observer can record the status of the target, or it does not need to be recorded. For example, we send After completing the database update or insertion operation of the SMS, we can only modify the status of the SMS data after the SMS interface is successfully sent. It is not necessarily necessary to transmit the target sending status to the observer.
  • When a class is in When a change occurs, you don’t know how many other classes may be affected. Observers are very useful at this time. There is still coupling in the observer pattern, that is, there is a list of observer objects in the target class. If you observe If the user does not implement the update() method, then there will be problems.
Then let’s talk about our mobile phone factory. Well, this time, we were targeted by a bunch of copycat phones (observation ), whatever function I provide (status update), they will provide the same function (update), and they also do more things based on me, which is euphemistically called: micro-innovation! You say it's irritating or not. Well, I also sent a group of market researchers (observers) to help me observe what functions other people’s mobile phones have (status updates), and then we will copy them and make some micro-innovations, so that we can all make progress together. ! !

Full code: https://github.com/zhangyue0503/designpatterns-php/blob/master/06.observer/source/observer.php

Example

This time we start with the order, but there is still the matter of sending text messages. When someone places an order on a general e-commerce platform, there are many things that need to be done, such as modifying inventory, sending text messages or push notifications to tell the merchant that someone has placed an order, and telling the buyer that the order was placed successfully and the payment was successful. In short, the occurrence of one thing will lead to various events. In fact, this leads to another very famous model

Subscribe and publish

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 diagram

An in-depth analysis of the observer pattern in PHP

Full source code: https://github.com/zhangyue0503/designpatterns- php/blob/master/06.observer/source/order-observer.php

interface 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
Description

  • 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!

source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!