PHP Design Patterns Learning notes: The responsibility chain model (Chain of Responsibility)

Source: Internet
Author: User

Abstract class Abstraction class Abstractbooktopic {abstract function gettopic ();    Abstract function getTitle (); Abstract function Settitle ($title _in);}    Book class, inherit from abstract book class Booktopic extends Abstractbooktopic {private $topic;    Private $title;      function __construct ($topic _in) {$this->topic = $topic _in;    $this->title = NULL;    } function Gettopic () {return $this->topic;      } function GetTitle () {if (NULL! = $this->title) {return $this->title;      } else {return ' currently has no title ';    }} function Settitle ($title _in) {$this->title = $title _in;    }}//, inheriting from abstract book class Booksubtopic extends Abstractbooktopic {private $topic;    Private $parentTopic;    Private $title;      function __construct ($topic _in, booktopic $parentTopic _in) {$this->topic = $topic _in;      $this->parenttopic = $parentTopic _in;    $this->title = NULL;    } function Gettopic () {return $this->topic; } function Getparenttopic () {return $this->parenttopic;      } function GetTitle () {if (NULL! = $this->title) {return $this->title;      } else {return $this->parenttopic->gettitle ();    }} function Settitle ($title _in) {$this->title = $title _in;    }}//Sub-book class, Inherit from abstract book class Booksubsubtopic extends Abstractbooktopic {private $topic;    Private $parentTopic;    Private $title;      function __construct ($topic _in, booksubtopic $parentTopic _in) {$this->topic = $topic _in;      $this->parenttopic = $parentTopic _in;    $this->title = NULL;    } function Gettopic () {return $this->topic;    } function Getparenttopic () {return $this->parenttopic;      } function GetTitle () {if (NULL! = $this->title) {return $this->title;      } else {return $this->parenttopic->gettitle ();    }} function Settitle ($title _in) {$this->title = $title _in; }} WritELN ("Start testing responsibility chain mode");  Writeln ("");  $bookTopic = new Booktopic ("Magic");  Writeln ("Booktopic before setting the title:"); Writeln ("Subject:".  $bookTopic->gettopic ()); Writeln ("title:".  $bookTopic->gettitle ());  Writeln ("");  $bookTopic->settitle ("Harry Potter and the Philosopher's Stone");  Writeln ("Booktopic after setting the title:"); Writeln ("Subject:".  $bookTopic->gettopic ()); Writeln ("title:".  $bookTopic->gettitle ());   Writeln ("");  $bookSubTopic = new Booksubtopic ("English Magic", $bookTopic);  Writeln ("Booksubtopic title pre-set:"); Writeln ("Subject:".  $bookSubTopic->gettopic ()); Writeln ("title:".  $bookSubTopic->gettitle ());  Writeln ("");  $bookSubTopic->settitle ("Harry Potter and the Half-Blood Prince");  Writeln ("Booksubtopic title set After:"); Writeln ("Subject:".  $bookSubTopic->gettopic ()); Writeln ("title:".  $bookSubTopic->gettitle ());   Writeln ("");  $bookSubSubTopic = new Booksubsubtopic ("Modern British Magic", $bookSubTopic);  Writeln ("Booksubsubtopic title pre-set:"); Writeln ("Subject:".  $bookSubSubTopic->gettopic ()); Writeln ("title:".  $bookSubSubTopic->gettitle ());  Writeln (""); $bookSubTopic->settitle (NULL);  Writeln ("Booksubsubtopic, Booksubtopic are not set title:"); Writeln ("Subject:".  $bookSubSubTopic->gettopic ()); Writeln ("title:".  $bookSubSubTopic->gettitle ());  Writeln ("");  Writeln ("End Test responsibility chain mode"); function Writeln ($line _in) {echo $line _in.  Php_eol; }



Results:

Start testing the responsibility chain mode Booktopic set title before: Theme: Magic title: currently no title booktopic after setting title: Theme: Magic title: Harry Potter and the Philosopher's Stone booksubtopic title before: Theme: English Magic title: Harry Potter and the Philosopher's Stone booksubtopic title after set: Theme: English magic title: Harry Potter and the Half-Blood Prince Booksubsubtopic title: The English Modern Magic title: Harry Potter and the Half-Blood Prince Booksubsubtopic, Booksubtopic is not set title: theme: British modern Magic title: Harry Potter and the Philosopher's Stone end test responsibility chain mode





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.