Visitor Mode (Visitor)

Source: Internet
Author: User

Visitor patterns decouple The data structure from the action on the structure, allowing the set of operations to evolve relatively freely. The visitor pattern is suitable for systems with relatively stable and variable data structures. Because the visitor pattern makes it easier to increase the algorithm operation. If the system data structure object is easy to change, often new data objects are added, it is not appropriate to use the visitor pattern. The advantage of the visitor pattern is that it is easy to increase the operation because increasing the operation means adding new visitors. The visitor pattern centralizes the behavior in a visitor object, and its change does not affect the system data structure. The disadvantage is that it is difficult to add new data structures. --from Encyclopedia

Simply put, the visitor pattern is a way of separating the object's data structure and behavior, which can be achieved by adding new operations to a visitor dynamically without having to make other changes. Simple diagram:

Take a look at the original code: A visitor class that holds the object to be accessed,

[Java]View Plaincopy
    1. Public Interface Visitor {
    2. Public void Visit (Subject sub);
    3. }
[Java]View Plaincopy
    1. Public class Myvisitor implements Visitor {
    2. @Override
    3. Public void Visit (Subject sub) {
    4. SYSTEM.OUT.PRINTLN ("Visit the Subject:" +sub.getsubject ());
    5. }
    6. }

The subject class, accept method, accepts the object that will access it, Getsubject () Gets the property that will be accessed,

[Java]View Plaincopy
    1. Public Interface Subject {
    2. Public void Accept (Visitor Visitor);
    3. Public String Getsubject ();
    4. }
[Java]View Plaincopy
    1. Public class Mysubject implements Subject {
    2. @Override
    3. Public void Accept (Visitor Visitor) {
    4. Visitor.visit (this);
    5. }
    6. @Override
    7. Public String Getsubject () {
    8. return "Love";
    9. }
    10. }

Test:

[Java]View Plaincopy
    1. Public class Test {
    2. Public Static void Main (string[] args) {
    3. Visitor Visitor = new myvisitor ();
    4. Subject sub = new mysubject ();
    5. Sub.accept (visitor);
    6. }
    7. }

Output: Visit the Subject:love

This pattern applies to scenarios: if we want to add new functionality to an existing class, there are a few things to consider: 1. Does the new feature have compatibility issues with existing features? 2. Will I need to add it again? 3. What if the class does not allow the code to be modified? In the face of these problems, the best solution is to use the visitor pattern, the visitor pattern for the relatively stable structure of the system, the data structure and algorithm decoupling,

Visitor Mode (Visitor)

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.