Java single application - Architecture Mode - 03. Design mode - 25. Visitor mode

Keywords: Java

Original address: http://www.work100.net/training/monolithic-architecture-design-patterns-visitor-pattern.html
More tutorials: Beam cloud - free course

Visitor mode

Serial number Chapter in text video
1 Summary -
2 Realization -

Please refer to the navigation above for reading

1. overview

In Visitor Pattern, we use a visitor class, which changes the execution algorithm of 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 pattern, the element object has accepted the accessor object, so that the accessor object can handle operations on the element object.

Intention:

It mainly separates data structure from data operation.

Main solutions:

Stable data structure and changeable operation coupling problem.

When to use:

We need to do many different and unrelated operations on the objects in an object structure, and we need to avoid these operations "contaminating" the classes of these objects, and use the visitor pattern to encapsulate these into the classes.

How to solve:

Add an external interface to the visited class to receive visitors.

Key code:

In the data base class, there is a method that accepts visitors and passes its own references to them.

Application example:

You are a visitor at a friend's house. A friend accepts your visit. You make a judgment on the description of your friend through the description of your friend. This is the visitor mode.

Advantage:

  • In line with the principle of single responsibility.
  • Excellent scalability.
  • Flexibility.

Disadvantages:

  • It is against Dimiter's principle to publish details to visitors.
  • It is difficult to change specific elements.
  • It violates the principle of dependency inversion, relies on concrete classes, and does not rely on abstraction.

Usage scenario:

  • In the object structure, the corresponding classes of objects rarely change, but new operations are often needed to be defined on the object structure.
  • You need to perform many different and unrelated operations on objects in an object structure, and you need to avoid these operations "contaminating" the classes of these objects, and you don't want to modify these classes when adding new operations.

matters needing attention:

Visitors can unify their functions and make reports, UI, interceptors and filters.

2. implementation

We will create a ComputerPart interface that defines the operations to be accepted.

Keyboard, Mouse, Monitor and Computer are entity classes that implement the ComputerPart interface.

We will define another interface, ComputerPartVisitor, which defines the operation of the visitor class.

Computer uses entity accessors to perform corresponding actions.

VisitorPatternDemo, our demonstration class uses the Computer and ComputerPartVisitor classes to demonstrate the usage of visitor pattern.

Step 1

Define an interface that represents an element.

ComputerPart.java, the code is as follows:

public interface ComputerPart {
   void accept(ComputerPartVisitor computerPartVisitor);
}

Step 2

Create entity classes that extend the above classes.

Keyboard.java, the code is as follows:

public class Keyboard  implements ComputerPart {

   @Override
   public void accept(ComputerPartVisitor computerPartVisitor) {
      computerPartVisitor.visit(this);
   }
}

Monitor.java, the code is as follows:

public class Monitor  implements ComputerPart {

   @Override
   public void accept(ComputerPartVisitor computerPartVisitor) {
      computerPartVisitor.visit(this);
   }
}

Mouse.java, the code is as follows:

public class Mouse  implements ComputerPart {

   @Override
   public void accept(ComputerPartVisitor computerPartVisitor) {
      computerPartVisitor.visit(this);
   }
}

Computer.java, the code is as follows:

public class Computer implements ComputerPart {

   ComputerPart[] parts;

   public Computer(){
      parts = new ComputerPart[] {new Mouse(), new Keyboard(), new Monitor()};      
   } 

   @Override
   public void accept(ComputerPartVisitor computerPartVisitor) {
      for (int i = 0; i < parts.length; i++) {
         parts[i].accept(computerPartVisitor);
      }
      computerPartVisitor.visit(this);
   }
}

Step 3

Define an interface that represents the visitor.

ComputerPartVisitor.java, the code is as follows:

public interface ComputerPartVisitor {
   public void visit(Computer computer);
   public void visit(Mouse mouse);
   public void visit(Keyboard keyboard);
   public void visit(Monitor monitor);
}

Step 4

Create an entity visitor that implements the above class.

ComputerPartDisplayVisitor.java, the code is as follows:

public class ComputerPartDisplayVisitor implements ComputerPartVisitor {

   @Override
   public void visit(Computer computer) {
      System.out.println("Displaying Computer.");
   }

   @Override
   public void visit(Mouse mouse) {
      System.out.println("Displaying Mouse.");
   }

   @Override
   public void visit(Keyboard keyboard) {
      System.out.println("Displaying Keyboard.");
   }

   @Override
   public void visit(Monitor monitor) {
      System.out.println("Displaying Monitor.");
   }
}

Step 5

Use ComputerPartDisplayVisitor to display the components of a Computer.

VisitorPatternDemo.java, the code is as follows:

public class VisitorPatternDemo {
   public static void main(String[] args) {

      ComputerPart computer = new Computer();
      computer.accept(new ComputerPartDisplayVisitor());
   }
}

Step 6

Execute the program and output the result:

Displaying Mouse.
Displaying Keyboard.
Displaying Monitor.
Displaying Computer.

Last article: Template mode

If you are interested in the content of the course, you can scan the code to pay attention to our official account or QQ group, and pay attention to our curriculum updates in time.


Posted by mikebr on Fri, 13 Mar 2020 06:43:13 -0700