3

When working with a single object there are a number of ways to segregate interfaces to it.

  • Breaking it into smaller components that can be treated independently.
  • Breaking it into simpler interfaces.

Both of these seem challenging to do with a tree. I think it would be nice to be able to do the following.

class Node : INodeData, INodePresentation { ... }

class Program
{
    static void Main()
    {
        var node = new Node();
        ManipulateNode(node);
        PresentNode(node);
    }

    void ManipulateNode(INodeData nodeData)
    {
        foreach (INodeData child in nodeData.GetChildren())
             nodeData.AddVertices(...);
        nodeData.Rotate(...);
    }

    void PresentNode(INodePresentation nodePresentation)
    {
        nodePresentation.Color = Color.Blue;
        nodePresentation.Size = 15;
        nodePresentation.Paint();
        foreach (INodePresentation child in nodePresentation.GetChildren())
            PresentNode(child);
    }
}

In this way different parts of the program could use the node according to a concise interface. But each interface I add will require more methods in the Node class which could be unmaintainable. It seems like the presentation and data should be defined in separate classes, but I can't think of a good way to do this while maintaining the ability to traverse the tree. Any suggestions?

1
  • This is not a dup. It's not a generic ISP question that any other question containing that term can answer. It's about a very specific problem getting it to work with a recursive traversal of a tree. Commented Apr 21, 2014 at 12:43

2 Answers 2

2

Probably the most common way is to create a Node<T> with just the structure stuff and pass that into other functions that can use a T to do the presentation stuff.

If you're having difficulty inverting control when doing a recursive traversal, try passing a function into a traversal method, something like (forgive syntax as I don't know C#):

void DepthFirstTraversal(Node<T> node, Function func)
{
    func(node);
    foreach (Node<T> child in node.GetChildren())
        DepthFirstTraversal(child, func);
}

Another useful method to add functionality to a class, but somewhat less familiar depending on what languages you know, is using a mixin. It may or may not be useful in your case.

You can implement mixins in C# 3.0 using extension methods, and I believe there are third-party libraries to help. Mixins basically let you add functionality to a class without requiring it to implement an interface. The implementation is mixed in with the existing implementation of the class.

1
  • Func<Node<T>> in C# and other than that it's perfectly fine Commented May 7, 2014 at 8:18
1

It seems like the presentation and data should be defined in separate classes, but I can't think of a good way to do this while maintaining the ability to traverse the tree. Any suggestions?

The thing about interfaces is that you have to know them all up front. If you want to add new interfaces, you'll need to use the Decorator pattern. But I think maybe you should re-evaluate your approach. Do you really need an interface for every new node manipulation? You can just add a function/static method that takes the tree as an argument and does whatever manipulation you need.

3
  • The issue is that I need to pass the tree to another assembly. Without segregating the interfaces they would be very tightly coupled.
    – TrevorM
    Commented Apr 3, 2014 at 17:54
  • @TrevorM Is there a reason you can't parameterize the tree over the type of its data? I.e. make it a Tree<T> where T can be any type.
    – Doval
    Commented Apr 3, 2014 at 18:02
  • It would be tricky since the class has methods that recursively call its children. With a Tree<T> the contained class would need to reach back to the tree structure containing it. The advantage is that a any interface to the class could reach back using its generic type without the need for special code. So that is something to consider, thanks.
    – TrevorM
    Commented Apr 3, 2014 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.