Php pattern design-Observer pattern

Source: Internet
Author: User
The Observer pattern of php pattern design is the fifth article I wrote "php pattern design. The previous four articles continue to learn and deepen their understanding. today, I think it is very easy to understand the observer model. This may be the result of our accumulation. Hope to continue improving.

Starting with the name, the word "Observer Mode" contains a large amount of information. Kids shoes who have played many online games should know that even landlords, in addition to players, there is also a role called "observer ". The same is true for the Observer in the pattern design we are talking about today. First, there must be a "topic ". Only with one subject can the observer gather together on the bench. Second, the observer must have his own operations. Otherwise, it makes no sense for you to get together and do nothing.

From the process-oriented perspective, the first is the observer-to-topicRegisterAfter registrationNotificationThe Observer makes the correspondingOperationThe entire process is complete.

From the object-oriented perspective, the topic provides interfaces for registration and notification, and the Observer provides interfaces for its own operations. (These observers have the same interface .) The Observer registers the topic with the topic interface, and the topic uses the observer interface to notify the observer. The coupling degree is quite low.

How to implement observer registration? Through the previous registrant model, it is easy to provide us with ideas. just add these objects to a registration tree. How to notify? This makes it easier to traverse the registration tree so that each object can perform the operations provided by its interface.

   

 _ Observers [] = $ observer;} public function Policy () {foreach ($ this-> _ observers as $ observer) {$ observer-> watch ();}}} // Observer 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
 ";}}// Application instance $ action = new Action (); $ action-> register (new Cat (); $ action-> register (new People ()); $ action-> register (new Dog (); $ action-> Policy ();

The so-called mode is more of an idea, and there is no need to stick to the code details. The observer mode shows that two independent classes use interfaces to accomplish a complex task. If you do not use theme classes, you need to create instances repeatedly and perform operations. Now, you only need to create an instance. to execute an operation, you only need to call the method of one notification.

From the beginning of the Singleton mode, I considered how to implement the code step by step. now, most of the implementation code is based on the previous accumulation. I really feel that through continuous learning of the design model, I can greatly deepen my thoughts on object-oriented programming. Of course, it is still not necessary to talk on paper. it is best to invest in more exercises ~~ ·

Series of articles:

Php pattern design-Singleton pattern

    Factory model of php pattern design

   Php pattern design-registration tree pattern

    Php pattern design-Adapter pattern 

Php pattern design-Observer pattern

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.