访问者模式VS迭代器模式:访问层次结构类? [英] Visitor Pattern VS Iterator Pattern: visiting across hierarchy class?

查看:572
本文介绍了访问者模式VS迭代器模式:访问层次结构类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究访客模式优势,并引用设计模式

I'm studying Visitor Pattern advantages, and quoting Design Patterns:


但迭代器无法跨越具有不同
类型元素的对象结构。例如,页面
295上定义的Iterator接口只能访问Item类型的对象:

But an iteratorcan't work across object structures with different types of elements. Forexample, the Iterator interface defined on page 295 can access only objects of type Item:



template <class Item> 
clas  Iterator { // ... Item CurrentItem() const; };




这意味着迭代器可以访问的所有元素都有一个共同的父类Item 。
访客没有此限制...

This implies that all elements the iterator can visit have a common parentclass Item. Visitor does not have this restriction...



class Visitor {
public:
// ...
void VisitMyType(MyType*);
void VisitYourType(YourType*);
};




MyType和YourType无需通过继承关联
all。

MyType and YourType do not have to be related throughinheritance at all.

我同意这个引用,但我无法弄清楚访客模式可以探索一个结构的例子(比如 List )其中收集的对象与超类没有关联。

I agree about this quote, but I can't figure out an example where the Visitor Pattern could explore a structure (like a List) where objects collected in it are not related by a super class.

换句话说,可以你给我看一个上面的特征是真实的例子吗?

In other words, can you show me an example where the features above is true please?

推荐答案

首先,你应该知道这些模式的用途。

First, you should know what these patterns are for.

迭代器模式用于顺序访问聚合而不暴露其底层表示。因此,您可以隐藏迭代器后面的列表或数组或类似聚合。

The Iterator Pattern is used to access an aggregate sequentially without exposing its underlying representation. So you could Hide a List or array or similar aggregates behind an Iterator.

访问者模式用于对元素结构执行操作,而不更改元素的实现他们自己。

Visitor Pattern is used to perform an action on a structure of elements without changing the implementation of the elements themselves.

所以你在两种不同情况下使用这些模式而不是彼此的替代方案。

So you use the patterns in two different situations and not as alternatives to each other.

In访问者模式您在要访问的每个元素中实现了一个接口IAcceptor。所以访问者模式不依赖于超类但是依赖于接口

In the Visitor Pattern you implement an Interface IAcceptor in each element you want to visit. So the Visitor Pattern doesn't rely on a superclass but on Interfaces

public interface IAcceptor
{
    public void Accept(IVisitor visitor);
}

因此,如果您有一个对象列表,您可以迭代它并访问实现IAcceptor的对象

So if you have a List of objects you can iterate over it and visit the objects implementing IAcceptor

public VisitorExample()
{
    MyVisitorImplementation visitor = new MyVisitorImplementation();
    List<object> objects = GetList();
    foreach(IAcceptor item in objects)
        item.Accept(visitor);
}


public interface IVisitor
{
    public void Visit(MyAcceptorImplementation item);
    public void Visit(AnotherAcceptorImplementation item);
}

public class MyAcceptorImplementation : IAcceptor
{ 
    //Some Code ...
    public void Accept(IVisitor visitor)
    {
        visitor.Visit(this);
    }
}

要完成此处的代码,访问者需要写入控制台如果它访问我或另一个接受者的实现。

To complete the code here is Visitor to write to Console if it visits my or another implementation of an acceptor.

public class MyVisitorImplementation : IVisitor
{
        public void Visit(MyAcceptorImplementation item)
        {
            Console.WriteLine("Mine");
        }
        public void Visit(AnotherAcceptorImplementation item)
        {
            Console.WriteLine("Another");
        }
}

有关更有用的示例和更好的解释,请查看访客模式迭代器模式

For more useful examples and better explanation have a look at Visitor Pattern and Iterator Pattern

编辑:这里有一个使用访客和迭代器的例子。迭代器只是如何在聚合中移动的逻辑。使用层次结构会更有意义。

Here an example using both, the visitor and Iterator. The iterator is just the logic how to move through your aggregate. It would make more sense with a hierarchical structure.

public VisitorExample2()
{
    MyVisitorImplementation visitor = new MyVisitorImplementation();
    List<object> myListToHide = GetList();

    //Here you hide that the aggregate is a List<object>
    ConcreteIterator i = new ConcreteIterator(myListToHide);

    IAcceptor item = i.First();
    while(item != null)
    {
       item.Accept(visitor);
       item = i.Next();
    }
    //... do something with the result
}

这篇关于访问者模式VS迭代器模式:访问层次结构类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆