Copy Code code as follows:
<?php
/**
* Decoration mode
*
* Dynamically adding 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 <br/>");
---The following is the use of decorative 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);
}
}
Filtering HTML
Class Htmlfilter extends Messageboarddecorator
{
Public function __construct ($handler)
{
Parent::__construct ($handler);
}
Public Function filter ($MSG)
{
Return "filter Out HTML tags |". Parent::filter ($msg);; Filtering out the processing of HTML tags 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); Filter out the processing of sensitive words just add text without processing
}
}
$obj = new Htmlfilter (new Sensitivefilter messageboard ());
echo $obj->filter ("must learn to decorate the pattern!<br/>");