PHP design PATTERN: Observer mode

Source: Internet
Author: User

First look at a chestnut:

Event class eventdemo{//triggers a new event public function trigger () {echo "event happening\r";        echo "Follow-up Business update logic 1\r\n";        echo "Follow-up Business update logic 2\r\n";    echo "Follow-up Business update logic 3\r\n"; }}


Scenario: Triggering other business-related actions when an event changes

Feature 1. All business code is intrusive and is placed directly in the core business.

2. The coupling degree is too high, it is not easy to manage maintenance. Need one thing to deal with.



So we can assume that:

If the follow-up business is all followers of the event, when the event changes, only the observer can be notified. Other work is done by the observer itself.

Similar to publish-subscribe to this pattern.



Code implementation:

Step 1: Define the Observer interface

Step 2: Define the Observer class

Step 3: Define the event Generator (abstract class)

Step 4. Defining the event scenario class (inheriting and implementing the event generator abstract class)

Step 5: Instantiation of the application
Instantiate event scene--add observer---Notify




Defining the Observer interface

/** * Defines the Observer interface (The Observer object is the event person) *interface Observer */interface observer{/** * event occurs, the update operation * @param null $eventInfo Information about the event that occurred * @return mixed */Public Function update ($eventInfo = null);}

Defining the Observer class

Define multiple observers class Observer1 implements observer{Public Function update ($eventInfo = null) {ECHO ' Observer 1: Ready to start {$ev    entinfo}<br> \ r \ n "; }}class Observer2 implements observer{Public Function update ($eventInfo = null) {ECHO ' Observer 2: Ready to start {$eventInfo    }<br> \ r \ n "; }}


Defining event Generator (abstract class)

/** *  Event Creator (abstract class)  * Class EventGenerator */abstract class  eventgenerator{    private  $observers all observers for the  = [];//event      /**     *  increased observers      *  @return  mixed      */    public function addobserver (Observer $ observer)     {         $this->observers[] =   $observer;    }    /**     *  Notification event has occurred and other observers have been notified to update business logic      *  @return  mixed     * /    public function notify ($eventInfo)     {         foreach  ($this->observers as  $observer)  {              $observer->update ($eventInfo);        }     }}


Defining an Event scenario class (inheriting and implementing an event generator abstract class)

/** * Define event class (inheriting event Creator Class) * Class Event */class event extends eventgenerator{public function trigger ($eventInfo) {        echo "event: {$eventInfo} is about to occur, notify the observers to prepare for the update <br>\r\n";        $this->notify ($eventInfo);    echo "<br/>"; }}


Implementation of the main business scenario

$event = new Event (),//The Observer $event->addobserver (New Observer1 ()) can be added and deleted dynamically here,//$event->addobserver (New Observer2 ());//There can be any number of events $event->trigger (' eating '), $event->trigger (' drinking '), $event->trigger (' walking ');


At this point, we can see that in the main business scenario, simply managing (adding, deleting) observers, managing (adding, deleting) events can simplify the code logic and reduce coupling to complete the task. A better advantage is that the business code is decoupled and facilitates later maintenance expansion.

PHP design PATTERN: Observer mode

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.