按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
battle is in milliseconds; the light remains on)。 A smarter approach would be to allow a timing
of the light。 But how much time—a quarter of an hour; a half hour; an hour?
Another approach is not to use a time interval; but to enhance the interface and allow the
controller to figure out the state。 This enhanced interface; called ISensorRoom; is defined as
follows (in LibLightingSystem):
Public Interface ISensorRoom
Inherits IRemoteControlRoom
ReadOnly Property IsPersonInRoom() As Boolean
End Interface
The ISensorRoom interface has a single property IsPersonInRoom; which is a Boolean prop
erty。 If the property has a value of True; then a person is in the room; otherwise; no person is in
the room。 How the implementation determines whether or not a person is in the room is not
the problem of the kernel。 The kernel assumes the implementation knows how to figure this
out。
■Note As a general rule of thumb; the kernel can municate with the implementation only via the inter
face。 The kernel should never assume a certain implementation of an interface。 The kernel should take the
approach that what it sees is what it gets。 Thus; if the kernel needs additional information; the interface
should be extended during design; or another interface should be implemented。 Of course; this does not mean
for every piece of state the interface should be extended。 Sometimes; you will need to define a specific inter
face; such as in the tax application example in the previous chapter ( ICanadianTaxEngine)。
Now that we’ve created the interfaces; we’re ready to implement the kernel。
Implementing the Kernel
In this example; the kernel will be a single class that contains all of the functionality of the
controller。 This definition means that the individual implementations; testing; and applica
tions will interact with a single class。
…………………………………………………………Page 226……………………………………………………………
204 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
Here is an example of implementing the light…dimming method using the
LightingController class (in LibLightingSystem):
Public Class LightingController
Public Sub DimLights(ByVal grouping As Object; ByVal level As Double)
End Sub
End Class
The user of LightingController would dim a light using this code:
Dim controller As LightingController = New LightingController()
Dim grouping As Object = Nothing
controller。DimLights(grouping; 0。50)
The user code instantiates the type LightingController directly and uses the method
DimLights() directly。 Using classes directly has the cost that the controller code cannot change
without affecting the users; as there is a tight coupling between the user code and the kernel。
And this is why I have been writing at length about using interfaces; ideas; and implementa
tions。 Yet; the controller appears to throw all of that out of the window。
The reason for using a class goes back to the previous chapter’s example and the interfaces
ITaxDeduction and ITaxIne。 That example had only a single implementation of each inter
face; and those implementations were not going to change。 As I explained in the previous chapter;
the interfaces could have been represented as classes。 The same logic applies with respect to
the controller。 The controller is not going to change much from a method and property signature
perspective; and there is going to be only a single implementation of the controller。 Therefore; an
interface is not necessary。 Using a class is pletely acceptable; and it’s the approach we’re
using in this chapter。 However; I’ll talk about when you might want to implement the kernel as
an interface a little later in the chapter; in the “Defining the Kernel As an Interface Instead of a
Class” section。
The controller represents a building that has the ability to organize rooms into groupings。
Based on the groupings; the controller can perform operations such as turning the lights on
or off; or setting lights to a specific dimness。 When each of these operations is executed; the
controller must respect the intentions of the individual rooms; by querying for a particular
interface as defined by the previous section。
The controller has two main responsibilities: calling the appropriate interface methods
and organizing the interface instances。 The organization of the instances involves using collec
tions; arrays; or a linked list。 We’ll use a linked list in this example。
Storing a Collection Using a Linked List
In the examples in previous chapters; we created a collection of objects using an array; like this:
Dim array As MyType() = New MyType(9) { }
array(0) = New MyType()
array(2) = New MyType()
This creates an array that can contain 10 elements at most (MyType(9))。 If you needed to store
20 elements; you would need to use the ReDim keyword with Preserve (as explained in Chapter 4)。
Another feature of an array is that you don’t need to assign the elements in a sequential manner。
The example assigns the first and third positions; with the second position being Nothing。
…………………………………………………………Page 227……………………………………………………………
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 205
Thus; some code that iterates the array will need to verify