Double Dispatch
What is dual distribution?
When talking about object-oriented programming, we often talk about object-oriented "polymorphism". Among them, there is often a saying about polymorphism that "the parent class reference points to the child class object."
This parent class reference points to subclass objects is written like the following:
Animal animal = new Dog(); animal.bark();
Another commonly used form is
public class Keeper { public void says(Animal a) { System.out.println("Animal says"); } public void says(Dog dog) { System.out.println("dog says"); } } Animal animal = new Animal(); Animal dog = new Dog(); Keeper keeper = new Keeper(); keeper.say(animal); keep.say(dog); What content will be output when the keeper above calls say twice? Will two different methods be called?
In fact, during these two calls, the method says(Animal a) will be called. Since these contents can be found in the compilation period, this is the static distribution of Java.
From the figure above, we can see that the bytecode generated by the two calls does point to the say(Animal a) method. The method is directly executed at runtime and the corresponding content is output.
Why does the corresponding animal.bark() end up calling the method of the dog class? This is to determine the type of the specific method recipient at runtime and execute it. This is called dynamic distribution, which determines specific methods at runtime and implements object-oriented polymorphism.
Dispatch
Distribution refers to the process of finalizing a method to be executed.
For static languages such as Java, they are all executed through single distribution (Single Dispatch).
For example, a line of code
dog.eat(new Bone())
The final execution of the eat method to be selected will only select the corresponding method according to the specific type of the dog, and the passed parameters cannot affect the selection of the corresponding method. This is single Dispatch
In order to make the real parameters passed in, Bone is here to really play a role, you need to use Double Dispatch or Multiple Dispatch
That is to say, the final decision is not only the receiver of the method, but also the parameter type.
Visitor mode
In GoF's design mode, Visitor mode uses Double Dispatch to achieve the purpose of calling real objects.
For Visitor mode, the most common example is tree traversal. For example, there are differences in the way to process nodes and leaves. This is done through the dual distribution of visitor to implement different Elements and execute different contents.
The code looks like this:
node.accept(new ConcreateVisitor()); leaf.accept(new ConcreateVisitor());
The accept method in node will pass its real type back to the visitor again
public void accept(Visitor v) { v.visit(this); }At this time, in the visitor, you can call specific methods according to the real type, and there are methods similar to those corresponding to node and leaf:
public void visit(Node n); public void visit(Leaf l);
Visitor To sum up, it generally includes the visitor interface. In the visitor interface, it contains the processing logic of each Element object to be accessed. In the specific implementation of each Element, you pass your own type back to the visitor for secondary distribution to implement the exact logic call.
Applications in Tomcat
Visitor is also used in Tomcat, typically parsing EL expressions.
For example org.apache.el.parser.Node
This class contains an accept(NodeVisitor visitor) method
There are many actual Node types, but at this time of real call, it will pass
public void accept(NodeVisitor visitor) throws Exception { visitor.visit(this);Passing the real type back to the visitor, a specific method will be called in the visor, so that the parameters can also play a decisive role.
public void visit(Node node) throws ELException { if (node instanceof AstFunction) { AstFunction funcNode = (AstFunction) node; Method m = null; } else if (xxx) { } Here, multiple visit methods will be declared, and the above visit(this) will be directly located on the target method.
The above are the various distributions in Java and the visitor model to achieve dual distribution through the form of patterns. I hope it will be helpful to everyone. If you have any questions, please leave me a message. The editor will reply to everyone in time!