PHP设计模式学习笔记: 责任链模式(Chain of Responsibility)
// 抽象书本类 abstract class AbstractBookTopic { abstract function getTopic(); abstract function getTitle(); abstract function setTitle($title_in); } // 书本类,继承自抽象书本类 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 ‘目前没有标题‘; } } function setTitle($title_in) { $this->title = $title_in; } } // 子书本类,继承自抽象书本类 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; } } // 子子书本类,继承自抽象书本类 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("开始测试责任链模式"); writeln(""); $bookTopic = new BookTopic("魔法"); writeln("bookTopic设置标题前:"); writeln("主题: " . $bookTopic->getTopic()); writeln("标题: " . $bookTopic->getTitle()); writeln(""); $bookTopic->setTitle("哈利波特与魔法石"); writeln("bookTopic设置标题后: "); writeln("主题: " . $bookTopic->getTopic()); writeln("标题: " . $bookTopic->getTitle()); writeln(""); $bookSubTopic = new BookSubTopic("英国魔法", $bookTopic); writeln("bookSubTopic标题设置前: "); writeln("主题: " . $bookSubTopic->getTopic()); writeln("标题: " . $bookSubTopic->getTitle()); writeln(""); $bookSubTopic->setTitle("哈利波特与混血王子"); writeln("bookSubTopic标题设置后: "); writeln("主题: ". $bookSubTopic->getTopic()); writeln("标题: ". $bookSubTopic->getTitle()); writeln(""); $bookSubSubTopic = new BookSubSubTopic("英国现代魔法", $bookSubTopic); writeln("bookSubSubTopic标题设置前: "); writeln("主题: " . $bookSubSubTopic->getTopic()); writeln("标题: " . $bookSubSubTopic->getTitle()); writeln(""); $bookSubTopic->setTitle(NULL); writeln("bookSubSubTopic、 bookSubTopic都没有设置标题:"); writeln("主题: " . $bookSubSubTopic->getTopic()); writeln("标题: " . $bookSubSubTopic->getTitle()); writeln(""); writeln("结束测试责任链模式"); function writeln($line_in) { echo $line_in.PHP_EOL; }
开始测试责任链模式 bookTopic设置标题前: 主题: 魔法 标题: 目前没有标题 bookTopic设置标题后: 主题: 魔法 标题: 哈利波特与魔法石 bookSubTopic标题设置前: 主题: 英国魔法 标题: 哈利波特与魔法石 bookSubTopic标题设置后: 主题: 英国魔法 标题: 哈利波特与混血王子 bookSubSubTopic标题设置前: 主题: 英国现代魔法 标题: 哈利波特与混血王子 bookSubSubTopic、 bookSubTopic都没有设置标题: 主题: 英国现代魔法 标题: 哈利波特与魔法石 结束测试责任链模式
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。