Easy to master the visitor pattern _php instance of PHP design pattern

Source: Internet
Author: User

Problems with visitor pattern resolution

In our code writing process, we often need to add some code to some similar objects, we take a computer object printing components as an example to look at:

  /**
  * Abstract class unit
  {
    /**
    * Get name/abstract public
    function getName ();

  }

  /**
  * CPU Class
  *
  /class CPU extends unit
  {public
    function getName ()
    {return
      ' I am CPU ' ;
    }
  }

  /**
  * Memory class
  *
  /class Memory extends unit
  {public
    function getName ()
    {return
      ' I am Memory ';
    }
  }

  /**
  * Keyboard class
  *
  /class Keyboard extends unit
  {public
    function getName ()
    {return
      ' I am Keyboard ';
    }
  }

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

    Public function Add (unit $unit)
    {
      $this->_items[] = $unit;
    }

    Public function print ()
    {
      ///loop Print each component
      foreach ($this->_items as $item) {
        $item->getname () ;
      }
    }
  }


This time the above code looks like a perfect look, but the problem is, now we need not only to print components, but also need to save the components to the database, not only that, but also need to print the price of each component; If you add the GetPrice () and Save () methods in the unit base class, We can do what we want, but there's a problem with that, you don't know what you need to add, and if every new operation is added in this way, our classes become bloated.

Realization of visitor pattern

And the visitor pattern is to solve this problem, he put the data structure and action on the structure of the coupling between the release, so that the operation set can be relatively free evolution, we look at the following improved code:

  /** * Abstract class Unit {/** * get name/abstract public Function getName (); /** * is used to accept the visitor object, callback the visitor's visit method * Very critical method * * Public Function accept (Visitor $visitor) {$method = Visit.
      Get_class ($this);
      if (Method_exists ($visitor, $method)) {$visitor-> $method ($this); 
    }}/** * CPU class/class CPU extends Unit {public Function getName () {return ' I am CPU '; }/** * Memory class */class Memory extends unit {public Function getName () {return ' I am
    Memory '; }/** * Keyboard class */class keyboard extends unit {public Function getName () {return ' I am K
    Eyboard ';
    }/** * Keyboard class * * 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"; The Public Function visitmemory (Memory $memory) {echo "Hello,". $memory->getname ().
    "\ n"; The Public Function Visitkeyboard (keyboard $keyboard) {echo "Hello,". $keyboard->getname ().
    "\ n";

    }/** * * */class Computer {protected $_items = [];
    Public function Add (unit $unit) {$this->_items[] = $unit; /** * Calls the Accept method of each component/Public function accept (Visitor $visitor) {foreach ($this->_i
      TEMs 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);
 The code above will print hello, I am CPU hello, I am memory Hello, I am keyboard

Summarize

After the above improvement, we want to expand it becomes very easy, if we need to add new methods to save to the database, we can add a visitor class, such as Savevisitor, in the class to implement the Save method, equal to the class and some of the operations in the class are decoupled, The object that sets up the class operation is the visitor.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.