Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / Visual Basic

Composite

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
20 Oct 2013CPOL 9.7K   3  
Composite Design PatternThe Gang of Four definition is "Compose objects into tree structures to represent part-whole hierarchies. Composite lets

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Composite Design Pattern

The Gang of Four definition is "Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly".

In the example below we use the Composite pattern to display a site map.

A VB example of the Composite Pattern

The code below will produce the output:

MySite
 - Products
 -  - Hats
 -  - Gloves
 -  - Boots
 -  - Sales Items
 -  -  - Sale - Hats
 -  -  - Sale - Gloves
 -  -  - Sale - Boots
 - Info
 -  - Delivery Info
 -  - About

VB.NET
' This code can be run in the page behind section
Dim items As New Section("Products")
Dim information As New Section("Info")
Dim saleItems As New Section("Sales Items")

siteRoot.AddNode(items)
siteRoot.AddNode(information)

items.AddNode(New Page("Hats"))
items.AddNode(New Page("Gloves"))
items.AddNode(New Page("Boots"))
items.AddNode(saleItems)

saleItems.AddNode(New Page("Sale - Hats"))
saleItems.AddNode(New Page("Sale - Gloves"))
saleItems.AddNode(New Page("Sale - Boots"))

information.AddNode(New Page("Delivery Info"))
information.AddNode(New Page("About")) ' Display the site layout
siteRoot.displaySelfAndChildren("")

' All Sections and Pages must implement this interface
Public Interface INode

     ReadOnly Property Name() As String
     Sub displaySelfAndChildren(ByVal Indent As String)

End Interface

' The Section class that contains the pages (Branch).
Public Class Section
    Implements INode

    Private _Name As String
    Private _ChildNodes As New ArrayList
    Private _Indent As String = " - "

    Public Sub New(ByVal Name As String)
        _Name = Name
    End Sub

    Public Sub displaySelfAndChildren(ByVal Indent As String) Implements INode.displaySelfAndChildren
        HttpContext.Current.Response.Write(String.Format("{0}{1}</br>", Indent, _Name))
        ' NOTE: We use the _indent variable only for demo purposes
        _Indent = _Indent & Indent
        For Each aNode As INode In _ChildNodes
            aNode.displaySelfAndChildren(_Indent)
        Next
    End Sub

    Sub AddNode(ByRef aNode As INode)
        _ChildNodes.Add(aNode)
    End Sub
    Public ReadOnly Property Name() As String Implements INode.Name
       Get
           Return _Name
       End Get
    End Property
End Class

' The Page Node (Leaf).
Public Class Page
    Implements INode

    Private _Name As String
    
    Public Sub New(ByVal Name As String)
        _Name = Name
    End Sub

    Public Sub displaySelfAndChildren(ByVal Indent As String) Implements INode.displaySelfAndChildren
        HttpContext.Current.Response.Write(String.Format("{0}{1}</br>", Indent, _Name))
    End Sub

    Public ReadOnly Property Name() As String Implements INode.Name
        Get
            Return _Name
        End Get
    End Property
End Class

UML Diagram

Composite UML Diagram

This article was originally posted at http://wiki.asp.net/page.aspx/359/composite

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

755 members

Comments and Discussions

 
-- There are no messages in this forum --