For implementing a Visitor Pattern in Java you can use Overriding or Overloading. Does the choice depends or is it always preferable to choose one of the two? Because I don't see no disadvantages. Because I think the first and third example will always do the job?
Visitor with overriding
public interface Visitor {
public void visitX(X x);
public void visitY(Y y);
}
public class ConcreteVisitor {
public void visitX(X x) { ... }
public void visitY(Y y) { ... }
}
public abstract class XY {
public abstract void accept(Visitor v);
}
public class X extends XY {
// alternative: one implementation with reflection possible in super class
public void accept(Visitor v) {
v.visitX(this);
}
}
public class Y extends XY {
// alternative: one implementation with reflection possible in super class
public void accept(Visitor v) {
v.visitY(this);
}
}
Visitor with overloading
public interface Visitor {
public void visit(XY xy); // dummy (couldn't otherwise been used in XY class)
public void visit(X x);
public void visit(Y y);
}
public class ConcreteVisitor {
public void visit(XY xy) { ... }
public void visit(X x) { ... }
public void visit(Y y) { ... }
}
public abstract class XY {
public void accept(Visitor v) {
v.visit(this); // Which is the compile-time/static type of this? XY?
}
}
public class X extends XY {
}
public class Y extends XY {
}
Better Visitor with overloading
public interface Visitor {
public void visit(X x);
public void visit(Y y);
}
public class ConcreteVisitor {
public void visit(X x) { ... }
public void visit(Y y) { ... }
}
public abstract class XY {
public abstract void accept(Visitor v);
}
public class X extends XY {
public void accept(Visitor v) {
v.visit(this);
}
}
public class Y extends XY {
public void accept(Visitor v) {
v.visit(this);
}
}
Visitor design pattern is one of the behavioral design patterns. It is used when we have to perform an operation on a group of similar kind of Objects. With the help of visitor pattern, we can move the operational logic from the objects to another class.
The purpose of a Visitor pattern is to define a new operation without introducing the modifications to an existing object structure. Imagine that we have a composite object which consists of components.
It is one way to follow the open/closed principle. In essence, the visitor allows adding new virtual functions to a family of classes, without modifying the classes.
Visitor in C++ Visitor is a behavioral design pattern that allows adding new behaviors to existing class hierarchy without altering any existing code. Read why Visitors can't be simply replaced with method overloading in our article Visitor and Double Dispatch.
The Visitor pattern is in essence about covering an important use case of double dispatch in a single-dispatch language. It leverages the single-dispatch mechanism in a two-step idiom. Therefore if you use reflection for one of those two steps, you are no longer implementing Visitor.
The question of whether or not to use overloading is a minor point because it comes down to nothing more than the choice of name for all the visit
methods. You can use either, but overloading makes more sense in professional code. While studying the pattern, separate names will make it easier to understand the Visitor pattern, which is a bit notorious for its convolutedness.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With