创建可变深度类别的最佳方法?
我的问题是如何最好地创建可变深度层次结构。 假设我希望能够将产品放入类别层次结构中,但是不同产品的层次结构深度不同。
例如,一辆法拉利可能属于“车辆 -> ”类别。汽车->运动的 而 LED 3D 电视可能属于电子产品 ->电视-> LED-> 3D。
希望你明白了:-) 对此进行建模的最佳方法是什么?我是否应该创建一个可以保存自身列表的类别对象,也许有一个布尔属性告诉当前对象是否是叶节点?其他建议?
或者我应该非常努力地为我的层次结构设置固定的深度?
My question regards how to best create a variable depth hierarchy.
Let´s say that I want to be able to put a product in a category hierarchy, however the depth of the hierarchy differs for different products.
For example, a Ferrari might be in the category Vehicle -> Car -> Sports
while a LED 3D TV might be in Electronics -> TV -> LED -> 3D.
Hopefully you get the idea :-)
What would be the best way to model this? Should I create a Category object that can hold a List of itself, with perhaps a boolean attribute telling if the current object is a leaf-node or not? Other suggestions?`
Or should I just try REALLY hard to have a fixed depth for my hierarchies?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
表示类别的模型可以是一棵树(具有不可见的根节点、“开始”或其他节点)。每个类别都有一个父类别和一个或多个子类别。
然后,对于该产品,向该产品添加类别列表。这是非常灵活的,因为有一天您可能会考虑在不同的类别中列出单个产品。
类别类的非常基本的模型:
The model to represent the categories could be a tree (with an invisible root node, the "start", or whatever). Each category has one parent and one or many child categories.
Then, for the product, add a list of categories to that product. This is quite flexible because one day you may think about listing a single product in different categories.
Very Basic model for the category class:
你可以做出很多选择,但我想让
产品知道他们的类别(最具体的级别 - 所以法拉利在“运动”中)
类别知道他们的父级,所以“运动”指向“汽车”,“汽车” ”指向“车辆”,“车辆”指向空,因为它是顶级类别。
如果您需要以这种方式存储它,它也可以很好地映射到 SQL 数据库。
不过,您仍然需要决定如何存储所有可用类别的列表。
另外,如果您需要能够从顶级类别转到其所有子类别,那么您可能还需要存储这些反向链接。
There's a number of choices you could make, but I would have something like
Product know their category (the most specific level - so Ferrari is in "Sports")
Categories know their parent, so "Sports" points to "Cars", "Cars" points to "Vehicles", "Vehicles" points to null, because it is a top level category.
That will also map pretty well onto an SQL database if you need to store it that way.
You'll still need to decide how to store a list of all available categories though.
Also, if you need to be able to go from a top-level category to all it's children, then you'll probably want to store those reverse links as well.
您对
Category
对象的建议对我来说听起来很正确。 复合模式可能会非常匹配。我也喜欢 Andreas_D 将产品和类别层次结构解耦的想法。
Your suggestion with the
Category
object sounds right to me. The composite pattern might match very well.I also like the idea of Andreas_D to decouple the products and the category hierarchy.