• English (United States)
    • العربية (مصر)
    • Deutsch (Deutschland)
    • Español (España, alfabetización internacional)
    • français (France)
    • हिंदी (भारत)
    • italiano (Italia)
    • 日本語 (日本)
    • 한국어 (대한민국)
    • Nederlands (Nederland)
    • polski (Polska)
    • русский (Россия)
    • ไทย (ไทย)
    • Türkçe (Türkiye)
    • Tiếng Việt (Việt Nam)
    • 中文(中华人民共和国)
    • 中文(香港特別行政區)
DotNetAge - Mvc & jQuery CMS
Hide sidebar

Composite


Definition



Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.

UML class diagram





Participants



The classes and/or objects participating in this pattern are:
  • Component
declares the interface for objects in the composition.

implements default behavior for the interface common to all classes, as appropriate.

declares an interface for accessing and managing its child components.

(optional) defines an interface for accessing a component's parent in the recursive structure, and implements it if that's appropriate.

  • Leaf
represents leaf objects in the composition. A leaf has no children.

defines behavior for primitive objects in the composition.

  • Composite
defines behavior for components having children.

stores child components.

implements child-related operations in the Component interface.

  • Client
manipulates objects in the composition through the Component interface.

Sample code in C#




///
/// MainApp startup class for Structural
/// Composite Design Pattern.
///
class MainApp
{
///
/// Entry point into console application.
///
static void Main()
{
// Create a tree structure
Composite root = new Composite("root");
root.Add(new Leaf("Leaf A"));
root.Add(new Leaf("Leaf B"));

Composite comp = new Composite("Composite X");
comp.Add(new Leaf("Leaf XA"));
comp.Add(new Leaf("Leaf XB"));

root.Add(comp);
root.Add(new Leaf("Leaf C"));

// Add and remove a leaf
Leaf leaf = new Leaf("Leaf D");
root.Add(leaf);
root.Remove(leaf);

// Recursively display tree
root.Display(1);

// Wait for user
Console.ReadKey();

}
}

///
/// The 'Component' abstract class
///
abstract class Component
{
protected string name;

// Constructor
public Component(string name)
{
this.name = name;
}

public abstract void Add(Component c);
public abstract void Remove(Component c);
public abstract void Display(int depth);
}

///
/// The 'Composite' class
///
class Composite : Component
{
private List _children = new List();

// Constructor
public Composite(string name)
: base(name)
{
}

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

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

public override void Display(int depth)
{
Console.WriteLine(new String('-', depth) + name);

// Recursively display child nodes
foreach (Component component in _children)
{
component.Display(depth + 2);
}
}
}

///
/// The 'Leaf' class
///
class Leaf : Component
{
// Constructor
public Leaf(string name)
: base(name)
{
}

public override void Add(Component c)
{
Console.WriteLine("Cannot add to a leaf");
}

public override void Remove(Component c)
{
Console.WriteLine("Cannot remove from a leaf");
}

public override void Display(int depth)
{
Console.WriteLine(new String('-', depth) + name);
}
}

 


    Average:
  • Reads
    (1011)
  • (0)
  • Permalink
Share to:

Comments (0)


  • rss
  • atom

There is no comment found in this article.

Tag cloud

Anything in here will be replaced on browsers that support the canvas element