开发者

Nested hierarchy of objects pattern

I have an object that can have parents and children of the same object type. Is there a standardized way of implementing this pattern?

Something like:

public class Nested
{
   public string Name {get; set;}

   public List<Nested> Parents {get {T开发者_运维知识库ODO}}
   public List<Nested> Children {get {TODO}}
}


Take a look on the composite pattern here


From a memory standpoint, it seems like any instance of your class will need to filter through everything recursively to figure out what the relationships are. That's burdensome.

I would recommend just giving your class a property like this (or something more complex, depending on what you're looking to accomplish):

public Nested parent;

This would make it into a linked list. You can separately create a method outside of the class that manages to find parents based on a limited set of parameters (e.g. FindParentsOfNestedToCertainGeneration(int numOfGenerations, Nested child) -- This will just go to child and go up the chain of parents in the "parent" property).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜