最單純的Whole-part關係就是某對象「內含」(contain) 其它對象。例如﹐
s 1 張訂單上面列示著多個採購的產品項目
s 1 支棒球隊擁有數字教練及多位球員
s 學生的成績單上印有多項成績
s 1 篇文章是由許多句子所組成
s 螢幕上的選擇表(menu)內含數個選擇項
s 1 份考卷包含數十道考題
......
'ex01.bas
Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms
Imports System.Collections
'----------------------------------------------------
Class OrderLine
Private pname As String
Private amt As Double
Public Sub New(ByVal na As String, ByVal am As Double)
pname = na
amt = am
End Sub
Public Function Name() As String
Name = pname
End Function
Public Function Amount() As Double
Amount = amt
End Function
End Class
Class Order
Private OrderID As String
Private lines As ArrayList
Public Sub New(ByVal id As String)
OrderID = id
lines = New ArrayList()
End Sub
Public Sub AddLine(ByVal ln As OrderLine)
lines.Add(ln)
End Sub
Public Function Amount() As Double
Dim total As Double = 0
Dim ln As OrderLine
For Each ln In lines
total = total + ln.Amount()
Next
Amount = total
End Function
Public Function getOrderID() As String
getOrderID = OrderID
End Function
End Class
'------------------------------------------------------------------------
Public Class Form1
Inherits System.WinForms.Form
Public Sub New()
MyBase.New()
Form1 = Me
'This call is required by the Win Form Designer.
InitializeComponent()
'TODO: Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Public Overrides Sub Dispose()
MyBase.Dispose()
components.Dispose()
End Sub
#Region " Windows Form Designer generated code "
......
#End Region
Protected Sub Form1_Click(ByVal sender As Object, ByVal
e As System.EventArgs)
Dim ord As New Order("Order777")
Dim pline As OrderLine
pline = New OrderLine("Pencil", 88.25)
ord.AddLine(pline)
pline = New OrderLine("Ballpen", 110.5)
ord.AddLine(pline)
Messagebox.Show(ord.getOrderID + "'s amount = " + str(ord.Amount()))
End Sub
End Class
'ex02.bas
Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms
Imports System.Collections
'----------------------------------------------------
Class Order
Class OrderLine
Public pname As String
Public amt As Double
End Class
Private OrderID As String
Private lines As ArrayList
Public Sub New(ByVal id As String)
OrderID = id
lines = New ArrayList()
End Sub
Public Sub AddLine(ByVal pna As String, ByVal am As Double)
Dim ln As OrderLine
ln = New OrderLine()
ln.pname = pna
ln.amt = am
lines.Add(ln)
End Sub
Public Function Amount() As Double
Dim total As Double = 0
Dim ln As OrderLine
For Each ln In lines
total = total + ln.amt
Next
Amount = total
End Function
Public Function getOrderID() As String
getOrderID = OrderID
End Function
End Class
'-----------------------------------------------------
Public Class Form1
Inherits System.WinForms.Form
Public Sub New()
MyBase.New()
Form1 = Me
'This call is required by the Win Form Designer.
InitializeComponent()
'TODO: Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Public Overrides Sub Dispose()
MyBase.Dispose()
components.Dispose()
End Sub
#Region " Windows Form Designer generated code "
......
#End Region
Protected Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim ord As New Order("Order777")
ord.AddLine("Pencil", 88.25)
ord.AddLine("Ballpen", 110.5)
Messagebox.Show(ord.getOrderID + "'s amount = " + str(ord.Amount()))
End Sub
End Class
這樣式建議我們應定義Add() 、Remove()和GetChild()三個基本的可再定義的(overridable) 程式﹐以及其它的程式。專家們把這標記法視為樣式﹐就意謂著﹕這是專家們所認為最理想的表達方式。現在﹐實際以依循這個樣式來表達上述BOM 結構,必須定義下述類別:
u Part類別 ----- 對應到Component
u PiecePart類別 ----- 對應到Leaf
u AssemblyPart類別 ----- 對應到Composite
再將之落實為VB程式,如下:
'ex03.bas
Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms
Imports System.Collections
'----------------------------------------------------
Interface IPart
Sub Add(ByVal p As IPart)
Function GetChild(ByVal n As Integer) As IPart
Function Cost() As Double
Function Name() As String
End Interface
Class Part
Private pname As String
Protected Sub New(ByVal na As String)
pname = na
End Sub
Protected Function getName() As String
getName = pname
End Function
End Class
Class PiecePart
Implements IPart
Inherits Part
Private pcost As Double
Public Sub New(ByVal na As String, ByVal c As Double)
MyBase.New(na)
pcost = c
End Sub
Public Sub Add(ByVal p As IPart) Implements IPart.Add
MessageBox.Show("Parts do not have Subparts")
End Sub
Public Function SubPart(ByVal n As Integer) As IPart Implements IPart.GetChild
SubPart = Nothing
End Function
Public Function Cost() As Double Implements IPart.Cost
Cost = pcost
End Function
Public Function Name() As String Implements IPart.Name
Name = MyBase.getName()
End Function
End Class
Class AssemblyPart
Implements IPart
Inherits Part
Private children As ArrayList
Public Sub New(ByVal na As String)
MyBase.New(na)
children = New ArrayList()
End Sub
Public Sub Add(ByVal p As IPart) Implements IPart.Add
children.Add(p)
End Sub
Public Function SubPart(ByVal n As Integer) As IPart Implements IPart.GetChild
Dim obj As Object
obj = children.Item(n)
SubPart = CType(obj, IPart)
End Function
Public Function Cost() As Double Implements IPart.Cost
Dim sum As Double
Dim ps As IPart
sum = 0
For Each ps In children
sum = sum + ps.Cost()
Next
Cost = sum
End Function
Public Function Name() As String Implements IPart.Name
Name = MyBase.getName()
End Function
End Class
'-----------------------------------------------------
Public Class Form1
Inherits System.WinForms.Form
Public Sub New()
MyBase.New()
Form1 = Me
'This call is required by the Win Form Designer.
InitializeComponent()
'TODO: Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Public Overrides Sub Dispose()
MyBase.Dispose()
components.Dispose()
End Sub
#Region " Windows Form Designer generated code "
......
#End Region
Protected Sub Form1_Click( ByVal sender As Object, ByVal
e As System.EventArgs )
Dim aly1, aly2, p1, p2 As IPart
Dim ps As IPart
aly1 = New AssemblyPart("bulb")
p1 = New PiecePart("body", 88.25)
aly1.Add(p1)
p1 = New PiecePart("head", 100.5)
aly1.Add(p1)
aly2 = New AssemblyPart("light")
aly2.Add(aly1)
p1 = New PiecePart("cover", 10)
aly2.Add(p1)
Messagebox.Show(aly2.Name() + "'s cost = " + str(aly2.Cost()))