首页 > 代码库 > PHP设计模式学习笔记: 责任链模式(Chain of Responsibility)
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都没有设置标题: 主题: 英国现代魔法 标题: 哈利波特与魔法石 结束测试责任链模式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。