梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
In Figure 8´5�察�the individual boxes represent a single assembly。 Each assembly serves
a unique purpose�此�
o Definitions�此�An assembly that contains all interfaces used by all of the other assemblies。
This represents a single assembly that changes very rarely and is a cornerstone of the
application。 Along with interfaces�察�you would add general utility classes that all assem
blies would reference。
o User�此�The main application that interacts with the interfaces of objects that are imple
mented in either the Kernel or Implementations assemblies。 The User assembly is
responsible for wiring together all of the types。
o Kernel�此�An assembly that defines the main functionality of the application and manipu
lates instances that implement interfaces from the Definitions assembly。 The kernel
does not know where the interfaces are implemented�察�and it expects some other piece of
code to know where the implementations are。
o Implementations�此�An assembly that contains the implementations of the interfaces that
the kernel manipulates。 The programmer may create a single implementation assembly
or multiple assemblies。 The implementations are only aware of the Definitions assembly�察 �
they are unaware of the Kernel assembly。
Building a plete Application
All of the code illustrated thus far is related to the kernel�察�and it would seem that our applica
tion is plete。 In reality�察�the kernel has done nothing other than organize and manipulate
the rooms。 The kernel has not defined any implementations for a particular room。 Now let¨s
see how to define some rooms and use the rooms in the context of the kernel。
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