Php design mode Decorator (decoration mode) _ PHP Tutorial

Source: Internet
Author: User
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.