26. behavior-based design model-Visitor pattern (visitor Mode)

Source: Internet
Author: User
ArticleDirectory
    • Definition
    • Instance 1 -- personnel evaluation
    • Strengths and weaknesses
    • Application scenarios
    • Definition

Indicates that an operation is executed in elements of an object structure. The visitor style allows you to define new operations for a class without changing the member classes it operates on.

The UML class diagram is as follows:

The relationship between classes and objects is:

1. Visitor (Abstract visitor): declares a visit operation for each concreteelement class in the object structure class. The operation name and sign (Signature) Identify the class from which the visit request is sent to the visitor. This allows the visitor to determine the specific class of the element to be accessed, so that the visitor can directly access the element through its special interface ).

2. concretevisitor (visitor): implements each operation declared by the visitor. Implementation of each operationAlgorithmThe algorithm fragment corresponds to the class of objects in the structure. Concretevisitor provides a scenario for the algorithm and stores its local state. This state often accumulates results while traversing the structure.

3. element: defines an accept operation, which takes a visitor as the parameter.

4. concreteelement (Abstract specific element): implements the accept operation, which takes a visitor as the parameter.

5. objectstructure (object structure class): it can enumerate its elements; it can provide a high-level interface to allow visitors to access its elements; it can be a composite pattern or a set, such as a list or an unordered set.

The following figure shows the order of typical applications:

    • Instance 1 -- personnel evaluation

The personnel evaluation of the company requires the personnel department to access a list of employees and evaluate employees one by one. The Personnel Department has two visitors. One visitor evaluates the employee's vacation and the other visitor evaluates the employee's salary. The class diagram is as follows:

View code

 Namespace Consoleapplication
{
// Abstract visitor
Abstract Class Visitor
{
// Access method, with members as parameters
Abstract Public Void Visit (element );
}
// Specific visitor-salary visitor
Class Incomevisitor: visitor
{
Public Override Void Visit (element)
{
Employee Employee = (employee) element );
Employee. Income * = 1.10 ;
Console. writeline ( " {0} new salary: {1: c} " , Employee. Name, employee. income );
}
}
// Visitor-holiday visitor
Class Vacationvisitor: visitor
{
Public Override Void Visit (element)
{
Employee Employee = (employee) element );
Employee. vacationdays + = 3 ;
Console. writeline ( " {0} new holiday days: {1} " , Employee. Name, employee. vacationdays );
}
}
// Abstract member class
Abstract Class Element
{
// Method of accepting visitors
Abstract Public Void Accept (visitor );
}
// Specific Members-employees
Class Employee: Element
{
String Name;
Double Income;
Int Vacationdays;
Public Employee ( String Name, Double Income, Int Vacationdays)
{
This . Name = Name;
This . Income = income;
This . Vacationdays = vacationdays;
}
Public String Name
{
Get { Return Name ;}
Set {Name = value ;}
}
Public Double Income
{
Get { Return Income ;}
Set {Income = value ;}
}
Public Int Vacationdays
{
Get { Return Vacationdays ;}
Set {Vacationdays = value ;}
}
// Reload the method of accepting visitor objects
Public Override Void Accept (visitor)
{
Visitor. Visit ( This );
}
}
// Employee Aggregation
Class Employees
{
Private Arraylist employees = New Arraylist ();
// Add employees
Public Void Attach (employee Employee)
{
Employees. Add (employee );
}
// Delete employees in aggregation
Public Void Detach (employee Employee)
{
Employees. Remove (employee );
}
// All aggregated employees accept visitors
Public Void Accept (visitor)
{
Foreach (Employee e In Employees)
E. Accept (visitor );
}
}
Class Program
{
Static Void Main ( String [] ARGs)
{
Employees E =New Employees ();
E. Attach ( New Employee ( " Zhang San " , 20000.0 , 14 ));
E. Attach ( New Employee ( " Li Si " , 30000.0 , 16 ));
E. Attach ( New Employee ( " Wang Wu " , 40000.0 , 18 ));
// Generate two visitor objects
Incomevisitor V1 = New Incomevisitor ();
Vacationvisitor v2 = New Vacationvisitor ();
// Employees in aggregation accept two visitor objects
E. Accept (V1 );
E. Accept (V2 );
Console. Read ();
}
}
}
    • Strengths and weaknesses

The visitor (visitor) mode makes it easy to add new operations. It can collect associated methods, while separating unrelated methods is especially suitable for separating things that change for different reasons, for example, "separating a boy from a man ". However, the visitor mode often breaks the encapsulation of objects. The visitor and element need to reach some consensus.

    • Application scenarios

The following scenario applies the visitor mode:

1. The structure of an object contains multiple objects with different interfaces, which must be processed differently based on specific objects.

2. There are many different and unrelated processes for objects in the structure. Therefore, avoid separating classes by operations.

3. The object structure defined in the class is rarely changed, but you need to often define new operations to process the structure.

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.