The observer pattern for PHP design Patterns

Source: Internet
Author: User

The Observer pattern defines a one-to-many dependency on an object, so that when an object changes state, all its dependents are notified and updated automatically! The principle of design in the observer pattern is to change the state of the subject and the number of observers. With this pattern, you can change objects that depend on the subject state, without changing the theme.  --Find out what is going to change in the program, and then separate it from the fixed aspect! Both themes and observers use interfaces: The Observer uses the interface of the topic to register the subject, and the subject uses the Observer interface to notify the Observer. This allows the two to function normally, but also has the advantage of loose coupling! --Programming for the interface, not for the implementation of programming!. The Observer pattern uses "combination" to combine many observers into the subject. This relationship between objects (the Observer-subject) is not generated by inheritance, but by the way the composition is used at run time. --Multi-use combination, less use inheritance! Code
<?php/** * 观察者模式 * @author: Mac * @date: 2012/02/22 */classPaper{ /* 主题    */    private$_observers= array();    publicfunction register($sub){ /*  注册观察者 */        $this->_observers[] = $sub;    }        publicfunction trigger(){  /*  外部统一访问    */        if(!empty($this->_observers)){            foreach($this->_observers as$observer){                $observer->update();            }        }    }}/** * 观察者要实现的接口 */interfaceObserverable{    publicfunctionupdate();}classSubscriber implementsObserverable{    publicfunction update(){        echo"Callback\n";    }}

Here is the test code

/*  测试    */$paper= new Paper();$paper->register(newSubscriber());//$paper->register(new Subscriber1());//$paper->register(new Subscriber2());$paper->trigger();

Summary when a new object is to be filled in, you only need to register it in the subject (also called an observer) (there are many ways to register it, or you can register it at the time of construction or the interface that the framework accesses), and then the implementation code is directly in the interface of the new object. This reduces the coupling of the subject object and the Observer object.

The observer pattern for PHP design Patterns

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.