<!-- 装饰器模式 -->
<?php
/*装饰器:动态修改类的功能*/
interface Decorator{
public function before();
public function after();
}
?>
<?php
require_once "Decorator.php";
class DecoratorO implements Decorator{
public function before(){
echo "DecoratorObefore";
}
public function after(){
echo "DecoratorOafter";
}
}
?>
<?php
require_once "Decorator.php";
class DecoratorT implements Decorator{
public function before(){
echo "DecoratorTbefore";
}
public function after(){
echo "DecoratorTafter";
}
}
?>
<?php
require_once "DecoratorO.php";
require_once "DecoratorT.php";
class Echotext{
public $decorators = array();
public function addDecorator($decorator)
{
$this->decorators[] = $decorator;
}
public function index()
{
$this->beforedo();
echo "do";
$this->afterdo();
}
public function beforedo()
{
foreach ($this->decorators as $decorator) {
$decorator->before();
}
}
public function afterdo()
{
foreach ($this->decorators as $decorator) {
$decorator->after();
}
}
}
$text = new Echotext();
$text->addDecorator(new DecoratorO());
$text->addDecorator(new DecoratorT());
$text->index();
?>
<?php
/*装饰器:动态修改类的功能*/
interface Decorator{
public function before();
public function after();
}
?>
<?php
require_once "Decorator.php";
class DecoratorO implements Decorator{
public function before(){
echo "DecoratorObefore";
}
public function after(){
echo "DecoratorOafter";
}
}
?>
<?php
require_once "Decorator.php";
class DecoratorT implements Decorator{
public function before(){
echo "DecoratorTbefore";
}
public function after(){
echo "DecoratorTafter";
}
}
?>
<?php
require_once "DecoratorO.php";
require_once "DecoratorT.php";
class Echotext{
public $decorators = array();
public function addDecorator($decorator)
{
$this->decorators[] = $decorator;
}
public function index()
{
$this->beforedo();
echo "do";
$this->afterdo();
}
public function beforedo()
{
foreach ($this->decorators as $decorator) {
$decorator->before();
}
}
public function afterdo()
{
foreach ($this->decorators as $decorator) {
$decorator->after();
}
}
}
$text = new Echotext();
$text->addDecorator(new DecoratorO());
$text->addDecorator(new DecoratorT());
$text->index();
?>