PHPデザインモードのデコレーションモード

5462 ワード

<?php



/*

                 。      ,                  。

*/

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("         <br/>");









// ---           ----

//       ----



//         ---

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);

    }

    



}



//----



class HtmlFilter extends MessageBoardDecorator

{

    public function __construct($handler)

    {

        parent::__construct($handler);

    }

    

    public function filter($msg)

    {

        return "   HTML  |".parent::filter($msg);

    }

}





class SensitiveFilter extends MessageBoardDecorator

{

    public function __construct($handler)

    {

        parent::__construct($handler);

    }



    public function filter($msg)

    {

        return "   HTML|".parent::filter($msg);//                        

    }

    

}



    $obj = new HtmlFilter(new SensitiveFilter(new MessageBoard()));

    

    echo $obj->filter("        <br/>");