PHP設計模式之訪問者模式

訪問者模式解決的問題

在咱們的代碼編寫過程中,常常須要對一些相似的對象添加一些的代碼,咱們以一個計算機對象打印組成部分爲例來看下:數據庫

/**
    * 抽象基類
    */
    abstract class Unit
    {
        /**
        *獲取名稱
        */
        abstract public function getName();

    }

    /**
    * Cpu類
    */
    class Cpu extends Unit
    {
        public function getName()
        {
            return 'i am cpu';
        }
    }

    /**
    * 內存類
    */
    class Memory extends Unit
    {
        public function getName()
        {
            return 'i am memory';
        }
    }

    /**
    * 鍵盤類
    */
    class Keyboard extends Unit
    {
        public function getName()
        {
            return 'i am keyboard';
        }
    }

    /**
    * 計算機類
    */
    class Computer
    {
        protected $_items = [];

        public function add(Unit $unit)
        {
            $this->_items[] = $unit;
        }

        public function print()
        {
            // 循環打印各個組成部分
            foreach ($this->_items as $item) {
                $item->getName();
            }
        }
    }

這個時候上面的代碼看上去好像很完美的樣子,可是問題來了,如今咱們不但須要打印組成部分,還須要保存各個組件到數據庫,不只如此,還須要打印各個組件的價格;此時若是在Unit基類中添加getPrice()和save()方法,也能實現咱們想要的功能,可是這樣作也存在問題,你並不知道還須要新增什麼操做,若是每新增一個操做都用這樣的方法來新增,咱們的類會變得愈來愈臃腫。數據結構

訪問者模式的實現

而訪問者模式就是爲了解決這個問題的,他把數據結構和做用於結構之上的操做之間的耦合解脫開,使得操做集合能夠相對自由的演化,咱們來看下面改良過得代碼:this

/**
    * 抽象基類
    */
    abstract class Unit
    {
        /**
        * 獲取名稱
        */
        abstract public function getName();

        /**
        * 用來接受訪問者對象,回調訪問者的visit方法
        * 很是關鍵的方法
        */
        public function accept(Visitor $visitor)
        {
            $method = visit . get_class($this);
            if (method_exists($visitor, $method)) {
                $visitor->$method($this);
            }
        }
    }

    /**
    * Cpu類
    */
    class Cpu extends Unit
    {
        public function getName()
        {
            return 'i am cpu';
        }
    }

    /**
    * Memory類
    */
    class Memory extends Unit
    {
        public function getName()
        {
            return 'i am memory';
        }
    }

    /**
    * Keyboard類
    */
    class Keyboard extends Unit
    {
        public function getName()
        {
            return 'i am keyboard';
        }
    }
    
    /**
    * Keyboard類
    */
    interface Visitor
    {
        public function visitCpu(Cpu $cpu);
        public function visitMemory(Memory $memory);
        public function visitKeyboard(Keyboard $keyboard);
    }

    /**
    * 
    */
    class PrintVisitor implements Visitor
    {
        public function visitCpu(Cpu $cpu)
        {
            echo "hello, " . $cpu->getName() . "\n";
        }

        public function visitMemory(Memory $memory)
        {
            echo "hello, " . $memory->getName() . "\n";
        }

        public function visitKeyboard(Keyboard $keyboard)
        {
            echo "hello, " . $keyboard->getName() . "\n";
        }
    }

    /**
    * 
    */
    class Computer
    {
        protected $_items = [];

        public function add(Unit $unit)
        {
            $this->_items[] = $unit;
        }
        
        /**
        * 調用各個組件的accept方法
        */
        public function accept(Visitor $visitor)
        {
            foreach ($this->_items as $item) {
                $item->accept($visitor);
            }
        }
    }

    $computer = new Computer();
    $computer->add(new Cpu());
    $computer->add(new Memory());
    $computer->add(new Keyboard());

    $printVisitor = new PrintVisitor();
    $computer->print($printVisitor);

    // 以上的代碼將打印出    
    hello, i am cpu
    hello, i am memory
    hello, i am keyboard

總結

通過上面的改良以後,咱們要擴展就變得很是容易,若是咱們須要新增保存到數據庫的方法,能夠在新增一個實現了Visitor的類,好比SaveVisitor,在該類中來實現保存的方法,等於咱們把該類和該類中的一些操做解藕了出來,而集合了類操做的對象就是訪問者。code

相關文章
相關標籤/搜索