Statement: This series of blog reference "Big Talk design mode", author Geoscience.
The visitor pattern represents an operation that acts on elements in an object's structure. It allows you to define new actions that act on these elements without changing the individual element classes.
UML Class Diagrams:
Role:
1. Abstract Visitor (state): declares an access operation interface for the specific element role in the object structure. The name and parameters of the operation interface identify the specific element role that sends the access request to the specific visitor, so that the visitor can access it directly through the specific interface of the element role.
2. Specific visitor (Success): The interface that implements the visitor declaration.
3. Abstract element (person): defines an accept access operation Accept (), which takes a visitor as a parameter.
4. Concrete element (man): implements an interface for accepting operations defined by an abstract element.
5. Structure Object (objectstruct): This is the role that is necessary to use the visitor pattern. It has the following characteristics: an element that can enumerate it, a high-level interface to allow visitors to access its elements, or, if necessary, a composite object or a aggregation (such as a list or unordered collection).
Core code:
<span style= "color: #000000;" ><?php/** * Created by Phpstorm. * User:jang * DATE:2015/6/11 * Tim:9:40 *//* men The content of the book is more appealing than the cover; the cover of a woman's book is usually more attractive than the content of a man. When a woman succeeds, there is a great woman behind it; There is a failure of the man behind most of the men who failed to drink, who also do not need to persuade, the woman failed, tears, who can not persuade a man in love, all things do not understand also to be installed to understand; When a woman is in love, she should understand and pretend not to understand *///abstract class state{ protected $state _name; Get the men to react public abstract function getmanaction (VMan $elementM); Get a woman to react public abstract function getwomanaction (Vwoman $elementW);} Abstract Person class person{public $type _name; public abstract function Accept (state $visitor);} Success Status Class Success extends state{public function __construct () {$this->state_name= "success"; } Public Function Getmanaction (VMan $elementM) {echo "{$elementM->type_name}:{$this->state_name}, there are many behind Half had a great woman. <br/> "; Public Function getwomanaction (Vwoman $elementW) {echo ' {$elementW->type_name}: {$this->state_name} , most of them have an unsuccessful man behind them. <br/> "; }}//failure Status Class Failure extends state{Public Function __construct () {$this->state_name= "failed"; } Public Function Getmanaction (VMan $elementM) {echo "{$elementM->type_name}:{$this->state_name}, Wine, no one should persuade. <br/> "; Public Function getwomanaction (Vwoman $elementW) {echo ' {$elementW->type_name}: {$this->state_name} When, tears, who also can not persuade. <br/> "; }}//Love Status class Amativeness extends state{public function __construct () {$this->state_name= "love"; } Public Function Getmanaction (VMan $elementM) {echo "{$elementM->type_name}:{$this->state_name}, everything is not Understand also need to install understand. <br/> "; Public Function getwomanaction (Vwoman $elementW) {echo ' {$elementW->type_name}: {$this->state_name} , you should pretend not to understand. <br/> "; }}//man class VMan extends person{function __construct () {$this->type_name= "man"; The Public Function Accept (state $visitor) {$visitor->getmanaction ($this); }}//Woman class Vwoman extends person{public function __construct () {$this->type_name= "woman"; The Public Function Accept (state $visitor) {$visitor->getwomanaction ($this); }}//object structure Class objectstruct{private $elements =array (); Add Public function Add (person $element) {Array_push ($this->elements, $element); }//Remove public function remove (person $element) {foreach ($this->elements as $k + = $v) { if ($v = = $element) {unset ($this->elements[$k]); }}}//view show Public function display (state $visitor) {foreach ($this->elements as $v) {$v->accept ($visitor); }}}</span>
To test the client code:
Header ("Content-type:text/html;charset=utf-8");//------------------------visitor mode--------------------require_once "./visitor/visitor.php"; $os = new Objectstruct (); $os->add (New VMan ()); $os->add (New Vwoman ());//Success Response $SS = new Success (); $os->display ($SS);//failure Response $fs = new Failure (); $os->display ($FS);//Love Reaction $ats=new amativeness (); $os- >display ($ats);
application Scenarios and advantages:
1) An object structure contains many classes of objects that have different interfaces, and you want to implement some operations that depend on their specific classes for those objects.
2) you need to do a lot of different and unrelated operations on objects in an object structure, and you want to avoid classes that let these actions "pollute" those objects. the Visitor mode allows you to centralize related operations into a class.
3) When the object structure is shared by many applications, use Visitor mode to have each app contain only the actions that need to be used.
4) classes that define the structure of an object rarely change, but it is often necessary to define new operations on this structure. Changing the object structure class requires redefining the interface to all visitors, which can take a significant cost. If the object structure classes often change, it might be better to define them in these classes.
Welcome to follow my video course, the address is as follows, thank you.
PHP Object-oriented design pattern
PHP design mode-visitor mode