PHP Observer Pattern)

Source: Internet
Author: User
Tags spl

For process-oriented programming (structured programming), we emphasize the block structure and process processing of programs. After entering the field of Object-Oriented Programming (OOP), design patterns have become an important technology. The design pattern processes object generation, collaboration, dependency, coupling, and other relationships. For example, the singleton mode ensures that a class can only be instantiated once, and the factory mode can generate instances (objects) of the corresponding class based on different conditions ). The observer mode is generally used to implement "Event" processing and construct a software event processing system.

About the observer mode (observer) implementation method, I discussed in a blog article (http://blog.why100000.com /? P = 744 ). But in fact, Spl (Standard PHP Library) provides convenient support for the observer mode after php5.0, especially after php5.2, the SPL function has been greatly expanded.

The observer mode involves two (two or more) classes, one as the observed object (subject) and the other as the "Observer" (observer ). The observer class monitors the status of one or more observed objects. When its status changes, the observer will be notified.

To support the observer mode, SPL provides splsubject and splobserver interfaces.

The splsubject interface provides three methods: attach (), detach (), and notify. The splobserver interface provides the update () method.

The splsubject derived class maintains a state. When the state changes, such as the attribute changes, the notify () method is called. In this case, attach () the Update () method of all splobserver instances registered in the method will be called.

Class subjectdemo implements splsubject
{
Private $ observers, $ value;

Public Function _ construct ()
{
// $ Observers is declared as an array, indicating that the observer can have multiple
$ This-> Observers = array ();
}

Public Function attach (splobserver $ observer)
{
$ This-> observers [] = $ observer;
}

Public Function detach (splobserver $ observer)
{
If ($ idx = array_search ($ observer, $ this-> observers, true ))
Unset ($ this-> observers [$ idx]);
}

Public Function notify ()
{
Foreach ($ this-> observers as $ observer)
$ Observer-> Update ($ this );
}

Public Function setvalue ($ value)
{
$ This-> value = $ value;
$ This-> Policy ();
}

Public Function getvalue ()
{
Return $ this-> value;
}

Public Function getobserversnumber ()
{
Return count ($ this-> observers );
}
}

Class observerdemo1 implements splobserver
{
Public Function Update (splsubject $ subject)
{
Echo 'the new value is '. $ subject-> getvalue ().' <br> ';
}
}

Class observerdemo2 implements splobserver
{
Public Function Update (splsubject $ subject)
{
Echo 'the number of observer (s) is (are) '. $ subject-> getobserversnumber ().' <br> ';
}
}

$ Subject = new subjectdemo ();

$ Observer1 = new observerdemo1 ();
$ Observer2 = new observerdemo2 ();

$ Subject-> attach ($ observer1 );
$ Subject-> attach ($ observer2 );

$ Subject-> setvalue (100 );

The code above shows that the observer can have multiple classes.
When the setvalue () method of the $ subject object is called (triggered), it is passed as a parameter to the update () method of $ observer1 and $ observer2 objects. The Update () function of all observers is similar to the event handler function.
To expand event processing capabilities, you only need to derive a subclass from the splobserver interface, write the update () function code, and attach it to the $ subject object.

Specific to the above Code, there is a defect, that is, different events are not implemented, and their corresponding classes are used for processing. This is not a problem of the observer mode. It must be solved in conjunction with other models.

Author: Zhang Qing (mesh) Xi'an PHP Education Training Center
From mesh horizon: http://blog.why100000.com
Author Weibo: http://t.qq.com/zhangking
Why 100,000 computer learning networks: http://www.why100000.com

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.