Introduction
The observer pattern is a behavioral pattern that defines a one-to-many relationship between objects. When the state of an object changes, the object that depends on it will be notified.
Applicable scenarios
Similar to triggering hook events, you can do message notifications and bottom-level monitoring of the framework.
Changes to an object will cause changes to one or more objects, which is convenient for writing extensions.
advantage
Convenient expansion, reduce coupling, and unify trigger rules. When you need to add or delete an observer, you only need to add an observer.
shortcoming
Compared to relying directly on certain classes without an observer, it increases the complexity of the code.
If the observer is dependent on each other by the observer, there is a possibility of a vicious cycle.
Replenish
It is necessary to clarify who the observer and the observed person are, and the observer can be understood as the object being passively notified. The observer is the object that actively sends notifications.
In a fixed routine, the observer needs at least one method to add an observer and a method to notify the observer to determine the identity and send notifications (generally there are three, one more method to delete the observer), and the observer needs at least one updated method to receive notifications from the observer.
Code (custom implementation)
//Suppose that the user needs to send email and SMS notification after successfully purchasing the productclass Order { private $observers = []; //Add an observer public function attach($type, $observer) { $this->observers[$type] = $observer; } //Notify each observer public function notify() { if ($this->observers == []) { return null; } foreach ($this->observers as $every_observer) { (new $every_observer)->update($this); } } //Purchase the product and trigger the notification public function buyGoods() { //todo order operation echo 'Commodity Purchase Completed' . PHP_EOL; $this->notify(); } } class Mail { public function update($observer) { echo 'Send an email' . PHP_EOL; } } class Sms { public function update($observer) { echo 'sending a text message' . PHP_EOL; } } $order = new Order(); //Add an observer$order->attach('mail', Mail::class); $order->attach('sms', Sms::class); $order->buyGoods();
Code (based on SPL implementation)
SPL (Standard PHP Library) Standard PHP class library, a set of interfaces and classes used to solve typical problems.
class OrderListener implements \SplSubject { //Observer list public $observers; public function __construct() { //SplObjectStorage class provides mapping from objects to data, or provides mapping of object sets by ignoring data. This dual purpose is very useful in many cases where uniquely identifying objects are required. $this->observers = new \SplObjectStorage(); } //Add the object to be notified public function attach(\SplObserver $observer) { $this->observers->attach($observer); } //Remove the object to be notified public function detach(\SplObserver $observer) { $this->observers->detach($observer); } //notify public function notify() { //Return the iterator (which can be understood as a pointer here) to the first storage element. $this->observers->rewind(); //Judge whether the pointer is valid while($this->observers->valid()) { //Get the current observer $curr_obj = $this->observers->current(); //Notify the current observer $curr_obj->update($this); //Move the pointer down $this->observers->next(); } } //Trigger notification public function buyGoods() { echo 'Purchase successful' . PHP_EOL; $this->notify(); } } //The SplObserver interface is used together with the SplSubject interface to implement the observer design pattern.class Mail implements \SplObserver { //Response processing of the observed object public function update(\SplSubject $subject) { echo 'Send an email' . PHP_EOL; } } class Sms implements \SplObserver { //Response processing of the observed object public function update(\SplSubject $subject) { echo 'sending a text message' . PHP_EOL; } } $listener = new OrderListener(); //Add an observer$listener->attach(new Mail()); $listener->attach(new Sms()); $listener->buyGoods();
Notification code (notify method optimization based on SPL implementation)
//The notify method of the above code is implemented by manually adjusting the pointer in native way. You can also use foreach to traverse the implementation public function notify() { foreach ($this->observers as $observer) { $observer->update($this); } }
This is the end of this article about the detailed explanation of the observer mode in the PHP design mode. For more related content on PHP observer mode, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!