views:

101

answers:

1

I have a hierarchical generic data structure. There is a root node and under that there can be many tree nodes, or just a single data node, and tree nodes can have more tree nodes. A basic tree structure.

All data in my system is persisted in this format. I do however want to have a strongly typed interface to some of the different types of data that these data structures represent (ie. turn a generic hierarchical tree into a strongly typed address record).

I was planning on using an adapter pattern where I pass in a node to the adapter and it then exposes properties by interrogating the tree. This would also allow me to validate the tree (ie. that is has specific elements and that they have valid data in them). It also allows for extensibility (ie. the tree itself would also be exposed if there were additional data that was added at a later date).

Do you think this is the most optimal approach to achieve this or is there a simpler way?

Note: this is in C# and .Net 4.0.

Thanks!

+1  A: 

An Adapter is usually used to bridge between two incompatible interfaces. That doesn't seem to be your problem here. In fact, I don't really see any problem -- as object languages are by nature hierarchical, you should be able to use a mostly 1-to-1 mapping between a class and a tree node.

Perhaps by "Adapter" you just mean a class that wraps a Node or whatever particular Object type that describes your tree nodes, and I'd agree. There should be fairly obvious parent-child relationships that you can describe by having your node classes own or somehow return an array of child node/classes, and the attributes as getters/setters. Any needed validation could be done by the setters, or if need be during construction as a class inspects a given node and its child nodes. Something like the following:

public class NodeFu {

    private Node node;

    public NodeFu(Node node){
        this.node = node;
        // perhaps traverse and validate node data here
    }

    public String getNodeAttribute(String attrName){
        // pardon the offense, Demeter, only for demonstration...
        return node.getAttributes().getNamedItem(attrName).toString();
    }

    public void setNodeAttribute(String attrName, attrValue){
        node.setAttributeValue(attrName, attrValue);
    }

    public ArrayList<NodeFu> getChildren(){
        ArrayList<NodeFu> children = new ArrayList<NodeFu>();
        for (Node childNode : node.getChildNodes()){
            children.add(new NodeFu(childNode));
        }
        return children;
    }
} 

I'm assuming you have more business logic to add to this class that will manipulate the data on the Node itself, otherwise the Node class would suffice and you could just use it directly.

Dave Sims