PHP In-depth observer mode

Source: Internet
Author: User
PHP In-depth observer mode
#### Observer mode * The Observer is designed to let an object track a certain state and know how the state changes. once the state changes, all subscribed objects can be notified ##### use SPL to implement observer mode * the Splsubject interface has three abstract methods: attach (), detach (), and notify Y () these methods do not return any results * the SplObserver interface has a method update () it will get the latest status of the subject state and hand it to the Observer instance * SplObjectStorage class description is a ing from the object to the data or object set"
 Observers = new SplObjectStorage ();} // add the public function attach (\ SplObserver $ observer) {$ this-> observers-> attach ($ observer );} // delete the public function detach (\ SplObserver $ observer) {$ this-> observers-> detach ($ observer);} // notification forwarding public function Y () {foreach ($ this-> observers as $ observer) {$ observer-> update ($ this) ;}// set public function setData ($ dataNow) {$ this-> data = $ dataNow;} public function getData () {return $ this-> data ;}} // SPL specific observer class ConcreteObserver implements SplObserver {public function update (SplSubject $ subject) {echo $ subject-> getData ();} class Client {public function _ construct () {echo"

Create three new concrete observers "; // create an observer $ ob1 = new ConcreteObserver (); $ ob2 = new ConcreteObserver (); $ ob3 = new ConcreteObserver (); $ subject = new ConcreteSubject (); $ subject-> setObservers (); $ subject-> setData ("here is u data "."
"); $ Subject-> attach ($ ob1); $ subject-> attach ($ ob2); $ subject-> attach ($ ob3 ); $ subject-> publish Y (); echo"

Reset data and reattach ob3

"; $ Subject-> setData (" more data that only "."
"); $ Subject-> attach ($ ob3); $ subject-> attach ($ ob2); $ subject-> Policy ();}} $ data = new Client (); "##### free PHP and observer modes" Observers, $ obser);} public function detachObser (Observer $ obser) {// remove data from the array $ position = 0; foreach ($ this-> observers as $ viwer) {++ $ position; if ($ viwer ==$ obser) {array_splice ($ this-> observers, ($ position), 1) ;}} public function notify () {foreach ($ this-> observers as $ viewer) {// call the update method $ viewer-> update ($ this );}}} class ConcreteSubject extends Subject {public function setState ($ stateSet) {$ this-> stateNow = $ stateSet;} public function getState () {return $ this-> stateNow ;}} class ConcreteObserverDT implements Observer {private $ currentState; public function update (Subject $ subject) {$ this-> currentState = $ subject-> getState (); echo "DT ";}} class ConcreObserverTablet implements Observer {private $ currentState; public function update (Subject $ subject) {$ this-> currentState = $ subject-> getState (); echo "tablet ";}} class ConcreObserverPhone implements Observer {private $ currentState; public function update (Subject $ subject) {$ this-> currentState = $ subject-> getState (); echo "phone ";}} class Client {public function _ construct () {$ sub = new ConcreteSubject (); $ ob1 = new ConcreObserverPhone (); $ ob2 = new ConcreObserverTablet (); $ ob3 = new ConcreteObserverDT (); $ sub-> attachObser ($ ob1); $ sub-> attachObser ($ ob2); $ sub-> attachObser ($ ob3 ); $ sub-> setState ("fuck"); $ sub-> detachObser ($ ob1); $ sub-> Policy () ;}}$ data = new Client ();"

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.