Back to list
Views:   10.3K
Replies:  1
Archived

Composite pattern & Displaying in treeview

I have a XML file which contains the data and my application needs to parse the XML file and display the data in to the tree view in parent- child hierarchy. The XML looks like below.

<?xml version="1.0" encoding="utf-8"?>
<Fragment>  
    <Feature Title="Main1" Id="Main_First" Description="FeatureDesc_1" Level="3" AllowAdvertise="no" Display="hidden" >
        <Feature Title="Main1Child1" Id="Main_FirstC1" Description="FeatureDesc_11" Level="3" AllowAdvertise="no" Display="hidden" />
        <Feature Title="Main1Child2" Id="Main_FirstC2" Description="FeatureDesc_12" Level="3" AllowAdvertise="no" Display="Show" />
        <Feature Title="Main1Child3" Id="Main_FirstC3" Description="FeatureDesc_13" Level="3" AllowAdvertise="no" Display="Show" />
    </Feature>
    <Feature Title="Main2" Id="Main_Second" Description="FeatureDesc_2" Level="3" AllowAdvertise="no" Display="hidden" />
    <Feature Title="Main3" Id="Main_Third" Description="FeatureDesc_3" Level="3" AllowAdvertise="no" Display="hidden" />
        <Feature Title="Main3Child1" Id="Main_FirstC1" Description="FeatureDesc_31" Level="3" AllowAdvertise="no" Display="hidden" />
        <Feature Title="Main3Child2" Id="Main_FirstC2" Description="FeatureDesc_32" Level="3" AllowAdvertise="no" Display="hidden" />
        <Feature Title="Main3Child3" Id="Main_FirstC3" Description="FeatureDesc_33" Level="3" AllowAdvertise="no" Display="hidden" />
    <Feature Title="Main4" Id="Main_Fourth" Description="FeatureDesc_4" Level="3" AllowAdvertise="no" Display="hidden" />   
 </Fragment>


I have created the CFeature class with members of Title, Id, Description, Level, AllowAdvertise, Display and array of CFeature objects to store the child objects if it has any.

I want to use composite pattern to do this. Can any one suggest how to do this?

mutpan S, Dec 27, 2010
Reply 1
Hello mutpan,

you can implement composite pattern as in DoFactory method. I have created a sample program. Please have a look. :-)


namespace DesignPattern.Composite
{
    class Program
    {
        static void Main(string[] args)
        {
            Tree tree = new Tree();
            CFeature cTree = tree.GetTreeStructure();
            cTree.Display(1);
            Console.Read();
        }
    }

    class Tree
    {
        public CFeature GetTreeStructure()
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load("data.xml");
            XmlNode root = xDoc.DocumentElement;
            CFeature cFeature = new CFeature(root.Name, "", "", "", "", "");
            CreateTreeStructure(root, cFeature);
            return cFeature;
        }

        private void CreateTreeStructure(XmlNode node, AbstractFeature cFeature)
        {
            if (node.HasChildNodes)
            {
                foreach (XmlNode cNode in node.ChildNodes)
                {
                    AbstractFeature cFeature1 = null;
                    if (cNode.HasChildNodes)
                    {
                        cFeature1 = new CFeature(cNode.Attributes["Title"].Value, cNode.Attributes["Id"].Value,
                                                cNode.Attributes["Description"].Value, cNode.Attributes["Level"].Value,
                                                cNode.Attributes["AllowAdvertise"].Value, cNode.Attributes["Display"].Value);

                        cFeature.Add(cFeature1);
                        CreateTreeStructure(cNode, cFeature1);
                    }
                    else
                        cFeature.Add(new Feature(cNode.Attributes["Title"].Value, cNode.Attributes["Id"].Value,
                                                               cNode.Attributes["Description"].Value, cNode.Attributes["Level"].Value,
                                                               cNode.Attributes["AllowAdvertise"].Value, cNode.Attributes["Display"].Value));
                }
            }
            else
            {
                cFeature.Add(new Feature(node.Attributes["Title"].Value, node.Attributes["Id"].Value,
                                       node.Attributes["Description"].Value, node.Attributes["Level"].Value,
                                       node.Attributes["AllowAdvertise"].Value, node.Attributes["Display"].Value));
            }
        }
    }

    abstract class AbstractFeature
    {
        public AbstractFeature(string title, string id, string description, string level, string allowAdvertise, string display)
        {
            _title = title;
            _id = id;
            _description = description;
            _level = level;
            _allowAdvertise = allowAdvertise;
            _display = display;
        }

        public virtual void Display(int indent)
        {
            Console.WriteLine(new String('-', indent) + String.Format("Title: {0}, Id: {1}, Description: {2}, Level: {3}, AllowAdvertise: {4}, Display: {5}",
                 _title, _id, _description, _level, _allowAdvertise, _display));
        }

        public abstract void Add(AbstractFeature feature);

        public abstract IEnumerator GetEnumerator();

        protected string _title;
        protected string _id;
        protected string _description;
        protected string _level;
        protected string _allowAdvertise;
        protected string _display;
    }

    class Feature : AbstractFeature
    {
        public Feature(string title, string id, string description, string level, string allowAdvertise, string display)
            : base(title, id, description, level, allowAdvertise, display)
        { }

        public override void Add(AbstractFeature feature)
        {
            // Not useful
        }

        public override IEnumerator GetEnumerator()
        {
            return null;
        }

    }

    class CFeature : AbstractFeature, IEnumerable
    {

        public CFeature(string title, string id, string description, string level, string allowAdvertise, string display)
            : base(title, id, description, level, allowAdvertise, display)
        {
            _children = new List<AbstractFeature>(0);
        }

        public override void Add(AbstractFeature feature)
        {
            _children.Add(feature);
        }

        public override void Display(int indent)
        {
            Display(this, indent);
        }

        private void Display(CFeature cFeature, int indent)
        {
            Console.WriteLine(new String('-', indent) + "+" + String.Format("Title: {0}, Id: {1}, Description: {2}, Level: {3}, AllowAdvertise: {4}, Display: {5}",
                 _title, _id, _description, _level, _allowAdvertise, _display));

            foreach (AbstractFeature f in this)
            {
                f.Display(indent + 2);
            }
        }

        public override IEnumerator GetEnumerator()
        {
            return _children.GetEnumerator();
        }

        private List<AbstractFeature> _children;
    }
}

 
Pradip Shrestha, Dec 28, 2010


Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Licensing       EULA       Sitemap      
© Data & Object Factory, LLC.
Made with    in Austin, Texas