Learn PHP Design Patterns PHP Implementation Observer mode (OBSERVER) _php tips

Source: Internet
Author: User
Tags abstract learn php

I. Intention
Defines a one-to-many dependency between objects, and when the state of an object changes, all objects that depend on it are notified and automatically updated "GOF95"
Also known as publish-subscribe (publish-subscribe) mode, model-view (model-view) mode, source-monitor (Source-listener) mode, or dependent (dependents) mode
Second, the Observer Pattern structure chart

iii. main roles in the Observer model
Abstract Theme (Subject) role:The theme role holds all references to the Observer object in a collection, and each topic can have any number of observers. Abstract topics provide an interface for adding and removing observer objects.
abstract Observer (Observer) role:Define an interface for all specific observers and update themselves when the subject of the observation changes.
specific Theme (concretesubject) role:Stores the related state to the specific observer object, giving notice to all registered observers when the internal state of the specific subject changes. Specific theme roles are usually implemented with a specific subclass.
specific observer (concretedobserver) role:Stores a specific theme object, stores the associated state, and implements the update interface required by the abstract observer role to keep its state and the state of the subject in line.
Iv. Advantages and disadvantages of the Observer model
Observer Mode ofAdvantages:
1, the degree of coupling between the observer and the subject is small;
2, support the broadcast communication;
Observer Mode ofDisadvantages:
1. Since the observer is unaware of the existence of other observers, it may be ignorant of the ultimate cost of changing the target. This may cause unexpected updates.
v. Application of the Observer model to the scene
1. When an abstract model has two aspects, one aspect relies on another.
2. When a change to an object needs to change other objects at the same time, do not know how many objects to change.
3, when an object must notify other objects, and it can not assume that other objects are who. In other words, you don't want these objects to be tightly coupled.
Vi. Observer patterns and other patterns
Mediator Mode (mediator):By encapsulating Complex update semantics, Changemanager acts as an intermediary between the target and the observer.
single case mode (singleton mode):Changemanager can use the singleton mode to ensure that it is unique and globally accessible.
vii. Observer mode PHP sample

<?php/** * Abstract theme Role/interface Subject {/** * Add a new Observer object * @param Observer $observer * * Public fun
 
  Ction Attach (Observer $observer);
 
  /** * Deletes a registered Observer object * @param Observer $observer/Public Function detach (Observer $observer);
/** * Notifies all registered observer objects/Public Function notifyobservers ();
 
  /** * Specific Theme Role * * Class ConcreteSubject implements Subject {private $_observers;
  Public Function __construct () {$this->_observers = array (); /** * Add a new Observer object * @param Observer $observer/Public Function attach (Observer $observer) {return
  Array_push ($this->_observers, $observer); /** * Deletes a registered Observer object * @param Observer $observer/Public Function detach (Observer $observer) {$ind
    ex = Array_search ($observer, $this->_observers);
    if ($index = = False | |! array_key_exists ($index, $this->_observers)) {return false;
    } unset ($this->_observers[$index]); RetUrn TRUE;
      /** * Notifies all registered Observer objects */Public function notifyobservers () {if (!is_array ($this->_observers)) {
    return FALSE;
    foreach ($this->_observers as $observer) {$observer->update ();
  return TRUE;
 
}/** * Abstract observer Role/interface Observer {/** * Update method/Public Function update ();}
 
  Class Concreteobserver implements OBSERVER {/** * Observer's name * @var <type> * * Private $_name;
  Public function __construct ($name) {$this->_name = $name; 
  /** * Update method/Public Function update () {echo ' Observer ', $this->_name, ' has notified.<br/> ';
   }/** * Client/class Client {/** * Main program.
 
    */public static function main () {$subject = new ConcreteSubject ();
    * * ADD first observer/$observer 1 = new Concreteobserver (' Martin ');
 
    $subject->attach ($observer 1);
    Echo ' <br/> The ' notify:<br/> '; $subject->Notifyobservers ();
    * * Add a second observer/$observer 2 = new Concreteobserver (' Phppan ');
 
    $subject->attach ($observer 2);
    Echo ' <br/> The Second notify:<br/> ';
 
    $subject->notifyobservers ();
 
    /* Delete the first observer/* * * $subject->detach ($observer 1);
    Echo ' <br/> The third notify:<br/> ';
  $subject->notifyobservers (); } client::main ();?>

The above is the code that uses PHP to implement Observer mode , and there are some conceptual distinctions about the observer pattern that I hope will help you learn.

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.