Observer mode example

Source: Internet
Author: User

Observer mode. one-to-many mode: the observer is "one", and the observer is "multiple ".
Once "1" changes, "more" changes accordingly.

In the following example:
Microsoft is "observed" and two employees are "observers ".
Once Microsoft loses money or goes bankrupt (it seems like a great deal of time), every employee will be affected accordingly.

Microsoft (class)-> Company (abstract class)-> observer (Interface)
Employee (class)-> observer (Interface)

/** <Br/> * Observer interface <br/> * method to be defined: add, remove, and notify the observer <br/> */<br/> interface subject {<br/> function registerobserver ($ employee ); <br/> function removeobserver ($ employee); <br/> function policyobserver ($ Str ); <br/>}</P> <p>/** <br/> * Observer interface <br/> * method to be defined: Corresponding update action (after notification) <br/> */<br/> interface observer {<br/> function updata ($ Str ); <br/>}</P> <p>/** <br/> * the abstract class of the company belongs to the observer. <br/> * implementation method: add, remove, and notify the observer <br/> */<br/> abstract class company implements subject {<br/> protected $ _ employee_arr = array (); // array of employee objects (employee list) <br/> protected $ _ status; // company operation status </P> <p> Public Function registerobserver ($ employee) <br/>{< br/> $ this-> _ employee_arr [] = $ employee; <br/>}</P> <p> Public Function removeobserver ($ employee) <br/>{< br/> foreach ($ this-> _ employee_arr as $ k => $ V) <br/>{< br/> if ($ V ===$ employee) unset ($ this-> _ employee_arr [$ K]); <br/>}</P> <p> Public Function policyobserver ($ Str) <br/>{< br/> // based on the list, notify employees one by one <br/> foreach ($ this-> _ employee_arr as $ v) <br/>{< br/> $ V-> updata ($ Str ); <br/>}</P> <p>/** <br/> * Microsoft inherits the COMPANY Abstract class <br/> * /<br/> class Microsoft extends company {</P> <p> public function _ construct () // listed company <br/>{< br/> $ this-> _ Status = 'operation '; <br/>}</P> <p> Public Function adduser ($ employee) // New Employee <br/>{< br/> parent :: registerobserver ($ employee); <br/>}</P> <p> Public Function deluser ($ employee) // dismiss an employee <br/>{< br/> parent:: removeobserver ($ employee); <br/>}</P> <p> Public Function changestatus ($ status) // Changes in company status <br/>{< br/> switch ($ status) {<br/> case 'losses ': $ STR = 'salary demo'; break; <br/> case 'failed': $ STR = 'laid-off'; break; <br/> default: $ STR = 'normal work '; <br/>}< br/> parent: notifyobserver ($ Str ); <br/>}</P> <p>/** <br/> * implement the observer interface for employees <br/> * Implementation Method: action after receiving the notification <br/> */<br/> class employee implements observer {</P> <p> Public Function updata ($ Str) <br/>{< br/> echo $ Str. '<br/>'; // employee status <br/>}</P> <p> $ M = new Microsoft (); // Microsoft listed <br/> $ e1 = new employee (); // applicant E1 <br/> $ e2 = new employee (); // candidate E2 <br/> $ M-> adduser ($ E1); // E1 becomes a formal employee <br/> $ M-> adduser ($ E2 ); // E2 becomes a formal employee <br/> $ M-> changestatus ('login'); // The company loses money <br/>/* <br/> output at this time: <br/> pay-as-you-go <br/> */<br/> $ M-> deluser ($ E2 ); // fire employee E2 <br/> $ M-> changestatus ('failed'); // The company is closed <br/>/* <br/> output at this time: <br/> laid-off <br/> */

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.