按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
The idea is to enable a developer to add functionality to the kernel without affecting the
code of the kernel。 The example that we will go through defines a couple of rooms in a museum
(the Museum project)。
■Note The implementation of the Home project is not discussed here; but it is available in this book’s down
loadable source code。
Defining Some Rooms
The rooms are defined in a separate assembly called Museum and are not part of the kernel。 The
following is an example of a room implementation。 Again; remember to include a reference to
LibLightingSystem (right…click References in Museum and select Add Reference then Projects
LibLightingSystem)。
…………………………………………………………Page 245……………………………………………………………
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 223
。 。 。
Imports LibLightingSystem
Friend Class PrivateRoom : Implements INoRemoteControlRoom
End Class
Friend Class PublicRoom : Implements ISensorRoom
Public ReadOnly Property IsPersonInRoom() As Boolean _
Implements ISensorRoom。IsPersonInRoom
Get
Return False
End Get
End Property
Private _lightLevel As Double
Public ReadOnly Property LightLevel() As Double _
Implements ISensorRoom。LightLevel
Get
Return _lightLevel
End Get
End Property
Public Sub LightSwitch(ByVal lightState As Boolean) _
Implements IRemoteControlRoom。LightSwitch
If lightState Then
_lightLevel = 1
Else
_lightLevel = 0
End If
End Sub
Public Sub DimLight(ByVal level As Double) _
Implements IRemoteControlRoom。DimLight
_lightLevel = level
End Sub
End Class
The two room declarations; PrivateRoom and PublicRoom; are both internal to the assembly。
Each room implements the interface that it deems appropriate。 PrivateRoom implements the
interface INoRemoteControlRoom; indicating that LightingController should leave the room alone。
PublicRoom implements ISensorRoom; indicating that it will tell the controller when a person is
in the room and allow itself to be controlled。 The implementation of PublicRoom is trivial and
frankly not that useful; but it illustrates the bare minimum of what needs to be implemented。
In a production environment; PublicRoom would have access to external devices such as a
heat sensor and lights。 The objective of PublicRoom would be to give and take signals from the
LightingController and take action。 It is not up to PublicRoom to ask whether or not a decision
…………………………………………………………Page 246……………………………………………………………
224 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
is correct。 For example; if LightingController indicated to turn the light off even though a
person is in the room; then PublicRoom would not ask why the light is being turned off。
■Note When you are designing a kernel…like architecture; the implementations are realizations of ideas and
should never question the controller。 The implementations might not be aware of a bigger picture and thus
might prevent an algorithm from functioning properly。 Of course; the exception to this rule is if the decision
would cause physical damage or cause the program to crash。 In that case; the implementation should throw
an exception; indicating that the decision is faulty。
Instantiating PublicRoom and PrivateRoom
As described in the previous chapter; when you are developing ponents; you want to separate
the interfaces from the implementations。 This gives you the flexibility to change the imple
mentation in an assembly without requiring the users of the assembly to repile their code。
To instantiate the implementations; you need a factory; and the museum with its PrivateRoom
and PublicRoom implementations is no different。 However; a builder method that assembles a
building of potential PrivateRoom and PublicRoom binations will be offered with the museum。
The builder method is useful because it predefines a canned building that has all the room
groupings and rooms properly added。
■Note Think of a builder method as a way of creating a predefined structure; thus saving users from having
to do that themselves。 A builder method is only a starting point; and you should be able to manipulate the
structure afterward for fine…tuning purposes。
The following is the implementation of the museum factory; which is added to the project
Museum。
Public Module FactoryRooms
Public Function CreateBuilding() As LightingController
Dim controller As New LightingController()
Dim publicAreas As Object = _
controller。AddRoomGrouping(〃public viewing areas〃)
Dim privateAreas As Object = _
controller。AddRoomGrouping(〃private viewing areas〃)
controller。AddRoomToGrouping(publicAreas; New PublicRoom())
controller。AddRoomToGrouping(privateAreas; New PrivateRoom())
Return controller
End Function
…………………………………………………………Page 247……………………………………………………………
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