Discussion on Observer Design Mode

Source: Internet
Author: User

I remember when I first learned joomla, I mentioned joomla on some teaching websites.CodeThe design pattern is "observer design pattern", which is the first time I have heard of the concept of "Design Pattern. then we went through the Wiki mode. maybe at that time, the idea of object-oriented programming only stuck in creating a class and then new it. so how to read the text is unknown. I always seem to understand, but I don't understand anything. now let's look at this mode. finally, I found the access point to understand this mode. next I will start with you to study this "Observer Design Pattern"
Before studying this mode, we should try to understand Object-Oriented Programming and fully understand the meaning of "object.

"Object" is an entity with its own attributes and methods. is an instance of class instantiation. we can think of a "class" as a concrete concept, and the object is the concrete instance of this "concept. when we start to generate an object for a new class, this object exists in the memory, no matter how many references we create for this object (this object is paid to one or some variables ), this object is always unique in the memory. for example:

 
Class A {}$ A = new A; // here an object is created and Its Reference is $ A $ B = $; // The object referenced by $ A has a second reference $ B $ A-> content = 'B'; unset ($ A); // at this time, released $ a memory, but the object it points to still exists in the memory, and it now has only one reference ($ B) echo $ B-> content; // output here: B

if you have enough knowledge about the meaning of the object, the following observer design pattern is relatively simple.
"observer design pattern", as its name implies, has two types of members: "Observer" and "Observer ", when the status of the observer changes, it can be received by all the observers (personal feeling, a bit like a trigger, a trigger point on the state of multiple methods. when a trigger is triggered, all methods bound to the trigger are run, but the trigger does not know how many trigger methods are involved ). the code structure is as follows:

// Observer class subject {// all observer var $ observer = array (); // sets the observer function setobserver ($ observer) {$ this-> observer [] = $ observer;} // notify the observer's status (or other) to the function notification () {If (! Empty ($ this-> observer) {foreach ($ this-> observer as $ observer) {$ observer-> updatefun ()} ($ this-> message) ;}}/// observer Class 1 observer1 {// var $ subject = ''by the observer; function observer1 ($ subject) {$ this-> subject = $ subject; $ this-> subject-> setobserver ($ this);} function updatemessage ($ message) {echo 'I am observer1 :'. $ this-> subject-> message;} // notify the observer of the notification interface function updatefun () {return 'updatemessage' when the notification is received ';}} // observer Class 2 class observer2 {// var $ subject = ''; function observer2 ($ subject) {$ this-> subject = $ subject; $ this-> subject-> setobserver ($ this);} function Update ($ message) {echo 'I am observer2 :'. $ this-> subject-> message;} // notify the observer of the notification interface function updatefun () {return 'update';} when the notification is sent ';}} // observer Class 2 class observer3 {// var $ subject = ''; function observer3 ($ subject) {$ this-> subject = $ subject; $ this-> subject-> setobserver ($ this);} function notification ($ message) {echo 'I am observer3 :'. $ this-> subject-> message;} // notify the observer of the notification interface function updatefun () {return 'notification';} when the notification is sent ';}} // call the code // The Observer $ subject = new subject; // when an observer is created, the observer $ observer1 = new observer1 ($ subject) is notified ); $ observer2 = new observer2 ($ subject); $ observer3 = new observer3 ($ subject); $ subject-> message = 'get all the observers up <HR/> '; $ subject-> notification (); // The observer does not know the number of observers. You only need to tell the observer who is the observer when creating the observer.

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.