Design Patterns - Part 1 ( Composite Pattern)

Sksingh
Posted by in Design Pattern & Practices category on for Advance level | Points: 250 | Views : 10242 red flag
Rating: 4.6 out of 5  
 5 vote(s)

Design patterns are recognized solutions to common problems defined originally by the Gang of Four programmers. Design patterns are used throughout the ASP.NET Framework. The various patterns are commonly divided into several different groups depending on the nature of the design problem they intend to solve.

Introduction


Design patterns are recognized solutions to common problems defined originally by the Gang of Four programmers. Design patterns are used throughout the ASP.NET Framework.  The various patterns are commonly divided into several different groups depending on the nature of the design problem they intend to solve.

Basically design pattern categorized in 3 parts.

1.     Creational Pattern

2.     Structural Patterns
3.     Behavioral Patterns

 

What is Composite Pattern:

 

I am going to describe Composite pattern that belong to creational pattern group.

 

Composite Pattern:

A Composite is a tree structure consisting of individual objects mixed with compositions of objects, that is, objects that have other objects as their children. The goal of the Composite pattern is to be able to treat individual objects and compositions of objects the same way. All objects in the Composite are derived from Composite itself. 

Real Time Example:

Example: We can take the real scenario of any organization, Project Manager à Team Leader à Team Members

 

From above example I can say a project manager can have multiple Team Leaders and a Team Leaders can have multiple Team members but all are employee to organization.

 

My aim to design a template such way that if any other designation come in between existing software so that It should not affect existing one.

 

Example:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using DesignPattern1;

 

namespace DesignPattern

{

    class Program

    {

        static void Main(string[] args)

        {

            Composite c = new Composite("Root");

                Composite PM = new Composite("Sunil");

                 IComponent TL1 = new ISComponent("TL1");

                 PM.Add(TL1);

                 IComponent TL2 = new ISComponent("TL2");

                 PM.Add(TL2);

 

                    IComponent a = new ISComponent("TL3");

                    PM.Add(a);

                    IComponent b = new ISComponent("TL4");

                    PM.Add(b);

 

            c.Add(PM);

 

 

                    Composite PM1 = new Composite("Rakesh");

                        IComponent TL3 = new ISComponent("TL5");

                    PM1.Add(TL3);

                        IComponent TL4 = new Composite("TL1");

                        IComponent tt = new ISComponent("TL8");

                        TL4.Add(tt);

                    PM1.Add(TL4);

 

 

            c.Add(PM1);

 

            c.display(1);

            c.Remove("TL1");

            c.display(1);

 

           

            Console.ReadLine();

           

        }

    }

 

 

    public interface IComponent

    {

        void Add(IComponent c);

        void Remove(string name);

        string Name { get;  }

        void display(int d);

    }

 

 

    public class ISComponent : IComponent

    {

 

        string cName = string.Empty;

 

        public ISComponent(string name)

        {

            cName = name;

        }

        public void Add(IComponent c)

        {

            throw new Exception("Not applicable");

        }

 

        public void Remove(string name)

        {

            this.Remove(name);

        }

 

        public string Name

        {

            get

            {

                return cName;

            }

          

        }

        public void display(int d)

        {

            Console.WriteLine(new String('-', d) + Name);

        }

    }

 

    public class Composite : IComponent

    {

        List<IComponent> cc = null;

        string cName = string.Empty;

 

 

        public string Name

        {

            get

            {

                return cName;

            }

        }

 

        public Composite(string name)

        {

            cc = new List<IComponent>();

            cName = name;

        }

 

        public void Add(IComponent c)

        {

            cc.Add(c);

        }

               

        public void Remove(string name)

        {

           for (int i = 0; i < cc.Count; i++)

            {

                if (name == cc[i].Name)

                {

                    cc.Remove(cc[i]);

                    i--;

                 

                }

                else

                {

                    if (cc[i] is Composite)

                    {

                        cc[i].Remove(name);

                    }

               }

           }

        }

 

 

        public void display(int d)

        {

            Console.WriteLine(new String('-', d) + Name);

            // Recursively display child nodes

            foreach (IComponent component in cc)

            {

                component.display(d + 2);

            }

        }

    }

}




Conclusion

From above example you can observe that same class work as single as well as composite also. This pattern is well known and frequently used wherever tree structure format used.

 

Important: Folder directory and Photo Gallery such typical stuffs work on composite pattern itself. In case of folder directory in Windows XP we can see if we select any folder then it show its all subfolders immediately and if we go further then it shows files in particular folder but once you select a file inside folder that mean file is a single component and there will be no further dig in .

 

Same way in photo gallery a single picture even act like a photo as well as in folder it will be work like group but picture will be same there will be now two separate instances for one picture.

 

Page copy protected against web site content infringement by Copyscape

About the Author

Sksingh
Full Name: sunil singh
Member Level: Starter
Member Status: Member
Member Since: 4/14/2011 6:57:19 AM
Country: India
Regards, Sunil
http://www.dotnetfunda.com
Around 3+ years of experience in Microsoft technologies like ASP.Net, C#.net, SQL server, WCF, WPF, SSIS, AJAX as well as good command on design patterns and currently working as Consultant in an MNC.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)