首頁  >  文章  >  後端開發  >  dvd.taiwan-sex.com php設計模式 Decorator裝飾模式

dvd.taiwan-sex.com php設計模式 Decorator裝飾模式

WBOY
WBOY原創
2016-07-29 08:45:482030瀏覽

複製代碼 代碼如下:


/**
* 裝飾模式
*
* 動態的給一個物件添加一些額外的職責,就擴展功能而言比生成子類方式更為靈活
*/
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 "處理留言板上的內容|".$msg;
}
}
}
$obj = new MessageBoard();
echo $obj->filter("一定要學好裝飾模式
");
// --- 以下是使用裝飾模式-- --
class MessageBoardDecorator extends MessageBoardHandler
{
private $_handler = null;
public function __construct($handler)
{
public function __construct($handler)
{
public function __construct($handler)
{
public function (); this->_handler = $handler;
}
public function filter($msg)
{
return $this->_handler->filter($msg);
}
}
// 過濾html
class HtmlFilter extends MessageBoardDecorator
{
public function __construct($handler)
{
parent::__construct($handler); 🎜>public function filter($msg)
{
return "過濾掉HTML標籤|".parent::filter($msg);; // 過濾掉HTML標籤的處理這時只是加個文字沒有進行處理
}
}
// 過濾敏感詞
class SensitiveFilter extends MessageBoardDecorator
{
public function __construct($handler)
{
parent::__construct ($handler);
}
public function filter($msg)
{
return "過濾掉敏感字|".parent::filter($msg); // 濾掉敏感字詞的處理這時只是加個文字沒有處理
}
}
$obj = new HtmlFilter(new SensitiveFilter(new MessageBoard()));

echo $obj->filter("一定學好裝飾模式!
");

以上就介紹了dvd.taiwan-sex.com php設計模式 Decorator裝飾模式,包括了dvd.taiwan-sex.com方面的內容,希望對PHP教程有興趣的朋友有所幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn