Home > Backend Development > PHP Tutorial > PHP design pattern——Observer pattern

PHP design pattern——Observer pattern

WBOY
Release: 2016-08-08 09:25:34
Original
999 people have browsed it

Observer Pattern

Observer pattern (Observer), when the state of an object changes, all objects that rely on it will receive notifications and automatically update.

The role in the pattern

  • Abstract observer (abstract class, easy to extend) stores the observer object in a container. This class provides some interfaces, such as adding observers, canceling observers, and notifying observers (notify )
  • Concrete observer (concrete class, inherits the abstract class of the observed) Stores the observer that needs to be notified. When the observer needs to update, call the notify method
  • Abstract observer (interface or abstract class) is concrete Observers provide an updated interface and update when notified by the observer
  • Concrete observer (concrete class, inherit or implement abstract observer) implements the interface of abstract observer and automatically updates

phpDemo

Abstract observer

<code><span><?php</span><span>abstract</span><span><span>class</span><span>EventGenerator</span>{</span><span>private</span><span>$observer_arr</span> = <span>array</span>();

    <span>/*
        添加观察者
     */</span><span>public</span><span><span>function</span><span>addObserver</span><span>( Observer <span>$observer</span>)</span>
    {</span><span>$this</span>->observer_arr[] = <span>$observer</span>;
    }

    <span>/*
        通知所有观察者
     */</span><span>public</span><span><span>function</span><span>notify</span><span>()</span>
    {</span><span>foreach</span> (<span>$this</span>->observer_arr <span>as</span><span>$observer</span>) 
        {
            <span>$observer</span>->update();
        }
    }

}
</span></code>
Copy after login

Concrete observer

<code><span><span>class</span><span>Event</span><span>extends</span><span>EventGenerator</span>{</span><span>public</span><span><span>function</span><span>trigger</span><span>()</span>
    {</span><span>echo</span><span>'event happen!<br/>'</span>;
        <span>//当事件发生时,通知所有观察者</span><span>$this</span>->notify();
    }

}
</code>
Copy after login

Abstract observer

<code><span><?php</span><span><span>interface</span><span>Observer</span>{</span><span>//自动更新</span><span><span>function</span><span>update</span><span>()</span>;</span>
}

</code>
Copy after login

Concrete observer

<code><span><span>class</span><span>Observer1</span><span>implements</span><span>Observer</span>{</span><span>//实现update方法</span><span>public</span><span><span>function</span><span>update</span><span>()</span>
    {</span><span>echo</span><span>'observer1 update<br/>'</span>;
    }
}

<span><span>class</span><span>Observer2</span><span>implements</span><span>Observer</span>{</span><span>//实现update方法</span><span>public</span><span><span>function</span><span>update</span><span>()</span>
    {</span><span>echo</span><span>'observer2 update<br/>'</span>;
    }
}

</code>
Copy after login

Test code

<code><span>$obj</span> = <span>new</span> Event();
<span>//添加观察者</span><span>$obj</span>->addObserver(<span>new</span> Observer1());
<span>$obj</span>->addObserver(<span>new</span> Observer2());
<span>$obj</span>->trigger();
</code>
Copy after login

Pattern summary

  • Advantages: The observer pattern implements low coupling, non-intrusive notification and automatic updates Mechanism
  • Disadvantages: The dependency relationship is not completely eliminated, the abstract notifier still relies on the abstract observer
  • Trial scenarios: 1. When a change in an object requires changing other objects, and it does not know how many objects are waiting. When changing; 2. An abstract type has two aspects. When one aspect depends on the other aspect, the observer pattern can be used to encapsulate the two in independent objects so that they can be changed and reused independently.

The above introduces the observer pattern, one of the PHP design patterns, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

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