Frage

Have some doubts regarding how to implement the Composite Design Pattern to implement a tree structure using Asp.net MVC3 architecture. My concerns are mainly due to the requirement that the tree structure has nodes which have different attributes or properties for each level .

Here is an example:

Tree has a root node - T which has attributes/properties X, Y ,Z ( X can be reffered to as Name for our convenience ) Corresponding child nodes A , B have properties - X, L, M Corresponding next level nodes have properties - X, P, Q Corresponding next nevel nodes have properties - X, R, T and so on.

The current design is not developed with any pattern and just each levels are different class type .

So if we want to add any levels or change it effects the whole classes because of the dependency classes it has.

Please let me know if I can apply composite design pattern so that these type of dependencies and the code maintenance can be easier. how should our POCO or Models should be defined to follow the composite pattern in such a scenario. I'm using Asp.net MVC3, C#, DB first approach with NICedit.js ( for the tree structure display ).

Appreciating the help in advance.

War es hilfreich?

Lösung

The Composite pattern is more used when you want to access the nodes and not care about their type. How are you accessing the nodes? What are you doing with them? Your goal is to improve maintenance. The pattern only applies if you need to treat each node as the same object.

Try something like the following code:

public interface IMyNode
{
    void Print();
}


public class A : IMyNode
{
    public void Print()
    {
        Console.WriteLine("I am a child of T. A or B");
    }
}

public class C : IMyNode
{
    public void Print()
    {
        Console.WriteLine("I am a child of A or B, I am C.");
    }
}

public class D : IMyNode
{
    public void Print()
    {
        Console.WriteLine("I am a child of C. I am D.");
    }
}

public class CompositeMyNode : IMyNode
{
    private readonly List<IMyNode> nodes;

    public CompositeMyNode()
    {
        nodes= new List<IMyNode>();
    }

    public void Add(IMyNode node)
    {
        nodes.Add(node);
    }

    public void AddRange(params IMyNode[] node)
    {
        nodes.AddRange(node);
    }

    public void Delete(IMyNode node)
    {
        nodes.Remove(node);
    }

    public void Print()
    {
        foreach (IMyNode childMyNode in nodes)
        {
            childMyNode.Print();
        }
    }
}

In this case we are priting information on each node without caring which nodes we actually have.

var compositeMyNode = new CompositeMyNode();
var compositeMyNode1 = new CompositeMyNode();
var compositeMyNode2 = new CompositeMyNode();

compositeMyNode1.Add(new A());

compositeMyNode2.AddRange(new A(), new A());

compositeMyNode.AddRange(new C(), compositeMyNode1, compositeMyNode2);

// Will print 4 nodes: A, A, A, C.
compositeMyNode.Print();

Example adapted from the Wikipedia article. Also see this stackoverflow question for further examples and explanations of the composite design pattern.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top