php 装饰模式

 

<?php

/**
 * 装饰模式用来帮助我们改变具体组件的功能
 * 装饰模式结构类似组合模式,组合是在代码运行时实现的,
 * 装饰模式可以很轻松的进行多个类的调用
 */

class Filter{
    
}

//抽象基类
abstract class ProcessFilter {
    
     abstract function process(Filter $filter);
    
}

//composite 具体的组件(request filter)
class ProcessRequestFilter extends ProcessFilter{
    public function process(Filter $filter ){
        print_r(__CLASS__."  <br>");
    }
}

//decorate 装饰器入口
abstract class DecorateFilter extends ProcessFilter {
     protected $processrequest;
     function __construct(ProcessFilter $pr )
     {
         $this->processrequest = $pr;
     }
    
}

class LogRequest extends DecorateFilter{
    public function process(Filter $filter)
    {
        print_r(__CLASS__." 39 <br>");
        $this->processrequest->process($filter);
    }
}

class AuthRequest extends DecorateFilter{
    public function process(Filter $filter)
    {
        print_r(__CLASS__." 48 <br>");
        $this->processrequest->process($filter);
    }
}

class StructureRequest extends DecorateFilter{
    public function process(Filter $filter)
    {
        print_r(__CLASS__." 56 <br>");
        $this->processrequest->process($filter);
    }
}

$process = new AuthRequest(new StructureRequest(
                           new LogRequest(
                           new ProcessRequestFilter()
            )));
//$process->process(new Filter());

/**
 * “小猪逃命”游戏:一只小猪和一只灰狼,小猪最多5条命,灰狼每咬到小猪一次,小猪就要少一条命,小猪的任务是要逃过灰狼的追咬到猪栏。在逃的过程中小猪可以吃到三种苹果,吃“红苹果”可以给小猪加上保护罩,吃“绿苹果”可以加快小猪奔跑速度,吃“黄苹果”可以使猪趟着水跑。小猪如果吃多种苹果的话,小猪可以拥有多种苹果提供的功能。
 */


/*
 * 思路以及实现
 * 下列代码最终能够实现 (7步骤) 一次执行,
 * 也就是说 小猪会 一次吃到多个苹果,受到多次攻击
 * 
 * 小猪速度为3
 * 大灰狼速度为4
 * 大灰狼攻击力为1
 * 小猪生命为5
 * (绿苹果) 加速苹果速度+3
 * (黑苹果) 加速苹果速度-2
 * (红苹果) 防护罩苹果可抵御2次大灰狼攻击
 * (黄苹果) 飘逸苹果可以增加小猪在河水中的速度+2
 * 河水宽度为2
 * 河水对小猪减速 -2
 * 河水对大灰狼速度-1
 * 
 * 
 * 小猪向前跑了3(3)米,大灰狼离小猪3米
 * 小猪向前跑了3(6)米,大灰狼离小猪2米
 * 小猪向前跑了3(9)米,小猪吃到了加速(速度+3),大灰狼离小猪1米
 * 小猪向前跑了6(15)米,小猪吃到了加速(速度+3),大灰狼离小猪2米(13)
 * 小猪向前跑了6(21)米,小猪吃到了加速(速度+3),大灰狼离小猪4米(17)
 */


class Pig{
    private  $velocity = 3 ; 
    private  $heart = 5 ;
    private $is_in_water = false;
    /**
     * 速度
     */
    public function getVelocity(){
        return $this->velocity;
    }
    public function setVelocity($velocity){
        return $this->velocity = $velocity;
    }
    public function getHeart(){
        return $this->heart;
    }
    public function setHeart($heart){
        return $this->heart = $heart;
    }
    public function isInWater(){
        return false;
    }
}
class Wolf{
    private  $velocity = 4;
    /**
     * 速度
     */
    public function getVelocity(){
        return $this->velocity;
    }
    
    public function getAtk(){
        return 1;
    }
}

abstract  class PigComment{
    //abstract function getVelocity(PigVelocity $pig);
}

abstract  class PigVelocityDecoractor extends PigComment{
    abstract function setVelocity(Pig $pig);
}

abstract class PigHeartDecorator extends PigComment{
    abstract public function setHeart(Pig $pig);
}

class PigWolfAttack extends PigHeartDecorator{
    public function setHeart(Pig $pig){
        $pig->setHeart($pig->getHeart()-1);
    }
}

class PigTigerAttack extends PigHeartDecorator{
    public function setHeart(Pig $pig){
        $pig->setHeart($pig->getHeart()-3);
    }
}

          
class PigOrangeApple extends PigVelocityDecoractor{

    public function setVelocity(Pig $pig){
        if($pig->isInWater()){
            $pig->setVelocity($pig->getVelocity+2);
        }
    }
}

//class PigRedApple extends PigVelocityDecoractor{
//    public function setVelocity(PigVelocity $pig){
//        if($pig->is_in_water()){
//            $pig->setVelocity($pig->getVelocity+2);
//        }
//    }
//}
class PigGreenApple extends PigVelocityDecoractor{
    public function setVelocity(Pig $pig){
        $pig->setVelocity($pig->getVelocity()+3);
    }
}
class PigBlackApple extends PigVelocityDecoractor{
    public function setVelocity(Pig $pig){
        $pig->setVelocity($pig->getVelocity()-2);
    }
}

$pig = new pig();
echo $pig->getVelocity();
echo ':pig 速度为3<br>';

$orage_Apple = new PigOrangeApple($pig);
$orage_Apple->setVelocity($pig);
echo $pig->getVelocity();
echo ':pig吃掉了orange Apple 速度不变(没在水中)<br>';


$wolf = new PigWolfAttack($pig);
$wolf->setHeart($pig);
echo ':pig遭受wolf 攻击,生命剩余:'.$pig->getHeart().'<br>';


$wolf = new PigTigerAttack($pig);
$wolf->setHeart($pig);
echo ':pig遭受wolf 攻击,生命剩余:'.$pig->getHeart().'<br>';
 


$green_Apple = new PigGreenApple($pig);
$green_Apple->setVelocity($pig);
echo $pig->getVelocity();
echo ':pig吃掉了green Apple 速度+3<br>';

$black_Apple = new PigBlackApple($pig);
$black_Apple->setVelocity($pig);
echo $pig->getVelocity();
echo ':pig吃掉了black Apple 速度-2<br>';


$green_Apple = new PigGreenApple($pig);
$green_Apple->setVelocity($pig);
echo $pig->getVelocity();
echo ':pig吃掉了green Apple 速度+3<br>';

posted on 2013-03-04 11:52  一天一點  阅读(115)  评论(0)    收藏  举报