C++ 中的复合模式
我必须使用类似于电话簿的 C++ 应用程序:带有 STL 联系人列表的 Agenda 类。关于联系人层次结构,有一个名为 Contact 的基类(一个抽象类),以及派生类 Friend 和熟悉(接触的类型)。
例如,这些类有一个名为 getName 的虚拟方法,该方法返回联系人的姓名。
现在我必须通过添加另一种类型的联系人公司(派生自联系人)来实现复合模式,该联系人还包含联系人集合(也是 STL 列表),可以是以下任一类型: “叶子”类型(朋友或熟人),或者他们也可以是公司。
因此,Company 是Compound 类型。
问题是:如何以及在哪里实现 STL find_if 来搜索具有给定名称的联系人(通过 getName 函数或建议我其他方式)在“叶子”类型联系人和内部公司收藏?
换句话说,如何使用统一的函数定义遍历树以便在那里找到可能的匹配项?
我希望我说得很清楚......
I have to work with an application in C++ similar to a phone book: the class Agenda with an STL list of Contacts.Regarding the contacts hierarchy,there is a base-class named Contact(an abstract one),and the derived classes Friend and Acquaintance(the types of contact).
These classes have,for instance, a virtual method called getName,which returns the name of the contact.
Now I must implement the Composite pattern by adding another type of contact,Company(being derived from Contact),which also contains a collection of Contacts(an STL list as well),that can be either of the "leaf" type(Friends or Acquaintances),or they can be Companies as well.
Therefore,Company is the Compound type.
The question is: how and where can I implement an STL find_if to search the contact with a given name(via getName function or suggest me smth else) both among the "leaf"-type Contact and inside the Company collection?
In other words,how do I traverse the tree in order to find possible matches there too,using an uniform function definition?
I hope I was pretty clear...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,一种方法是:
然后:
您正在做的就是要求每个节点找到您正在寻找的节点,而不关心它是什么类型的节点(叶节点或其他节点)。然后,每个节点检查自身,并且对于具有子节点的节点,检查其子节点。
Well, one way to do it:
Then:
What you're doing is asking each node to find the one you're looking for, without caring what type of node (leaf or otherwise) it is. Each node then checks itself, and, for those with child nodes, their children.
对于大量联系人来说,列表是错误的类型,因为您可能需要 O(N) 才能找到最后一个联系人,甚至找不到任何联系人。
我建议您使用哈希映射(来自 boost/tr1 的 unordered_map)或常规映射,这样您就可以使用键通过 ID 或名称找到它们。
听起来公司应该只是一棵联系人树。
您可以在此处找到树实现。
您遍历树以找到所需的节点。
List is the wrong type for a large type of contacts since you might have a O(N) to find the last contact or even to find no contact.
I suggest you to use a hash map (unordered_map from boost/tr1) or a regular map so you will be able to find them by ID or their name using a key.
Also sounds like a company should just be a tree of contacts.
You can find tree implementations here.
You transvase through the tree to find the node you need.
“现在我必须通过添加另一种类型的联系人公司(派生自联系人)来实现复合模式,其中还包含联系人集合(也是 STL 列表),可以是“叶”类型(朋友)或熟人),或者他们也可以是公司”
您可以为公司创建一个与 stl 兼容的复合迭代器。
对于上面给出的前向迭代器方法的实现,请参阅 Github 上的复合迭代器代码。大部分实现都在 Directory.cpp 中。 github 代码用于对文件系统进行建模的复合模式。类目录是复合的。文件类是叶类。 Node 类是基本组件类。
find_if 的函子看起来像
最后,find_if 代码
"Now I must implement the Composite pattern by adding another type of contact,Company(being derived from Contact),which also contains a collection of Contacts(an STL list as well),that can be either of the "leaf" type(Friends or Acquaintances),or they can be Companies as well"
You could create a stl-compatible composite iterator for Company.
For the implementation of the forward iterator methods given above, see the Composite Iterator code on Github. Most of the implementation is in Directory.cpp. The github code is for composite pattern that models a file system. Class Directory is the composite. Class File is the leaf class. Class Node is the base component class.
The functor for find_if would look like
Finally, the find_if code