我正在用C#编写一个小型数据结构库,我遇到了一个架构问题。基本上我有一个实现访问者模式的类,并且有许多可能的访问者实现:
public interface ITreeVisitor<T, U>
{
U Visit(Nil<T> s);
U Visit(Node<T> s);
}
public abstract class Tree<T> : IEnumerable<T>
{
public readonly static Tree<T> empty = new Nil<T>();
public abstract U Accept<U>(ITreeVisitor<T, U> visitor);
}
public sealed class Nil<T> : Tree<T>
{
public override U Accept<U>(ITreeVisitor<T, U> visitor) { return visitor.Visit(this); }
}
public sealed class Node<T> : Tree<T>
{
public Tree<T> Left { get; set; }
public T Value { get; set; }
public Tree<T> Right { get; set; }
public override U Accept<U>(ITreeVisitor<T, U> visitor) { return visitor.Visit(this); }
}
任何时候我想传递一个访问者,我必须创建一个访问者类,实现该接口,然后像这样传递:
class InsertVisitor<T> : ITreeVisitor<T, Tree<T>> where T : IComparable<T>
{
public T v { get; set; };
public Tree<T> Visit(Nil<T> s)
{
return new Node<T>() { Left = Tree<T>.empty, Value = v, Right = Tree<T>.empty };
}
public Tree<T> Visit(Node<T> s)
{
switch (v.CompareTo(s.Value))
{
case -1: return new Node<T>() { Left = Insert(v, s.Left), Value = s.Value, Right = s.Right };
case 1: return new Node<T>() { Left = s.Left, Value = s.Value, Right = Insert(v, s.Right) };
default: return s;
}
}
}
public static Tree<T> Insert<T>(T value, Tree<T> tree) where T : IComparable<T>
{
return tree.Accept<Tree<T>>(new InsertVisitor<T>() { v = value });
}
我不喜欢编写那么多的样板代码,因为当你有非常重要的访问者实现时,它会变得非常混乱。
我想写一些类似于anonymous classes Java(概念代码)的内容:
public static Tree<T> Insert<T>(T v, Tree<T> tree) where T : IComparable<T>
{
return tree.Accept<Tree<T>>(new InsertVisitor<T>()
{
public Tree<T> Visit(Nil<T> s) { return new Node<T>() { Left = Tree<T>.empty, Value = v, Right = Tree<T>.empty }; }
public Tree<T> Visit(Node<T> s)
{
switch (v.CompareTo(s.Value))
{
case -1: return new Node<T>() { Left = Insert(v, s.Left), Value = s.Value, Right = s.Right };
case 1: return new Node<T>() { Left = s.Left, Value = s.Value, Right = Insert(v, s.Right) };
default: return s;
}
}
};
}
有没有办法在C#中使用接口实现来模拟匿名类?
答案 0 :(得分:7)
您可以将类的“行为”部分从针对接口定义的方法更改为在适当时间调用的委托,并通过传递新委托创建新访问者 - 从而使匿名函数能够执行匿名类的工作。
草图代码(未经测试,您可以根据需要进行清理):
class CustomVisitor<T> : ITreeVisitor<T, Tree<T>> where T : IComparable<T>
{
public T v { get; set; };
public Func<Nil<T>, Tree<T>> VisitNil { get; set; }
public Func<Node<T>, Tree<T>> VisitNode { get; set; }
public Tree<T> Visit(Nil<T> s) { return VisitNil(s); }
public Tree<T> Visit(Node<T> s) { return VisitNode(s); }
}
public static Tree<T> Insert<T>(T v, Tree<T> tree) where T : IComparable<T>
{
return tree.Accept<Tree<T>>(new CustomVisitor<T> {
VisitNil = s =>
return new Node<T>() { Left = Tree<T>.empty, Value = v, Right = Tree<T>.empty }; }
VisitNode = s =>
{
switch (v.CompareTo(s.Value))
{
case -1: return new Node<T>() { Left = Insert(v, s.Left), Value = s.Value, Right = s.Right };
case 1: return new Node<T>() { Left = s.Left, Value = s.Value, Right = Insert(v, s.Right) };
default: return s;
}
}
});
}