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


to labor to find the bug。 A debugger has its uses; but when writing good tests in many scenarios; you will rarely
need to use it。 
And speaking of writing tests; as I noted in Chapter 6; in this book; I could introduce a testing framework
like NUnit (http://nunit。org) or Microsoft Visual Studio Team System (http:// 
msdn2。microsoft。/en…us/vstudio/default。aspx)。 When you are writing production code; you
will probably use such a testing framework。 Testing frameworks do not help you to write your tests; but rather
help you by providing support code to generate errors; log problems; and indicate progress of the tests。 Do
not get misled by tools that say they can write the tests for you。 No tool can write your tests; because that
would imply the tool understands the context of your code。 And since such a tool does not yet exist; you will
need to write your own tests。 
Implementing Room Groupings
Room groupings are collections of rooms that fall into a specific organization。 The idea behind
a grouping is to perform group operations without having to explicitly examine a room before
performing an operation。 For example; in the case of the museum; we don’t need to figure out
whether a room is public or private each time a global operation is being performed。 
The organization of the collection is that there can be multiple room groupings that are
linked together; and within a grouping; there are multiple rooms that can be grouped together。
The linked list structure has two levels and is coded as follows (in LibLightingSystem):
Class RoomGrouping
Inherits BaseLinkedListItem 
Public Rooms As Room 
Public Description As String 
End Class 
Class Room
Inherits BaseLinkedListItem 
Public ObjRoom As IRoom 
End Class 
…………………………………………………………Page 235……………………………………………………………
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 213 
The class declaration Room represents an individual room。 But notice how it derives from
BaseLinkedListItem; which seems to imply that Room is actually many rooms。 This is part of the
linked list implementation; it is like a chain; where the chain is created by individual links。 
The RoomGrouping class has two data members: Rooms; which represents the list of rooms in
the grouping; and Description; which represents an easy…to…understand description of the
grouping。 The single data member for Room is a reference to an IRoom interface instance。
The room groupings are managed by the LightingController class。 An initial implemen
tation of LightingController is as follows: 
Public Class LightingController
Private _roomGroupings As BaseLinkedListItem = New RoomGrouping() 
End Class 
When dealing with linked lists; you have a problem: which is the first element of a list? When
you use arrays; an empty list of arrays is an array with no references。 But there is an explicit
array object。 Using the linked list; an empty linked list is a list that does not exist。 Thus; when
you want to create a list; you need a room。 In LightingController; the first element is an instance
of RoomGrouping; which is not a room grouping; but serves as a placeholder。 To insert a room
grouping; you could simply use this code: 
_roomGroupings。Insert(NewRoomGroup()) 
Without the placeholder; you would need to write the following code whenever you
wanted to add an element into the list。 
If _roomGroupings Is Nothing Then 
_roomGroupings = NewRoomGroup() 
Else
_roomGroupings。Insert(NewRoomGroup()) 
End If 
The code that uses the placeholder is shorter and simpler; however; it also requires a dangling
instance of RoomGrouping that has no real value。 I chose the dangling approach because I am
making the decision that a room grouping with no identifier is the default room grouping。 
Adding a Room Grouping 
The following code adds a room grouping (added to the class LightingController)。 
Public Function AddRoomGrouping(ByVal description As String) As Object 
Dim grouping As RoomGrouping = New RoomGrouping() _ 
With { _ 
。Description = description; _ 
。Rooms = Nothing 
} 
_roomGroupings。Insert(grouping) 
Return grouping 
End Function
…………………………………………………………Page 236……………………………………………………………
214 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 
To add a new room grouping; you instantiate RoomGrouping; assign the data members; and
then call the method _roomGroupings。Insert() to insert the new room grouping into the linked list。 
Let’s look at the technique for assigning data members; called object initialization。 In
previous examples; when an object was instantiated and we wanted to assign default values;
we would create a constructor with the appropriate parameters。 However; another way is to instan
tiate the object and define a block that assigns the appropriate data members or properties。 In the
case of RoomGrouping; there are two publicly defined data members: Description and Rooms:
。Description = description; _ 
。Rooms = Nothing 
The Description and Rooms data members have assign access; which is important as this
technique o
小说推荐
返回首页返回目录