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