Copy codeThe Code is as follows:
<? Php
/**
* Decoration Mode
*
* Dynamically add some additional responsibilities to an object, which is more flexible in terms of the extension function than the subclass generation method.
*/
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 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 HTML tags. In this case, only text is added and not processed.
}
}
// 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 sensitive words. At this time, only a text is added and not processed.
}
}
$ Obj = new HtmlFilter (new SensitiveFilter (new MessageBoard ()));
Echo $ obj-> filter ("Be sure to learn the decoration mode! <Br/> ");