I have used the same pattern when developing a double linked tree. Each node has 1 parent, and 0-many children
class Tree<T> where T : Tree<T>
{
T parent;
List<T> children;
T Parent { get; set; }
protected Tree(T parent)
{
this.parent = parent;
parent.children.Add(this);
}
// lots more code handling tree list stuff
}
implementation
class Coordinate : Tree<Coordinate>
{
Coordinate(Coordinate parent) : this(parent) { }
static readonly Coordinate Root = new Coordinate(null);
// lots more code handling coordinate systems
}
usage
Coordinate A = Coordinate.Root;
Coordinate B = new Coordinate(A);
B.Parent // returns a Coordinate type instead of a Node<>.
So anything that inherits from Tree<>
will contain properties for parent and children objects in the appropriate type. This trick is pure magic for me.