访客模式项目跟踪器
在具有如下接口的访问者模式的实现中(如果您认为接口本身是错误的,请随时告诉我),谁应该负责跟踪所有访问过的项目的列表?访客还是可访问者?具体来说,跟踪器还必须负责确保同一项目不会被访问两次(如果我正在访问的图表包含循环引用)。
/// <summary>
/// Defines a type that may accept visitors.
/// </summary>
/// <typeparam name="T"></typeparam>
public interface IVisitable<T>
{
// Methods
void Accept(T instance, IVisitor<T> visitor);
}
/// <summary>
/// Defines a type that visits objects.
/// </summary>
/// <typeparam name="T"></typeparam>
public interface IVisitor<T>
{
// Methods
void Visit(IVisitable<T> visitable);
// Properties
bool HasCompleted { get; }
}
In an implementation of the visitor pattern with interfaces as follow (feel free to tell me if you think the interfaces themselves are wrong), who should be responsible for tracking a list of all the items visited? The visitor or the visitable? Specifically, the tracker must also be responsible for making sure the same item isn't visited twice (if the graph I'm visiting contains circular references).
/// <summary>
/// Defines a type that may accept visitors.
/// </summary>
/// <typeparam name="T"></typeparam>
public interface IVisitable<T>
{
// Methods
void Accept(T instance, IVisitor<T> visitor);
}
/// <summary>
/// Defines a type that visits objects.
/// </summary>
/// <typeparam name="T"></typeparam>
public interface IVisitor<T>
{
// Methods
void Visit(IVisitable<T> visitable);
// Properties
bool HasCompleted { get; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
访问者应该跟踪其访问过的所有项目。与只知道自己可以被访问的
IVisitable
相比,访问者始终知道自己访问了什么。任何其他解决方案都会增加耦合。
作为您的界面,我将更改它们,使它们看起来像这样:
这意味着访问者如果不能多次处理同一项目,则应保留已访问项目的列表:
更新 2
IEnumerable
(迭代器)实际上是访问者模式的演变。不同之处在于您将循环从访问的类内部移动到外部。更新3
您可以创建一个列表:
List; items = new List();
并使用foreach
语句(使用IEnumerable
接口)迭代它:这是同样的事情作为:
The visitor should keep track of all items that it have visited. The visitor is always aware of what it visits, compared to the
IVisitable
who only knows that it can be visited.Any other solution would increase the coupling.
As your interfaces, I would change them so that they look like this:
This means that the visitor should keep a list of visited items if it may not process the same item more than once:
Update 2
IEnumerable
(iterators) is really an evolution of the visitor pattern. The difference is that you move the loop from inside the visited class to outside.Update 3
You can create a list:
List<MyItem> items = new List<MyItem>();
and iterate it using theforeach
statement (which uses theIEnumerable<T>
interface):That's the same thing as: