梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
´´´´´´´´´´´´´´´´´´´´´´Page 238´´´´´´´´´´´´´´´´´´´´´´´
216 CH AP T E R 8 * L E A R N IN G AB OU T CO M P O N E N TO R IE N T E D AR C HI TE CT U R E
other properties。 However�察�the default property is unique in that you use it like an array refer
ence。 You could use the default property as follows�此�
Dim foundHandle As Object = controller�─�description;��
In the example�察�the array index is a string and behaves more like a property bag。 However�察 �
if description were of type Integer�察�then the default property would behave like an index。
*Note Default properties are utility´based and best added to classes that manage collections。 In the case
of the LightingController�察�which manages a collection of room groupings�察�use of a default property is
appropriate。
To find a particular room grouping�察�you would use the methods or default property of
LightingController。 However�察�sometimes a user would like to work with all of the room group
ings that are available and iterate over them。
The previous indexer example and the FindRoomGrouping�┌� method returned an object。
When you iterate the room groupings�察�you don¨t want an object�察�because you don¨t know what
the object represents。 If you call the FindRoomGrouping�┌� method and you search based on a
description�察�the object that is returned is cross´referenced with the description。 However�察�if
you iterate using a numeric default property�察�the returned object means nothing to you other
than being associated with a specific index。 What you really want to know is the description of
each object。
You could define a default property with a numeric value�察�as in the following code�察�and
iterate the individual elements to obtain descriptions。
Default Public ReadOnly Property Item��ByVal index As Integer�� As String
Get
。 。 。
End Get
End Property
*Note A type can have multiple default property definitions�察�but each default property definition must have
different array parameters。
Suppose we implemented a numeric default property。 To iterate the individual room
groupings�察�we would need to use the following code。
For c1 As Integer = 0 To controller。Length 1
Dim description As String = controller��c1��
Next
´´´´´´´´´´´´´´´´´´´´´´Page 239´´´´´´´´´´´´´´´´´´´´´´´
C H AP TE R 8 * L E AR N IN G AB O U T CO M P O N E N T O R IE N TE D A R CH I TE C TU R E 217
This iteration code is acceptable and something that we could use�察�but it involves adding
the property Length to the LightingController class。 Another approach is to use For Each�察�as
follows�此�
For Each rg As RoomGrouping In controller。RoomGroupingIterator�┌�
' Do something with the rg
Console。WriteLine��rg。Description��
Next
The For Each syntax is simpler。 It doesn¨t matter that we¨ve lost the information about
which offset is which description�察�because that information is useless。 Remember we are dealing
with a linked list that can change its order however it pleases。 Thus�察�having a numeric identifier
is pletely meaningless。 The only reliable way to find a room grouping is to know its description
or hold a specific index for the collection。
*Note Unless you are absolutely sure that the collection you are manipulating does not move elements
around�察�holding an index as a unique description of the object can be dangerous�察�and potentially could corrupt
the state of an application。 In this chapter�察�I have illustrated two other techniques that can be used to refer
ence a particular object�此�a handle and a default property。
We¨ll e to the RoomGroupingIterator�┌� method in a bit�察�but first we need to see the
enumerable that it returns。
The LightingController class has no For Each functionality built in。 To give it this function
ality�察�you need to create a class that can be enumerated and a class that can be an enumerator�察 �
which is a class that can perform the iteration。 The plete implementation of a class that
can be enumerated for BaseLinkedListItem is as follows ��it is also the enumerator class�察�as
you¨ll see��。
Public Class LinkedListEnumerable
Implements IEnumerable�察�IEnumerator
Private _currNode As BaseLinkedListItem
Private _firstNode As BaseLinkedListItem
Public Sub New��ByVal linkedList As BaseLinkedListItem��
_currNode = linkedList
_firstNode = linkedList
End Sub
Public Function GetEnumerator�┌� As IEnumerator _
Implements IEnumerable。GetEnumerator
Return Me
End Function
´´´´´´´´´´´´´´´´´´´´´´Page 240´´´´´´´´´´´´´´´´´´´´´´´
218 CH AP T E R 8 * L E A R N IN G AB OU T CO M P O N E N TO R IE N T E D AR C HI TE CT U R E
Public ReadOnly Property Current�┌� As Object _
Implements IEnumerator。Current
Get
Return _currNode
End Get
End Property
Public Function MoveNext�┌� As Boolean _
Implements IEnumerator。MoveNext
If _currNode Is Nothing Then
Return False
End If
_currNode = _currNode。NextItem
If _currNode Is Nothing Then
Return False
Else
Return True
End If
End Function
Public Sub Reset�┌� _
Implements IEnumerator。Reset
_currNode = _firstNode
End Sub
End Class
A class that can be enumerated must fulfill two objectives�此�implement the IEnumerable
interface and implement the IEnumerator interface。 The IEnumerable class has a single method�察 �
GetEnumerator�┌��察�which is used to retrieve a class instance that implements the IEnumerator
interface。 The two interfaces are split because retrieving the iteration functionality and iter
ating the collection are two separate steps。 However�察�in the case of LinkedListEnumerable�察 �
these steps are bined into one class�察�and that is often the case。
The best way to explain how LinkedListEnumerable works is to go through the code and
explain what methods and properties are called。
1。 The code in the client application starts a For Each loop�察�and sets up a context where a
collection of elements is being iterated。
2。 The code calls the collection iterator。 In the example�察�this means calling the method
RoomGroupingIterator�┌�。
3。 RoomGroupingIterator�┌� returns an instance of LinkedListEnumerable�察�which is assigned
the linked list that will be iterated。
Public Function RoomGroupingIterator�┌� As IEnumerable
Return New LinkedListEnumerable��_roomGroupings��
End Function
´´´´´´´´´´´´´´´´´´´´´´Page 241´´´´´´´´´´´´´´´´´´´´´´´
C H AP TE R 8 * L E AR N IN G AB O U T CO M P O N E N T O R IE N TE D A R CH I TE C TU R E 219
4。 The method LinkedListEnumerable。GetEnumerator�┌� is called�察�and an instance of
IEnumerator is returned。
5。 The code calls the method LinkedListEnumerable。MoveNext�┌�。
6。 The