[one java design pattern per day] - Visitor pattern

In the visitor pattern, we use a visitor class, which changes the execution algorithm of the element class. In this way, the execution algorithm of the element can change as the visitor changes. This type of design pattern belongs to behavioral pattern. According to the schema, the element object has accepted the visitor object so that the visitor object can handle operations on the element object.

The visitor pattern contains the following main roles.

  • Abstract visitor role: define an interface for accessing concrete elements. Each concrete element class corresponds to an access operation visit(). The parameter type in the operation identifies the accessed concrete elements.
  • Specific visitor role: implement each access operation declared in the abstract visitor role and determine what visitors should do when accessing an element.
  • Abstract element role: declare an interface containing the accept operation accept(), and the accepted visitor object is used as the parameter of the accept() method.
  • Concrete element role: implement the accept() operation provided by the abstract element role, and its method body is usually visitor Visit (this). In addition, the specific elements may also contain relevant operations of their own business logic.
  • Object structure role: it is a container containing element roles and provides methods for visitor objects to traverse all elements in the container. It is usually implemented by aggregate classes such as List, Set and Map.

Implementation of visitor mode:

1. Create visitor interface

public interface Visitor {
    void visit(Element element);
}

2. Create specific visitors

public class AVisitor implements Visitor {
    @Override
    public void visit(Element element) {
        System.out.println(this.getClass().getName() + "Visited" + element.getClass().getName());
    }
}
public class BVisitor implements Visitor {
    @Override
    public void visit(Element element) {
        System.out.println(this.getClass().getName() + "Visited" + element.getClass().getName());
    }
}

3. Create element interface

public interface Element {
    void accept(Visitor visitor);
}

4. Create specific elements

public class AElement implements Element {
    @Override
    public void accept(Visitor visitor) {
        visitor.visit(this);
    }
}
public class BElement implements Element {
    @Override
    public void accept(Visitor visitor) {
        visitor.visit(this);
    }
}

5. Create object structure class

public class Structure {
    private List<Element> elements = new ArrayList<>();

    public void add(Element element){
        elements.add(element);
    }

    public void remove(Element element){
        elements.remove(element);
    }

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

6. Visitor mode test

public class VisitorMain {
    public static void main(String[] args) {
        Structure structure = new Structure();
        structure.add(new AElement());
        structure.add(new BElement());

        Visitor visitor_A = new AVisitor();
        structure.accept(visitor_A);

        Visitor visitor_B = new BVisitor();
        structure.accept(visitor_B);
    }
}

Create pattern

Structural model

Behavioral model

Keywords: Java Design Pattern

Added by JoeCrane on Sat, 25 Dec 2021 11:35:37 +0200