PHP Viewer design Pattern test code notes

Source: Internet
Author: User
 '; }}/** * Observer 2 * Class Savename */class Savename implements Observer {public Function update () {echo ' The Nam E has been save
'; }}/** * Viewer interface * Interface observered */interface observered {public function attach ($object); Public Function Delete ($object); Public function notify ();} /** * The viewer Class user */class User implements observered {public $observers = array (); /** * Registered Observer * @param $object */Public Function attach ($object) {if (!is_object ($object)) { Exit (' It is not a object '); if ($object instanceof Observer) {if (!in_array ($object, $this->observers)) {$this- >observers[] = $object; }} else {echo Get_class ($object). ' is disable
'; }}/** * Delete Observer * @param $object */Public Function Delete ($object) {if (In_array ($object, $this->observers)) {$index = Array_search ($object, $this->observers); unset ($this->observers[$index]); }}/** * Notifies all observers */Public function notify () {foreach ($this->observers as $val) { $val->update (); }}/** * The observed person's own business */Public Function printname () {echo ' I had been observing
'; }} $object = new User (); $object->attach (New SendEmail ()); Registered Observer 1 $object->attach (new Savename ()); Registered Observer 2 $object->printname (); The observed person's own business $object->notify (); To inform all observers
  • 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.