Option Explicit
Public Function hasNext() As Boolean
End Function
Public Function nextItem() As Object
End Function
Option Explicit
Public Function iterator() As IIterator
End Function
Option Explicit
Private this_id As String ' ID
Private this_name As String ' 名前
'-------------------------------------------------------------------------------
' 【関 数 名】コンストラクタ
' 【処理概要】初期化を行う
' 【引 数】なし
' 【戻 り 値】なし
'-------------------------------------------------------------------------------
Private Sub Class_Initialize()
End Sub
'-------------------------------------------------------------------------------
' 【関 数 名】デストラクタ
' 【処理概要】終期化を行う
' 【引 数】なし
' 【戻 り 値】なし
'-------------------------------------------------------------------------------
Private Sub Class_Terminate()
End Sub
'-------------------------------------------------------------------------------
' 【getter・setter】ID
'-------------------------------------------------------------------------------
Public Property Get id() As String
id = this_id
End Property
Public Property Let id(ByVal value As String)
this_id = value
End Property
'-------------------------------------------------------------------------------
' 【getter・setter】名前
'-------------------------------------------------------------------------------
Public Property Get name() As String
name = this_name
End Property
Public Property Let name(ByVal value As String)
this_name = value
End Property
'-------------------------------------------------------------------------------
' 【関 数 名】初期化
' 【処理概要】初期化を行う
' 【引 数】[I]ByVal id As String
' [I]ByVal name As String
' 【戻 り 値】なし
'-------------------------------------------------------------------------------
Public Sub init(ByVal id As String, ByVal name As String)
Me.id = id
Me.name = name
End Sub
Option Explicit
Implements IIterator
Private baseAggr As New BaseAggregate
Private index As Long
'-------------------------------------------------------------------------------
' 【関 数 名】次要素有無判定
' 【処理概要】次の要素があるか判定する
' 【引 数】なし
' 【戻 り 値】True 次要素あり
' False 次要素なし
'-------------------------------------------------------------------------------
Public Function IIterator_hasNext() As Boolean
If index <= baseAggr.getCount Then
IIterator_hasNext = True
Else
IIterator_hasNext = False
End If
End Function
'-------------------------------------------------------------------------------
' 【関 数 名】次要素取得
' 【処理概要】次の要素を取得する
' 【引 数】なし
' 【戻 り 値】次要素あり
'-------------------------------------------------------------------------------
Public Function IIterator_nextItem() As Object
Set IIterator_nextItem = baseAggr.getItem(index)
index = index + 1
End Function
'-------------------------------------------------------------------------------
' 【関 数 名】初期化
' 【処理概要】初期化を行う
' 【引 数】[I]ByVal BaseAggregate As IAggregate 集合体
' 【戻 り 値】なし
'-------------------------------------------------------------------------------
Public Sub init(ByVal BaseAggregate As IAggregate)
Set baseAggr = BaseAggregate
index = 1
End Sub
Option Explicit
Implements IAggregate
Private items As New Collection
'-------------------------------------------------------------------------------
' 【関 数 名】コンストラクタ
' 【処理概要】初期化を行う
' 【引 数】なし
' 【戻 り 値】なし
'-------------------------------------------------------------------------------
Private Sub Class_Initialize()
Call addItem("0", "なまえ0")
Call addItem("1", "なまえ1")
End Sub
'-------------------------------------------------------------------------------
' 【関 数 名】デストラクタ
' 【処理概要】終期化を行う
' 【引 数】なし
' 【戻 り 値】なし
'-------------------------------------------------------------------------------
Private Sub Class_Terminate()
End Sub
'-------------------------------------------------------------------------------
' 【関 数 名】イテレータ
' 【処理概要】イテレータ
' 【引 数】なし
' 【戻 り 値】イテレータ
'-------------------------------------------------------------------------------
Public Function IAggregate_iterator() As IIterator
Dim res As New BaseBeanIterator
Call res.init(Me)
Set IAggregate_iterator = res
End Function
'-------------------------------------------------------------------------------
' 【関 数 名】要素数取得
' 【処理概要】要素数を取得する
' 【引 数】なし
' 【戻 り 値】要素数
'-------------------------------------------------------------------------------
Public Function getCount() As Integer
getCount = items.Count
End Function
'-------------------------------------------------------------------------------
' 【関 数 名】要素取得
' 【処理概要】要素を取得する
' 【引 数】[I]ByVal index As Integer 要素インデックス
' 【戻 り 値】要素
'-------------------------------------------------------------------------------
Public Function getItem(ByVal index As Integer) As BaseBean
Set getItem = items.Item(index)
End Function
'-------------------------------------------------------------------------------
' 【関 数 名】要素追加
' 【処理概要】要素を設定する
' 【引 数】なし
' 【戻 り 値】なし
'-------------------------------------------------------------------------------
Private Sub addItem(ByVal id As String, ByVal name As String)
Dim bean As New BaseBean
Call bean.init(id, name)
items.Add Item:=bean
End Sub
Public Sub test()
Dim beanAggr As New BaseAggregate
Dim bean As New BaseBean
Dim it As New IIterator
Set it = beanAggr.IAggregate_iterator
Do While (it.hasNext)
Set bean = it.nextItem
Debug.Print bean.id
Debug.Print bean.name
Loop
Set bean = Nothing
Set it = Nothing
End Sub