The observer pattern for PHP design Patterns

Source: Internet
Author: User

Now there are two factions, some people recommend the use of design patterns, some people do not recommend the use of design mode!
This is like writing articles, some people like the article follow the routines, such as narrative nature of the article, time, place, character, events. And some people like to write essays or essays, some people like to write poetry!

Writing code now is a lot like writing articles, but in some places you need more skills than writing articles! Write articles written more generally can also write excellent articles, and code is the same, write more can also write a lot of some code!

Many times, when I look at design patterns, some design patterns just match my code habits. But if you try to set it up, it's counterproductive. --many times is learned the moves, in the application unconsciously use these moves, can grasp its way, but also do not rigidly adhere to the moves, is so-called "no recruit wins has the recruit"? I'm learning design patterns, and that's what I know about this stuff. I have such an impression in my mind that I will not give birth to it! If design patterns don't fit your habits, it's bad for you to read the code! 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();    publicfunctionregister($sub){ /*  注册观察者 */        $this->_observers[] = $sub;    }         publicfunctiontrigger(){  /*  外部统一访问    */        if(!empty($this->_observers)){            foreach($this->_observers as$observer){                $observer->update();            }        }    }}/** * 观察者要实现的接口 */interfaceObserverable{    publicfunctionupdate();}class Subscriber implementsObserverable{    publicfunctionupdate(){        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. A good design pattern does not go directly into your code, but into your brain. Reference: Head First design mode

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.