Home > Web Front-end > JS Tutorial > body text

Detailed explanation of how to implement observer pattern code in javascript and php

伊谢尔伦
Release: 2017-07-24 15:08:25
Original
1143 people have browsed it

The observer pattern means that when the state of an object changes, all objects that depend on the state are automatically notified. The observed and the observers have a one-to-many relationship. The book also gives a very easy-to-understand example: getting the current system time and outputting it to the command line. If you want to hardcode it, it's very simple. Define a method to get the time first and then process the display. In this way, the function of obtaining time cannot be reused, because it is a drag bottle for processing display.

If you use the observer pattern, you can define two classes, one for timing and getting the current time; the other for displaying the time. The class that displays the time is the observer. The advantage of this is to reduce mutual dependence. The first class does not need to handle anything after the change. It only needs to send notifications to tell its observers that specific things are handled by them. For example, one day we want to display the time in a different form, or display the time in multiple ways, without involving the first class at all.

php implementation

//被观察者
class Subject
{
    private $_observers;
    public function __construct() {
        $this->_observers = array();
    }
    public function add_observer($obs) {
        $this->_observers[] = $obs;
    }
    public funtion delete_observer($bos) {
        $index = array_search($bos, $this->_observers);
        unset($this->_observers[$index]);
    }
    public function notify_observers() {
        foreach($this->_observers as $v) {
            $v->update();
        }
    }
}
//观察者
class Observer
{
    public function __construct() {
        do sth;
    }
    public function update() {
        do sth;
    }
}
//实例
$sub = new Subject();
$obs = new Observer();
$sub->add_observer($obs);
$sub->notify_observers();
Copy after login

js implementation

js implementation is not troublesome, you just have to write some tool functions for easy use , such as deleting the specified element from the array. Only the simplest implementation method is used below.

//被观察者
function Subject() {
    var _this = this;
    this.observers = [];
    this.addObserver = function(obj) {
        _this.observers.push(obj);
    }
    this.deleteObserver = function(obj) {
        var length = _this.observers.length;
        for(var i = 0; i < length; i++) {
            if(_this.observers[i] === obj) {
                _this.observers.splice(i, 1);
            }
        }
    }
    this.notifyObservers = function() {
        var length = _this.observers.length;
        console.log(length)
        for(var i = 0; i < length; i++) {
            _this.observers[i].update();
        }
    }
}
//观察者
function Observer() {
    this.update = function() {
        alert(1)
    }
}
var sub = new Subject();
var obs = new Observer();
sub.addObserver(obs);
sub.notifyObservers();
var sub = new Subject();
Copy after login

The above is the detailed content of Detailed explanation of how to implement observer pattern code in javascript and php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template