开发者

Best way to create variable depth category?

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? Shoul开发者_如何学运维d 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?


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");
  }
}


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜