zoukankan      html  css  js  c++  java
  • 执行及描述任务-------访问者模式

    UMl类

    代码实现

    <?php
    //visitor.php 访问者模式
    
    abstract class Unit{
        protected $depth;
        function accept(ArmyVisitor $visitor){
            $method = "visit".get_class($this);
            $visitor->$method($this);
        }
    
        protected function setDepth($depth){
            $this->depth = $depth;
        }
    
        function getDepth(){
            return $this->depth;
        }
    
        function getComposite(){
            return null;
        }
        abstract function bombardStrengrg();
    }
    
    abstract class CompositeUnit extends Unit{
        protected $unit = array();
    
        function getComposite(){
            return $this;
        }
    
    
        function removeUnit(Unit $unit){
            $this->unit = array_udiff($this->unit,array($unit),
                function($a,$b){return ($a===$b)?0:1;});
        }
    
        function addUnit(Unit $unit){
            if(in_array($unit,$this->unit,true)){
                return;
            }
            $unit->setDepth($this->depth+1);
            $this->unit[] = $unit;
        }
    
        function accept(ArmyVisitor $visitor){
            parent::accept($visitor);
            foreach ($this->unit as $thisunit) {
                $thisunit->accept($visitor);
            }
        }
    }
    
    class Army extends CompositeUnit{
        function bombardStrengrg(){
            $ret = 0;
            foreach ($this->unit as $unit) {
                $ret+=$unit->bombardStrengrg();
            }
            return $ret;
        }
    }
    
    class Archer extends Unit{
        function bombardStrengrg(){
            return 4;
        }
    }
    
    class LaserCannon extends Unit{
        function bombardStrengrg(){
            return 5;
        }
    }
    
    abstract class ArmyVisitor{
        abstract function visit(Unit $node);
        function visitArcher(Archer $node){
            $this->visit($node);
        }
    
        
    
        function visitLaserCannon(LaserCannon  $node){
            $this->visit($node);
        }
    
        function visitTroopCarrierUnit(TroopCarrierUnit $node){
            $this->visit($node);
        }
    
        function visitArmy(Army $node){
            $this->visit($node);
        }
    }
    
    class TextDumpArmyVisitor extends ArmyVisitor{
        private $text='';
        function visit(Unit $node){
            $ret = '';
            $pad = 4*$node->getDepth();
            $ret.=sprintf("%{$pad}s",'');
            $ret.=get_class($node).": ";
            $ret.="bombard: ".$node->bombardStrengrg()."<br>";
            $this->text.=$ret;
        }
    
        function getText(){
            return $this->text;
        }
    }
    
    class TaxCollectionVisitor extends ArmyVisitor{
        private $due=0;
        private $report=0;
        function visit(Unit $node){
            $this->levy($node,1);
        }
    
        function visitArcher(Archer $node){
            $this->levy($node,2);
        }
    
        function visitTroopCarrierUnit(TroopCarrierUnit $node){
            $this->levy($node,2);
        }
    
        function visitLaserCannon(LaserCannon $node){
            $this->levy($node,2);
        }
    
        function levy(Unit $unit,$amout){
            $this->report.="Tax levied for ".get_class($unit);
            $this->report.=": $amout<br>";
            $this->due+=$amout;
        }
    
        function getRepost(){
            return $this->report;
        }
    
        function getTax(){
            return $this->due;
        }
    }
    
    $main_army = new Army();
    $main_army->addUnit(new Archer());
    $main_army->addUnit(new LaserCannon());
    
    //$textdump = new TextDumpArmyVisitor();
    $taxcollector = new TaxCollectionVisitor();
    $main_army->accept($taxcollector);
    //echo $textdump->getText();
    echo "TOTAL: ";
    echo $taxcollector->getTax()."<br>";
    echo $taxcollector->getRepost();
    ?>

    问题

    访问者可可以用于任何对象集合

    外部化操作可能破坏封装,例如给TextdumpArmyVisitor对象提供信息,我们被迫给Unit接口提供了一个额外的方法

  • 相关阅读:
    spring 循环依赖问题
    spring data jpa 关键字 命名
    mongodb 添加字段并设置默认值
    java mongoTemplate的group统计
    java8 从对象集合中取出某个字段的集合
    springboot12-zuul
    springboot11-01-security入门
    springboot项目怎么部署到外部tomcat
    springboot10-springcloud-eureka 服务注册与发现,负载均衡客户端(ribbon,feign)调用
    UML
  • 原文地址:https://www.cnblogs.com/rcjtom/p/6071080.html
Copyright © 2011-2022 走看看