《VB2008从入门到精通(PDF格式英文版)》第104章


The Description and Rooms data members have assign access; which is important as this
technique only works with properties that are not read…only。 To assign a data member or prop
erty; after the object instantiation; add the With keyword; and then within curly brackets; assign
each individual data member using a key/value pair; in this form: 
With { 。Key1 = value1; 。Key2 = value2 } 
The key represents the data member property to assign; and the value is the data that is
assigned to the data member or property。 In the form example; the properties Key1 and Key2
are set。 
Another technique of interest in the code to add a room grouping is the definition of a data
handle when passing information: 
Return grouping 
In the implementation of AddRoomGrouping(); the variable grouping is assigned an
instance of RoomGrouping。 The declaration of the RoomGrouping class limits its scope to the
LibLightingSystem assembly only; while the declaration of LightingController is public。 If the
method AddRoomGrouping() had attempted to return an instance of RoomGrouping; the piler
would have marked this as an error; because the scope is inconsistent。 Assuming for the moment
that you did want to return an instance of RoomGrouping; your only solution would be to declare
RoomGrouping as public。 The declaration change is the wrong solution; because RoomGrouping is
a class without declared methods (other than the base class methods) and has public data
members。 It is a class for a specific purpose and should not be shared。 
Declaring RoomGrouping as public is the wrong approach; so another solution is needed。
You could add a counter data member to the RoomGrouping declaration and return an Integer
value indicating the RoomGrouping instance you are referring to in the list。 However; that would
mean having access to the list somewhere; and then needing to iterate to find the appropriate
RoomGrouping instance。 
The solution is to declare the method as returning a type Object。 When you use Object;
you are defining that your method is giving you an object instance。 The caller may or may not
know what the instance type is; and in the case of AddRoomGrouping(); it doesn’t。 But that is fine;
because you; as the user; will consider the instance as a key that is managed by the class
LightingController。 In technical jargon; the object instance is a handle that you hold and pass
to some other ponent that knows what to do with it。 In the example; it means giving the
handle to LightingController because it knows that the handle is an instance of RoomGrouping。
…………………………………………………………Page 237……………………………………………………………
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 215 
■Note Handles were very popular in the C programming days and were consider pointers to memory。 The
caller did not know what the pointer pointed to; but kept using it when interacting with an API。 These days;
handles have lost significance as we have objects; generics; and other programming constructs。 However; at
times; handles are very useful。 They can help you to avoid the problem of having to expose the internal state
of your API; while not having to maintain an object hierarchy to watch which objects are being referenced。 
Finding a Room Grouping 
When a number of room groupings have been added; you will want to find a room grouping
with a particular description。 As room groupings are a doubly linked list; it means needing to
iterate the list; as follows (added to LightingController): 
Public Function FindRoomGrouping(ByVal description As String) As Object 
Dim curr As RoomGrouping = _roomGroupings。NextItem 
Do While curr IsNot Nothing 
If curr。Description。pareTo(description) = 0 Then 
Return curr 
End If 
curr = TryCast(curr。NextItem; RoomGrouping) 
Loop 
Return Nothing 
End Function 
In the iteration code; the iteration is similar to the code illustrated earlier in the “Storing a
Collection Using a Linked List” section。 The one difference is that the curr variable is of type
RoomGrouping; and because NextItem is of type BaseLinkedListItem; a type cast is necessary。
Then an iteration using a While loop is carried out; during each iteration; a test paring
curr。Description to the parameter description is made。 If an object is found; the handle to
the RoomGrouping is returned; and if nothing is found; Nothing is returned; indicating that the
RoomGrouping could not be found。 
This method would be used as follows: 
Dim foundHandle As Object = controller。FindRoomGrouping(〃description〃) 
Visual Basic has constructs that make it possible to convert the LightingController class
into a class that has array functionality。 The following method in LightingController declares
array…like functionality; which is called a default property 
Default Public ReadOnly Property Item(ByVal description As String) As Object
Get
Return FindRoomGrouping(description) 
End Get 
End Property 
A Visual Basic default property is defined like a property; except that the property is prefixed
with a Default keyword。 The default property is named Item by convention; and this name is
used by all collection classes for consistency。 The implementation of a property is just like
…………………………………………………………Page 238……………………………………………………………
216 CH AP T E R 8 ■ L E A R N
小说推荐
返回首页返回目录