Visitor mode

1.1 visitors

1.1.1 definition

The operations that act on each element in a data structure are separated and encapsulated into independent classes, so that they can add new operations that act on these elements without changing the data structure, and provide a variety of access methods for each element in the data structure. It separates the operation of data from the data structure. It is the most complex pattern in the behavior class pattern. The key to the implementation of visitor pattern is how to separate the operation acting on elements and package them into independent classes.

1.1.2 roles

  • UML
  • Abstract Visitor is mainly for Visitor extension. It defines a series of Visitor methods. This method can be distinguished by method name or overloaded by parameters. It is used to specify different operation logics corresponding to different element objects. Generally, a Visitor method is provided for an element.
  • Concrete visitor implements Abstract access logic.
  • The abstract Element element defines an accept method, and the parameter is usually the abstract visitor.
  • The concrete element, ConcreteElement, implements accept to complete the operation you want to do when accessing an element. The essence is to call the passed method of the abstract visitor, that is, the visit method of the visitor. This mechanism is also known as double dispatch. In this way, using the characteristics of parameter overloading, we can pass and call the added visitor in this way without modifying any code (calling the corresponding method through parameter overloading)
  • The object structure ObjectStructure is used to store the of element objects and provide the method of traversing internal elements, which can be realized by using composite mode. It can also be a simple collection object. An object structure encapsulates the structure of the set, and then the specific elements are extracted to form a system. Visitors are another system. In the visitor system, every visitor should implement a visit method for the required operation. This method will pass itself to the specific element object in the future, and then call it through the element object.

1.1.3 cases

The visitor mode is used to realize the scene of a user accessing a blog. Users can access a blog through the Web mode on the computer (Visitor) or mobile APP mode (Visitor). Each blog is an element, and then the blog list is an object structure class.

1.1.4 code

public class Demo {
    public static void main(String[] args) {
        Blog blog = new Blog();
        blog.addBlog(new BlogElement("First blog post"));
        blog.addBlog(new BlogElement("Second blog post"));
        blog.addBlog(new BlogElement("The third blog post"));
        blog.addBlog(new BlogElement("The fourth blog post"));

        Visitor webVisit = new WebVisitor();
        Visitor appVisit = new AppVisitor();

        blog.accept(webVisit);
        blog.accept(appVisit);
    }
}

/**
 * Define abstract visitors
 */
abstract class Visitor {
    public abstract void visitBlog(Element element);
}

/**
 * Define concrete visitors, including web and app
 */
class WebVisitor extends Visitor {
    public void visitBlog(Element element) {
        System.out.println("Through computer web Website access Blog:" + element.blogName);
    }
}

/**
 * Define concrete visitors, including web and app
 */
class AppVisitor extends Visitor {
    public void visitBlog(Element element) {
        System.out.println("Via mobile phone App Website access Blog:" + element.blogName);
    }
}

/**
 * Define abstract elements
 */
abstract class Element {
    public String blogName;

    abstract public void accept(Visitor visitor);
}

/**
 * Define a concrete element, that is, a blog
 */
class BlogElement extends Element {
    public BlogElement(String blogName) {
        this.blogName = blogName;
    }

    public void accept(Visitor visitor) {
        visitor.visitBlog(this);
    }
}

/**
 * Define the object structure class (ObjectStructure), that is, the blog list
 */
class Blog {
    private List<Element> blogList = new ArrayList<>();

    public void addBlog(Element element) {
        blogList.add(element);
    }

    public void removeBlog(Element element) {
        blogList.remove(element);
    }

    public void accept(Visitor visitor) {
        for (Element element : blogList) {
            element.accept(visitor);
        }
    }
}

1.1.5 features

  • It has good expansibility and can add new functions to the elements in the object structure without modifying the elements in the object structure. Good reusability. Visitors can define the general functions of the whole object structure, so as to improve the reusability of the system. The visitor mode decouples the data structure from the operation on the structure, so that the operation set can evolve relatively freely without affecting the data structure of the system. In line with the principle of single responsibility, the visitor mode encapsulates the relevant behaviors to form a visitor, so that the function of each visitor is relatively single.
  • It is difficult to add new element classes. In the visitor mode, each new element class is added with corresponding specific operations in each specific visitor class, which violates the opening and closing principle. The encapsulation is broken. The specific elements in the visitor pattern publish details to visitors, which destroys the encapsulation of objects and violates the dependency inversion principle. The visitor pattern relies on concrete classes rather than abstract classes.

1.1.6 application scenarios

  • Object structure is relatively stable, but its operation algorithm often changes.
  • Objects in the object structure need to provide a variety of different and irrelevant operations, and the changes of these operations should not affect the object structure.
  • The object structure contains many types of objects. You want to implement some operations on these objects that depend on their specific types.

Tags: element

Posted by madcat on Mon, 18 Apr 2022 08:03:40 +0930