Php design mode Decorator (decoration mode ). Copy the code as follows :? Php *** decoration mode ** dynamically adds some additional responsibilities to an object, which is more flexible than the subclass method in terms of the extension function * header (Content
The code is as follows:
/**
* 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 ("You must learn the decoration mode well.
");
// --- 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!
");
The http://www.bkjia.com/PHPjc/323785.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/323785.htmlTechArticle code is as follows :? Php/*** decoration mode ** dynamically adds some additional responsibilities to an object, which is more flexible than the subclass method in terms of extended functions */header ("Content...