创建可变深度类别的最佳方法?

发布于 2024-10-16 01:38:53 字数 276 浏览 1 评论 0原文

我的问题是如何最好地创建可变深度层次结构。 假设我希望能够将产品放入类别层次结构中,但是不同产品的层次结构深度不同。

例如,一辆法拉利可能属于“车辆 -> ”类别。汽车->运动的 而 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不再让梦枯萎 2024-10-23 01:38:53

表示类别的模型可以是一棵树(具有不可见的根节点、“开始”或其他节点)。每个类别都有一个父类别和一个或多个子类别。

然后,对于该产品,向该产品添加类别列表。这是非常灵活的,因为有一天您可能会考虑在不同的类别中列出单个产品。


类别类的非常基本的模型:

public class Category {

  private List<Category> children = new ArrayList<Category>();
  private Category parent;
  private String name;

  // private constructor
  private Category(Category parent, String name) { 
    this.parent = parent; 
    this.name = name;
  }

  // adds a category to this category
  public Category addCategory(String name) { 
     Category child = new Category(this, name);
     children.add(child); 
     return child;
  }

  // creates and returns a new categories tree
  public static Category createCategories() {
     return new Category(null, "root");
  }
}

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:

public class Category {

  private List<Category> children = new ArrayList<Category>();
  private Category parent;
  private String name;

  // private constructor
  private Category(Category parent, String name) { 
    this.parent = parent; 
    this.name = name;
  }

  // adds a category to this category
  public Category addCategory(String name) { 
     Category child = new Category(this, name);
     children.add(child); 
     return child;
  }

  // creates and returns a new categories tree
  public static Category createCategories() {
     return new Category(null, "root");
  }
}
心的位置 2024-10-23 01:38:53

你可以做出很多选择,但我想让

class Product
{
    private Category category;
    // ...
}

class Category
{
    private Category parent;
    private String name;

    public Category getParent() { return parent; }
    public boolean isTopLevelCategory() { return parent == null }

    public String getName() { return name; }

    public String getFullName() {
       if(isTopLevelCategory())
          return name;
       else
          return parent.getFullName() + " -> " + name;
    }

    // ....
}

产品知道他们的类别(最具体的级别 - 所以法拉利在“运动”中)

类别知道他们的父级,所以“运动”指向“汽车”,“汽车” ”指向“车辆”,“车辆”指向空,因为它是顶级类别。

如果您需要以这种方式存储它,它也可以很好地映射到 SQL 数据库。

不过,您仍然需要决定如何存储所有可用类别的列表。

另外,如果您需要能够从顶级类别转到其所有子类别,那么您可能还需要存储这些反向链接。

There's a number of choices you could make, but I would have something like

class Product
{
    private Category category;
    // ...
}

class Category
{
    private Category parent;
    private String name;

    public Category getParent() { return parent; }
    public boolean isTopLevelCategory() { return parent == null }

    public String getName() { return name; }

    public String getFullName() {
       if(isTopLevelCategory())
          return name;
       else
          return parent.getFullName() + " -> " + name;
    }

    // ....
}

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.

柒夜笙歌凉 2024-10-23 01:38:53

您对 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文