迭代节点的层次结构 - 访问者和复合? [英] Iterating hierarchy of nodes - Visitor and Composite?

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

问题描述

让我们想象一下,我有一个节点集合,我以后为我的Renderer类使用。然后我有一个访问类,可以访问节点或整个集合。这很简单,因为我的节点集合只是一个std :: list的包装器,只有一些额外的方法。



问题是我想有一个树状结构对于节点(而不是简单列表),所以一个节点可以有一个父和n个孩子。这将是方便,因为我想要能够传递给我的渲染器一个节点,并渲染一切下面的那个节点。答案可能是复合。



如何一起使用Visitor和Composite?我已经读过,它通常是一个很好的组合,但我的实现看起来很糟糕...我缺少sth。

解决方案

我有一个非常类似的实现我们的系统。我想要一种方法来组成几何对象的层次结构,并将其渲染到卷中。我使用复合模式来构造我的描述(根是Node,然后派生的孩子是compositeNode(节点列表)。



CompositeNode有方法accept因此,您的访问者层次结构具有作为NodeVisitor的基类和派生访问者,如RenderVisitor(呈现器)对象),ReportVisitor(将节点信息转换成文本)。你的基类将需要接受基本和专门的节点类型。



是的,组合工作,



希望这有助于


Let's imagine I have a collection of nodes that I use for my Renderer class later on. Then I have a Visitor class that can visit node or whole collection. It's simple because my collection of nodes it's simply a wrapper to the std::list with few extra methods.

The problem is I'd like to have a tree like structure for nodes(instead of simple list) so a node can have a parent and n children. That would be handy as I'd like to be able to pass to my Renderer a node and render everything "below" that node. The answer probably is Composite.

How can I use together Visitor and Composite? I've read that its often a good combo but my implementations look pretty bad... I'm missing sth.

解决方案

I have something very similar implemented for our system. I wanted a way to compose hierarchy of geometrical object and render them into the volume. I used composite pattern to compose my description (root was Node and then derived child was compositeNode (list of Nodes).

CompositeNode has method accept() which accepts a visitor (Visitor) and then inside the accept() you do visitor->visit(this).

Thus your visitor hierarchy has base class as NodeVisitor and derived visitors like RenderVisitor (renders objects), ReportVisitor (dumped node info into text). Your base class will need to accept both base and specialized node types.

So yes, combo works and I have working code but I agree that design takes more effort than what you would read online (Wiki or toy example).

Hope this helps

这篇关于迭代节点的层次结构 - 访问者和复合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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