Author - StudySection Post Views - 44 views
Composite Pattern

Composite Pattern in C#

In C# Composite pattern is a type of structural design pattern which allows us to compose the objects into a tree-like structure and then we can work with it as working with a singular object. This is mainly required for the problems in which we need to have a tree-type structure. With this pattern, we can run the methods on the tree structure recursively. It is used so as to represent the hierarchies of UI components or while working with graphs/charts.

Below is the example code for the composite pattern:

using System;
using System.Collections.Generic;
namespace Webner.Composite
{
// Component class declares operations for simple and
// complex objects of a composition.
abstract class Component
{
public Component() { }
public abstract string Operation();
public virtual void Add(Component component)
{
throw new NotImplementedException();
}
public virtual void Remove(Component component)
{
throw new NotImplementedException();
}
public virtual bool IsComposite()
{
return true;
}
}
class Leaf: Component
{
public override string Operation()
{
return "Leaf";
}
public override bool IsComposite()
{
return false;
}
}
//Composite class represents the complex components that
//have children. Composite objects delegate the actual work
//to their children and then provide the summed up result.
class Composite: Component
{
protected List<Component> _children = new List<Component>();

public override void Add(Component component)
{
this._children.Add(component);
}
public override void Remove(Component component)
{
this._children.Remove(component);
}

public override string Operation()
{
int i = 0;
string result = "Branch(";
foreach (Component component in this._children)
{
result += component.Operation();
if (i != this._children.Count - 1)
{
result += "+";
}
i++;
}
return result + ")";
}
}
class Client
{
public void ClientCode(Component leaf)
{
Console.WriteLine($"RESULT: {leaf.Operation()}\n");
}
public void ClientCode2(Component component1, Component component2)
{
if (component1.IsComposite())
{
component1.Add(component2);
}
Console.WriteLine($"RESULT: {component1.Operation()}");
}
}
class Program
{
static void Main(string[] args)
{
Client client = new Client();
Leaf leaf = new Leaf();
Console.WriteLine("This is a simple component");
client.ClientCode(leaf);
Composite tree = new Composite();
Composite branch1 = new Composite();
branch1.Add(new Leaf());
branch1.Add(new Leaf());
Composite branch2 = new Composite();
branch2.Add(new Leaf());
tree.Add(branch1);
tree.Add(branch2);
Console.WriteLine("This is a Composite Pattern tree structure");
client.ClientCode(tree);
client.ClientCode2(tree, leaf);
}
}
}

jQuery allows the user to create powerful and dynamic webpages that run without any hiccup. With StudySection, you have the liberty to choose among beginner or expert level jQuery Certification Exams to help you excel in this career field.

Leave a Reply

Your email address will not be published.