如何使用复合模式访问特定的叶子?
如果我想访问复合节点的第七百七十七个叶子,并且只访问该叶子,那么使用复合模式是否可以实现这一点,或者这种访问类型是否在模式域之外?
If I wanted to access the seven hundredth and seventy seventh leaf of a Composite node, and only access that leaf, is that possible with the Composite Pattern, or is that type of access outside the domain of the pattern?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您正在谈论复合设计模式。该模式的一般版本不允许您直接跳到叶子,因此您必须递归地遍历子级。用伪代码表示:
此时,您可以访问
results
的第 77 个元素来获取第 77 个叶子,按照“您首先发现的孩子”顺序进行测量。请记住,一般来说,仅仅说“第 77 片叶子”并没有多大意义,除非您准确说明了排序标准。I'm assuming you're talking about the Composite design pattern. The general version of that pattern does not let you skip directly to leaves, so you'll have to walk the children recursively. In pseudocode:
At that point, you can access the 77th element of
results
to get the 77th leaf, as measured in 'children you found out about first' order. Keep in mind that, in general, just saying 'the 77th leaf' is not very meaningful unless you give a precise account of what your ordering criterion is.