Back to list
Views:   0
Replies:  0
Archived
,
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