SoftPilot2000 - Software Outsourcing Ukraine. Software Development Ukraine. Design Patterns. C# Templates, dotNet, ASP.NET, .Net Structural Design Patterns. Composite. SoftPilot2000 - Software Outsourcing Ukraine. Software Development Ukraine. Design Patterns. C# Templates, dotNet, ASP.NET, .Net Structural Design Patterns. Composite.
SoftPilot2000 - Software Outsourcing Ukraine. Software Design Patterns C# Articles. SoftPilot2000 - Software Outsourcing Ukraine. Software Development Ukraine. Design Patterns. C# Templates, dotNet, ASP.NET, .Net Structural Design Patterns. Composite.
SoftPilot2000 - Software Outsourcing Ukraine. Software Design Patterns C# Articles. SSoftPilot2000 - Software Outsourcing Ukraine. Software Development Ukraine. Design Patterns. C# Templates, dotNet, ASP.NET, .Net Structural Design Patterns. Composite.
SoftPilot.2000 - Software Outsourcing Ukraine. Software Design Patterns C# Articles. SoftPilot.2000 - Software Outsourcing Ukraine. Software Development Ukraine. Design Patterns. C# Templates, dotNet, ASP.NET, .Net Structural Design Patterns. Composite.
Software Outsourcing Ukraine : softPilot.2000 - Site Map SoftPilot2000 - Software Outsourcing Ukraine. Software Development Ukraine. Design Patterns. C# Templates, dotNet, ASP.NET, .Net Structural Design Patterns. Composite.
SoftPilot2000 - Software Outsourcing Ukraine. Software Development Ukraine. Design Patterns. C# Templates, dotNet, ASP.NET, .Net Structural Design Patterns. Composite - Offshore Programming : professional dotNet B2B solutions : C#, Smart Client, Windows Forms, AJAX, Web Services. SoftPilot2000 - Software Outsourcing Ukraine. Software Development Ukraine. Design Patterns. C# Templates, dotNet, ASP.NET, .Net Structural Design Patterns. Composite - Offshore Programming : professional dotNet B2B solutions : C#, Smart Client, Windows Forms, AJAX, Web Services. SoftPilot2000 - Software Outsourcing Ukraine. Software Development Ukraine. Design Patterns. C# Templates, dotNet, ASP.NET, .Net Structural Design Patterns. Composite - Offshore Programming : professional dotNet B2B solutions : C#, Smart Client, Windows Forms, AJAX, Web Services. SoftPilot2000 - Software Outsourcing Ukraine. Software Development Ukraine. Design Patterns. C# Templates, dotNet, ASP.NET, .Net Structural Design Patterns. Composite - Offshore Programming : professional dotNet B2B solutions : C#, Smart Client, Windows Forms, AJAX, Web Services. SoftPilot2000 - Software Outsourcing Ukraine. Software Development Ukraine. Design Patterns. C# Templates, dotNet, ASP.NET, .Net Structural Design Patterns. Composite - Offshore Programming : professional dotNet B2B solutions : C#, Smart Client, Windows Forms, AJAX, Web Services. SoftPilot2000 - Software Outsourcing Ukraine. Software Development Ukraine. Design Patterns. C# Templates, dotNet, ASP.NET, .Net Structural Design Patterns. Composite - Offshore Programming : professional dotNet B2B solutions : C#, Smart Client, Windows Forms, AJAX, Web Services. SoftPilot2000 - Software Outsourcing Ukraine. Software Development Ukraine. Design Patterns. C# Templates, dotNet, ASP.NET, .Net Structural Design Patterns. Composite - Offshore Programming : professional dotNet B2B solutions : C#, Smart Client, Windows Forms, AJAX, Web Services.
SoftPilot2000 : Software Outsourcing Ukraine - Home SoftPilot2000 : Software Outsourcing Ukraine - About SoftPilot2000 : Software Outsourcing Services Ukraine - Company Profile Software Development Ukraine : SoftPilot2000 - Case Study SoftPilot2000 : SEO Ukraine - Search Engine Optimization Services Ukraine - Services SoftPilot2000 : Software Outsourcing Ukraine - Partners SoftPilot2000 : Software Outsourcing Ukraine - Contacts SoftPilot2000 : Software Outsourcing Ukraine - E-mail SoftPilot.2000 : Software Outsourcing to Ukraine

SoftPilot2000 - Software Outsourcing Ukraine.

Software development articles about design patterns implementation in C#.

Structural Patterns.




Design Pattern Composite

Structural Design Pattern Composite let us to operate with different parts of the complex object in a unifyed manner. The good example of this abstraction could be a HTML page (composite object), which could consist of single tags (leaf) as well as of the list tags, which are composite objects, which includes the leaves.

Let's try to implement this in C#.

 

   
  Composite Design Pattern
C# implementation:

//
// Composite pattern. 
//
using System;
using System.Collections;
// "Component"
abstract class PageElement
{
  protected string name;
  // Constructor
  public PageElement( string name )
  {
    this.name = name;
  } 
  // Methods
  abstract public void Add(PageElement e);
  abstract public void Remove( PageElement e);
  abstract public void GetContent();
}

// "Leaf" 
class Element : PageElement
{
  public Element( string name ) : base( name ) {}
  public override void Add( PageElement e )
  {
    Console.WriteLine("Error. Can't add to leaf");
  }
  public override void Remove( PageElement e )
  {
    Console.WriteLine("Error. Can't remove from a leaf");
  }
  public override void GetContent()
  {
    Console.WriteLine( "This is content of :" + name );
  }
}

// "Composite" 

class CompositeElement : PageElement
{
  // Fields
  private ArrayList elements = new ArrayList();
  // Constructor
  public CompositeElement( string name ) 
                              : base( name ) {}
  // Methods
  public override void Add( PageElement d )
  {
    elements.Add( d );
  }

  public override void Remove( PageElement d )
  {
    elements.Remove( d );
  }

  public override void GetContent()
  {
    Console.WriteLine( "Content from composite object : "+name);

    // Display each child element on this node
    foreach( PageElement p in elements )
      p.GetContent();
  }
}

/// 
///    Summary description for Client.
/// 
public class Client
{
  public static void Main( string[] args )
  {   
    // Create a tree structure 
    CompositeElement page = new CompositeElement( "Page 1" );
    page.Add( new Element( "Tag 1" ));
    Element e2 = new Element( "Tag 2" );
    page.Add(e2);
    page.Add( new Element( "Tag 3" ));

    CompositeElement list = new CompositeElement( "List 1" );
    list.Add( new Element( "List 1, Tag 1" ) );
    list.Add( new Element( "List 1, Tag 2" ) );
    page.Add( list );
    // Add and remove a PrimitiveElement
    page.Remove(e2);
    // Recursively display nodes
    page.GetContent();
    Console.ReadLine();
  }
}


Software Outsoursing Ukraine - SoftPilot2000 - Articles - Composite - C# Templates



The results are below:


C:\...cs\Composite\Composite\bin\Debug>Composite.exe
Content from composite object : Page 1
This is content of :Tag 1
This is content of :Tag 3
Content from composite object : List 1
This is content of :List 1, Tag 1
This is content of :List 1, Tag 2



 
   
(C) SoftPilot.2000 All Rights Reserved. 2000 - 2006