문제

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);
  }
}
도움이 되었습니까?

해결책

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.

다른 팁

This is true of any behavior, not just Visitor. You can choose either one, of course. Both work.

Sounds like a rehash of the "interface or abstract class?" question. You prefer abstract when there can be meaningful, non-abstract default behavior. Interfaces have no default, so every user is forced to implement the method. No assumptions are possible.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top