Home  >  Article  >  Backend Development  >  What you don’t know about php observer pattern

What you don’t know about php observer pattern

一个新手
一个新手Original
2017-09-12 10:16:111329browse

Let’s start with the name. The word “observer mode” contains a lot of information. Children who have played many online games should know that even in Doudizhu, in addition to the players, there is also a character called "Observer". In the pattern design we are talking about today, the same is true for observers. First, there must be a "theme". Only with a theme can observers gather together with small stools. Secondly, the observer must also have his own operations. Otherwise there is no point in gathering together and doing nothing.

From a process-oriented perspective, first the observer registers with the subject . After registration, the subject notifies the observer to make the corresponding Operation and the whole thing is over.

From an object-oriented perspective, topics provide interfaces for registration and notification, and observers provide interfaces for their own operations. (These observers have the same interface.) Observers use the subject's interface to register with the topic, and the subject uses the observer interface to notify observers. The degree of coupling is quite low.

How to implement observer registration? The previous registrant pattern can easily provide us with ideas, just add these objects to a registration tree. How to notify? This is simpler, traverse the registration tree and let each object implement the operations provided by its interface.  

<?php
// 主题接口
interface Subject{
    public function register(Observer $observer);
    public function notify();
}
// 观察者接口
interface Observer{
    public function watch();
}
// 主题
class Action implements Subject{
     public $_observers=array();
     public function register(Observer $observer){
         $this->_observers[]=$observer;
     }

     public function notify(){
         foreach ($this->_observers as $observer) {
             $observer->watch();
         }

     }
 }

// 观察者
class Cat implements Observer{
     public function watch(){
         echo "Cat watches TV<hr/>";
     }
 } 
 class Dog implements Observer{
     public function watch(){
         echo "Dog watches TV<hr/>";
     }
 } 
 class People implements Observer{
     public function watch(){
         echo "People watches TV<hr/>";
     }
 }

// 应用实例
$action=new Action();
$action->register(new Cat());
$action->register(new People());
$action->register(new Dog());
$action->notify();

  The so-called pattern is more of an idea, and there is no need to stick to code details. The observer pattern is more about two independent classes using interfaces to complete something that should be very complicated. If we don't use theme classes, we still need to continuously create instances and perform operations in a loop. Now you only need to create an instance, and you only need to call the notification method once to perform the operation.

Since the beginning of the singleton model, I have considered how to implement the code step by step. Now, most of the implementation code has been explained in one sentence. It is actually based on the continuous accumulation of the previous ones. I really feel that through continuous learning of design patterns, I can greatly deepen my thinking about object-oriented programming. Of course, talking on paper is still not necessary, it’s better to put in more practice~~·

The above is the detailed content of What you don’t know about php observer pattern. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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