Visitor pattern (Visitor Pattern)

In the visitor pattern (Visitor Pattern), we use a visitor class, it changes the algorithm execution element classes. In this way, the algorithm elements can change as the visitor change. This type of design pattern belongs behavioral patterns. Depending on the mode, the subject has received a visitor element objects, such objects can visitors processing operations on the element object. Introduction is intended: the main operation of the data structure and data separation. Mainly to solve: a stable and variable data structure operatively coupled problems. When to use: the need for a target structure objects and many different unrelated operation ..


In the visitor pattern (Visitor Pattern), we use a visitor class, it changes the algorithm execution element classes. In this way, the algorithm elements can change as the visitor change. This type of design pattern belongs behavioral patterns. Depending on the mode, the subject has received a visitor element objects, such objects can visitors processing operations on the element object.

Introduction

Intent: separating the main data structures and data manipulation.

Mainly to solve: stable data structures and variable operating coupling problem.

When to use: the need for a lot of different and unrelated to the operation of the structure of an object in an object, and to make these operations need to avoid "contamination" of these classes of objects, these packages use of visitors to the class.

How to fix: add a reception of foreign visitors to provide interfaces to be accessed inside the class.

The key code: the data base class which has a method to accept visitors, the self-reference incoming visitors.

Application examples: You friend's house guest, you are a visitor, a friend accepts your access, through your friend's description, then the description of a friend make a judgment, which is the visitor pattern.

Advantages: 1, in line with the principle of single responsibility. 2, excellent scalability. 3, flexibility.

Disadvantages: 1, the specific elements of the visitor announced details of a violation of the principle of Demeter. 2, the specific elements of change more difficult. 3, in violation of the Dependency Inversion principle, rely on a specific class that does not rely on abstraction.

Be used: 1, the object corresponding to the object class structure rarely changes, but often need to define a new operation on the object structure. 2, the need for the structure of a target object in a lot of different and unrelated operations, and these operations need to avoid "contamination" class of these objects, we do not want to modify these classes when adding new operations.

Note: Visitors can be unified function, you can make a report, UI, interceptors and filters.

 

Visitors mode:
  indicates the operation acting on an object structure of the individual elements. It allows you to define the premise without changing the respective elements based on the effect of the operation of these elements.

Role:
  1) abstract visitors: a user interface for accessing specific elements of the role of object structure declaration. The name and operational parameters of the interface identifies specific elements of role access request is sent to a specific visitor, so that visitors can access it directly through a specific interface to the role of the elements.
  2) Specific Visitors: Visitors implement the interface declaration.
  3) abstract elements: accepting a definition of access operation accept (), which as a parameter to a visitor.
  4) Specific elements: accepting an operation implements the interface defined by the abstract elements.
  5) structural objects: This is an essential role to use the visitor pattern. It has the following characteristics: its elements can be enumerated; may provide a high-level interface to allow visitors to its elements; if necessary, can be designed as a composite object or an aggregate (e.g., a list or set of unordered)

UML 类图:

  clip_image023

Advantages and application scenarios:
  1) a class of object structure contains many objects, they have different interfaces, and you want to implement some operations depend on its specific category for these objects.
  2) the need for a lot of different and unrelated to the operation of an object structure of objects, and you want to avoid these operations, "pollution" of these objects class. Visitor pattern such that your related operations may be defined together in a class.
  3) when the object structure is shared by many applications, with Visitor allowing each application contains only need to use the operation.
  4) the class definition of the object structure rarely changes, but often need to define a new operation on this structure. Changes in the structure of the object class needs to redefine the interface for all visitors, which may require a high price. If the object class structure change frequently, it may still define these operations is preferably in those classes.

 

Code:

Copy the code
? <PHP 
header ( "Content-of the type: text / HTML; Charset = UTF-8"); 

// Visitors abstract interface that defines methods to achieve specific visitors to access different elements call different methods 
abstract class Visitor { 
    abstract function eAT ($ Place); 
    abstract function Work ($ Place); 
} 
// specific role of visitors 
class ConcreteVisitor the extends Visitor { 
     // home for dinner 
    function eAT ($ Place) { 
        .. echo "return" $ place-> name " eating "; 
    } 
     // company to work 
    function work ($ Place) { 
        echo 'to work," "$ Place-> name.."; 
    } 
} 


// abstract elements, define a receiving access operation AcceptVisitor (), to which a the visitor as an argument. 
Place {class abstract 
    abstract function AcceptVisitor (Visitor $ hit);

// home, visitors go home for dinner 
class Homn Place the extends { 
    public $ name; 

    function __construct ($ name) { 
        $ this-> name = $ name; 
    } 

    function AcceptVisitor ($ Visitor visitor) { 
         $ visitor-> EAT ($ the this); 
    } 
} 

// company, the company work visitor 
class Place the extends company { 
    public $ name; 

    function the __construct ($ name) { 
        $ this-> name = $ name; 
    } 

    function AcceptVisitor (visitor hit $) { 
         $ hit -> Work ($ the this); 
    } 
} 

// tree structure of the object, i.e. the set of elements 
class ObjectStruction { 
    Private Places $ = Array (); 

    // add elements
    function addPlace (Place Place $) {
        this- $> Places [] = $ Place; 
    } 
    // remove elements 
    function removePlace (Place Place $) { 
        $ Key = the array_search (Place $, $ this-> Places); 
        IF (! == $ Key to false) { 
            the unset ($ this-> Places [Key $]); 
        } 
    } 
    // set all the elements receiving element access 
    function allAccept (Visitor hit $) {   
        the foreach ($ this-> Places Place AS $)   
        {   
            $ Place-> AcceptVisitor ($ hit );   
        }   
    }   
} 

// test 
$ objectStruction = new new objectStruction (); 
$ home = new new Homn ( "home"); 
$objectStruction->addPlace($home);
$ company = new new company ( "company"); 
$ objectStruction-> addPlace ($ company);

$visitor = new ConcreteVisitor();
$objectStruction->allAccept($visitor);

?>
Copy the code

 

Visitors mode:
  indicates the operation acting on an object structure of the individual elements. It allows you to define the premise without changing the respective elements based on the effect of the operation of these elements.

Role:
  1) abstract visitors: a user interface for accessing specific elements of the role of object structure declaration. The name and operational parameters of the interface identifies specific elements of role access request is sent to a specific visitor, so that visitors can access it directly through a specific interface to the role of the elements.
  2) Specific Visitors: Visitors implement the interface declaration.
  3) abstract elements: accepting a definition of access operation accept (), which as a parameter to a visitor.
  4) Specific elements: accepting an operation implements the interface defined by the abstract elements.
  5) structural objects: This is an essential role to use the visitor pattern. It has the following characteristics: its elements can be enumerated; may provide a high-level interface to allow visitors to its elements; if necessary, can be designed as a composite object or an aggregate (e.g., a list or set of unordered)

UML 类图:

  clip_image023

Advantages and application scenarios:
  1) a class of object structure contains many objects, they have different interfaces, and you want to implement some operations depend on its specific category for these objects.
  2) the need for a lot of different and unrelated to the operation of an object structure of objects, and you want to avoid these operations, "pollution" of these objects class. Visitor pattern such that your related operations may be defined together in a class.
  3) when the object structure is shared by many applications, with Visitor allowing each application contains only need to use the operation.
  4) the class definition of the object structure rarely changes, but often need to define a new operation on this structure. Changes in the structure of the object class needs to redefine the interface for all visitors, which may require a high price. If the object class structure change frequently, it may still define these operations is preferably in those classes.

 

Code:

Copy the code
? <PHP 
header ( "Content-of the type: text / HTML; Charset = UTF-8"); 

// Visitors abstract interface that defines methods to achieve specific visitors to access different elements call different methods 
abstract class Visitor { 
    abstract function eAT ($ Place); 
    abstract function Work ($ Place); 
} 
// specific role of visitors 
class ConcreteVisitor the extends Visitor { 
     // home for dinner 
    function eAT ($ Place) { 
        .. echo "return" $ place-> name " eating "; 
    } 
     // company to work 
    function work ($ Place) { 
        echo 'to work," "$ Place-> name.."; 
    } 
} 


// abstract elements, define a receiving access operation AcceptVisitor (), to which a the visitor as an argument. 
Place {class abstract 
    abstract function AcceptVisitor (Visitor $ hit);

// home, visitors go home for dinner 
    // add elements
Place the extends Homn {class 
    public $ name; 

    function the __construct ($ name) { 
        $ this-> name = $ name; 
    } 

    function AcceptVisitor (Visitor hit $) { 
         $ visitor-> EAT ($ the this); 
    } 
} 

// company, visitors to the company work 
class Place the extends company { 
    public $ name; 

    function the __construct ($ name) { 
        $ this-> name = $ name; 
    } 

    function AcceptVisitor (visitor hit $) { 
         $ visitor-> work ($ the this); 
    } 
} 

// the object tree structure, i.e. the set of elements 
class ObjectStruction { 
    Private Places $ = Array (); 

    function addPlace (Place Place $) {
        this- $> Places [] = $ Place; 
    } 
    // remove elements 
    function removePlace (Place Place $) { 
        $ Key = the array_search (Place $, $ this-> Places); 
        IF (! == $ Key to false) { 
            the unset ($ this-> Places [Key $]); 
        } 
    } 
    // set all the elements receiving element access 
    function allAccept (Visitor hit $) {   
        the foreach ($ this-> Places Place AS $)   
        {   
            $ Place-> AcceptVisitor ($ hit );   
        }   
    }   
} 

// test 
$ objectStruction = new new objectStruction (); 
$ home = new new Homn ( "home"); 
$objectStruction->addPlace($home);
$ company = new new company ( "company"); 
$ objectStruction-> addPlace ($ company);

$visitor = new ConcreteVisitor();
$objectStruction->allAccept($visitor);

?>
Copy the code

 

Guess you like

Origin www.cnblogs.com/benbenhan/p/12396457.html