简化的访客模式
Simplified visitor pattern
这是在 Java 中实现的访问者模式,用于计算 (1 + 2) + 3 之类的表达式。此处的代码受以下代码示例的启发:https://en.wikipedia.org/wiki/Visitor_pattern#Sources.
interface Node
{
public int accept(Visitor v);
}
class ConstantNode implements Node
{
public int constant;
public ConstantNode(int constant)
{
this.constant = constant;
}
public int accept(Visitor v) {
return v.visit(this);
}
}
class SumNode implements Node
{
public Node left;
public Node right;
public SumNode(Node left, Node right)
{
this.left = left;
this.right = right;
}
public int accept(Visitor v) {
return v.visit(this);
}
}
interface Visitor
{
public int visit(ConstantNode n);
public int visit(SumNode n);
}
class EvalVisitor implements Visitor
{
public int visit(ConstantNode n) {
return n.constant;
}
public int visit(SumNode n) {
return n.left.accept(this) + n.right.accept(this);
}
}
public class VisitorDemo
{
public static void main(String[] args)
{
// First make an expression tree to represent the following.
//
// +
// / \
// + 3
// / \
// 1 2
Node a = new ConstantNode(1);
Node b = new ConstantNode(2);
Node c = new ConstantNode(3);
Node d = new SumNode(a, b);
Node e = new SumNode(d, c);
Visitor visitor = new EvalVisitor();
int result = e.accept(visitor);
System.out.println(result);
}
}
我知道在每个递归级别,调用哪个 visit()
方法取决于访问者的类型(在本例中 evalVisitor
)以及节点的类型( ConstantNode
或 SumNode
),因此需要双重调度。但是这种使用 accept()
和 visit()
方法实现双重调度的编码对我来说似乎太复杂了。但是我见过的几乎所有访问者模式的例子都使用这种方法,通过 accept()
将访问者传递给节点,然后调用访问者的 visit()
方法来执行双重调度。
为什么代码示例不能像这样更简单?
interface Node
{
}
class ConstantNode implements Node
{
public int constant;
public ConstantNode(int constant)
{
this.constant = constant;
}
}
class SumNode implements Node
{
public Node left;
public Node right;
public SumNode(Node left, Node right)
{
this.left = left;
this.right = right;
}
}
interface Visitor
{
public int visit(Node n) throws Exception;
}
class EvalVisitor implements Visitor
{
public int visit(Node n) throws Exception {
if (n instanceof ConstantNode) {
return ((ConstantNode) n).constant;
} else if (n instanceof SumNode) {
return this.visit(((SumNode) n).left) + this.visit(((SumNode) n).right);
} else {
throw new Exception("Unsupported node");
}
}
}
public class SimpleVisitorDemo
{
public static void main(String[] args) throws Exception
{
// First make an expression tree to represent the following.
//
// +
// / \
// + 3
// / \
// 1 2
Node a = new ConstantNode(1);
Node b = new ConstantNode(2);
Node c = new ConstantNode(3);
Node d = new SumNode(a, b);
Node e = new SumNode(d, c);
Visitor visitor = new EvalVisitor();
int result = visitor.visit(e);
System.out.println(result);
}
}
在此代码示例中,我完全消除了在每个节点中实现 apply()
的需要,并且包括双重调度逻辑在内的全部访问逻辑现在仅包含在访问者 class 中.
我有以下问题:
能否客观地列举一下简化访客模式在代码的可维护性或效率方面存在的问题?
Why can't the code examples be simpler, [...]
因为您的示例将虚拟分派替换为 switch
分派(您将其实现为对象子类型上的 if
链)。这种方法极难维护,因为编译器无法帮助您检测继承层次结构的变化。
简化实施的具体问题在于最后一个 else
,其中您 return 为零。一个更常见的解决方案是在那里抛出异常,因为你真的不知道你有什么样的节点。
现在想象一下用 SubtractNode
扩展层次结构。这自然需要向 Visitor
接口添加一个方法,确保所有访问者在编译时都被迫处理新的节点子类型。
另一方面,简化示例会继续编译,在您的情况下,它还会继续运行,returnSubtractNode
的错误结果。
这是在 Java 中实现的访问者模式,用于计算 (1 + 2) + 3 之类的表达式。此处的代码受以下代码示例的启发:https://en.wikipedia.org/wiki/Visitor_pattern#Sources.
interface Node
{
public int accept(Visitor v);
}
class ConstantNode implements Node
{
public int constant;
public ConstantNode(int constant)
{
this.constant = constant;
}
public int accept(Visitor v) {
return v.visit(this);
}
}
class SumNode implements Node
{
public Node left;
public Node right;
public SumNode(Node left, Node right)
{
this.left = left;
this.right = right;
}
public int accept(Visitor v) {
return v.visit(this);
}
}
interface Visitor
{
public int visit(ConstantNode n);
public int visit(SumNode n);
}
class EvalVisitor implements Visitor
{
public int visit(ConstantNode n) {
return n.constant;
}
public int visit(SumNode n) {
return n.left.accept(this) + n.right.accept(this);
}
}
public class VisitorDemo
{
public static void main(String[] args)
{
// First make an expression tree to represent the following.
//
// +
// / \
// + 3
// / \
// 1 2
Node a = new ConstantNode(1);
Node b = new ConstantNode(2);
Node c = new ConstantNode(3);
Node d = new SumNode(a, b);
Node e = new SumNode(d, c);
Visitor visitor = new EvalVisitor();
int result = e.accept(visitor);
System.out.println(result);
}
}
我知道在每个递归级别,调用哪个 visit()
方法取决于访问者的类型(在本例中 evalVisitor
)以及节点的类型( ConstantNode
或 SumNode
),因此需要双重调度。但是这种使用 accept()
和 visit()
方法实现双重调度的编码对我来说似乎太复杂了。但是我见过的几乎所有访问者模式的例子都使用这种方法,通过 accept()
将访问者传递给节点,然后调用访问者的 visit()
方法来执行双重调度。
为什么代码示例不能像这样更简单?
interface Node
{
}
class ConstantNode implements Node
{
public int constant;
public ConstantNode(int constant)
{
this.constant = constant;
}
}
class SumNode implements Node
{
public Node left;
public Node right;
public SumNode(Node left, Node right)
{
this.left = left;
this.right = right;
}
}
interface Visitor
{
public int visit(Node n) throws Exception;
}
class EvalVisitor implements Visitor
{
public int visit(Node n) throws Exception {
if (n instanceof ConstantNode) {
return ((ConstantNode) n).constant;
} else if (n instanceof SumNode) {
return this.visit(((SumNode) n).left) + this.visit(((SumNode) n).right);
} else {
throw new Exception("Unsupported node");
}
}
}
public class SimpleVisitorDemo
{
public static void main(String[] args) throws Exception
{
// First make an expression tree to represent the following.
//
// +
// / \
// + 3
// / \
// 1 2
Node a = new ConstantNode(1);
Node b = new ConstantNode(2);
Node c = new ConstantNode(3);
Node d = new SumNode(a, b);
Node e = new SumNode(d, c);
Visitor visitor = new EvalVisitor();
int result = visitor.visit(e);
System.out.println(result);
}
}
在此代码示例中,我完全消除了在每个节点中实现 apply()
的需要,并且包括双重调度逻辑在内的全部访问逻辑现在仅包含在访问者 class 中.
我有以下问题:
能否客观地列举一下简化访客模式在代码的可维护性或效率方面存在的问题?
Why can't the code examples be simpler, [...]
因为您的示例将虚拟分派替换为 switch
分派(您将其实现为对象子类型上的 if
链)。这种方法极难维护,因为编译器无法帮助您检测继承层次结构的变化。
简化实施的具体问题在于最后一个 else
,其中您 return 为零。一个更常见的解决方案是在那里抛出异常,因为你真的不知道你有什么样的节点。
现在想象一下用 SubtractNode
扩展层次结构。这自然需要向 Visitor
接口添加一个方法,确保所有访问者在编译时都被迫处理新的节点子类型。
另一方面,简化示例会继续编译,在您的情况下,它还会继续运行,returnSubtractNode
的错误结果。