Translate

Tuesday, September 17, 2013

composite pattern c# simple example

What are we trying to solve?
Suppose that there is an email group, and there are groups underneath it that have their own groups (as shown below). If you are given a group, You should be able to traverse through the tree and find all the people in it and send them emails.  



This is an ideal case for composite pattern. Composite pattern is applied where you want to treat every node in the tree in the same way. If a group of Super People is handed to you, you should be able to do something like groupOfSuperPeople.SendMail() and emails should go out to all people in that group.

How do we solve this?
The idea here is to have the "leaf" of the tree and the node both implement the same interface. The implementation in the node will call the implementation of the nodes beneath it. This will continue until the leaf is reached. The leaf's implementation of this interface will actually send out the email.  

The C# code that applies composite pattern to solve this problem
using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
internal class Program
{
    private static void Main(string[] args)
    {
        var adam = new Person
            {
                Name = "Adam",
                EmailAddress = "Adam@dotnetanalysis.com"
            };
        var eve = new Person
            {
                Name = "Eve",
                EmailAddress = "Eve@dotnetanalysis.com"
            };
        var joker = new Person
            {
                Name = "Joker",
                EmailAddress = "Joker@dotnetanalysis.com"
            };
        var skeletor = new Person
            {
                Name = "Skeletor",
                EmailAddress = "Skeletor@dotnetanalysis.com"
            };
        var batMan = new Person
            {
                Name = "BatMan",
                EmailAddress = "BatMan@dotnetanalysis.com"
            };
        var ironMan = new Person
            {
                Name = "IronMan",
                EmailAddress = "IronMan@dotnetanalysis.com"
            };

        var parentGroup = new Group {Name = "Parent"};
        var groupPeople = new Group {Name = "People"};
        var groupSuper = new Group {Name = "Super"};
        var groupSuperHeros = new Group {Name = "SuperHeroes"};
        var groupSuperVillians = new Group {Name = "SuperVillians"};

        groupPeople.Add(adam);
        groupPeople.Add(eve);

        groupSuperHeros.Add(batMan);
        groupSuperHeros.Add(ironMan);

        groupSuperVillians.Add(joker);
        groupSuperVillians.Add(skeletor);

        groupSuper.Add(groupSuperHeros);
        groupSuper.Add(groupSuperVillians);

           
        parentGroup.Add(groupPeople);
        parentGroup.Add(groupSuper);

        parentGroup.SendMail();
    }
}

public interface ISendMail
{
    void SendMail();
}

public class Person : ISendMail
{
    public string Name { get; set; }
    public string EmailAddress { get; set; }

    public void SendMail()
    {
        Console.WriteLine("Sending email to {0}", EmailAddress);
    }
}


public class Group : ISendMail
{
    private readonly List<ISendMail> sendMailImplementations;

    public Group()
    {
        sendMailImplementations = new List<ISendMail>();
    }

    public string Name { get; set; }

    public void SendMail()
    {
        Console.WriteLine("Processing Group " + Name);
        foreach (ISendMail sendMailImplementation in sendMailImplementations)
        {
            sendMailImplementation.SendMail();
        }
    }

    public void Add(ISendMail sendMail)
    {
        sendMailImplementations.Add(sendMail);
    }

    public void Remove(ISendMail sendMail)
    {
        sendMailImplementations.Remove(sendMail);
    }
}
}


Output:


No comments:

Post a Comment

Comments will appear once they have been approved by the moderator