Copy code The code is as follows:
/**
* Decoration mode
*
* Dynamically adds some additional responsibilities to an object, which is more flexible than generating subclasses in terms of extended functionality
*/
header(" Content-type:text/html;charset=utf-8");
abstract class MessageBoardHandler
{
public function __construct(){}
abstract public function filter($msg);
}
class MessageBoard extends MessageBoardHandler
{
public function filter($msg)
{
return "Process the content on the message board|".$msg;
}
}
$obj = new MessageBoard();
echo $obj->filter("Be sure to learn the decoration mode
");
// --- The following is the use of decoration mode----
class MessageBoardDecorator extends MessageBoardHandler
{
private $_handler = null;
public function __construct($handler)
{
parent::__construct();
$this->_handler = $handler;
}
public function filter($msg)
{
return $this->_handler->filter($msg);
}
}
// Filter html
class HtmlFilter extends MessageBoardDecorator
{
public function __construct ($handler)
{
parent::__construct($handler);
}
public function filter($msg)
{
return "Filter out HTML tags |".parent::filter($msg);; // Filter out the processing of HTML tags. At this time, just add text without processing
}
}
// Filter sensitive words
class SensitiveFilter extends MessageBoardDecorator
{
public function __construct($handler)
{
parent::__construct($handler);
}
public function filter( $msg)
{
return "Filter out sensitive words|".parent::filter($msg); // The processing of filtering out sensitive words is just adding text without processing at this time
}
}
$obj = new HtmlFilter(new SensitiveFilter(new MessageBoard()));
echo $obj->filter("Be sure to learn the decoration mode!
" );
http://www.bkjia.com/PHPjc/323785.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323785.htmlTechArticleCopy the code as follows: ?php /*** Decoration mode * * Dynamically adds some additional responsibilities to an object, which is more flexible than generating subclasses in terms of extended functionality.*/ header("Content...