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 registerswith the subject. After registration, the subjectnotifies theobserver to make the correspondingOperationand 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.
_observers[]=$observer; } public function notify(){ foreach ($this->_observers as $observer) { $observer->watch(); } } } // 观察者 class Cat implements Observer{ public function watch(){ echo "Cat watches TV
"; } } class Dog implements Observer{ public function watch(){ echo "Dog watches TV
"; } } class People implements Observer{ public function watch(){ echo "People watches TV
"; } } // 应用实例 $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!