梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
problem�察�but solves a problem relating to building the application。
Another purpose of this chapter is to give you more experience with developing ponent
oriented code。 In particular�察�you¨ll learn how to develop a kernel。 Developing a kernel demon
strates the power and flexibility of the ponent´oriented development approach。 You can
build a working system even though you might not know what all of the implementations are
ahead of time。 This type of development makes it possible to modularize development�察�in that
individual teams are responsible for certain interfaces。 Then once the pieces have been imple
mented�察�they are fitted together like a jigsaw puzzle。 Of course�察�interfaces and ponents do
not guarantee success�察�but they do ensure one team does not need to wait for another team to
finish its code。
To demonstrate all of these concepts�察�we¨ll build an application that controls lighting in
buildings�察�using the kernel programming approach。
Understanding Kernels
Imagine you are the owner of a house or mercial building�察�and you want to reduce your
electricity bill。 One way of doing that is to automate the lighting system of your building so that
lights are on when they need to be。 What is unique about this system is that you have a controller
and the devices that are controlled。
The controller controls devices that it does not know about ahead of time�察�but fulfills a
contract。 The lighting system is a controller�察�programmatically called a kernel�察�because the
rooms that it controls are not known initially。 The rooms are known when the controller is used
to manage the lighting system。 The kernel programming approach is one where a core of func
tionality is developed。 The core functionality by itself does not work�察�because it relies on other
pieces to perform certain tasks。 In programming terms�察�this is called developing ponents
that use interfaces and implementations。
197
´´´´´´´´´´´´´´´´´´´´´´Page 220´´´´´´´´´´´´´´´´´´´´´´´
198 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
ponents are implemented at the technical level using interfaces and classes that subclass
the interfaces。 The interface represents an idea�察�and the classes represent the implementation
of an idea。 A class could implement multiple interfaces�察�where each interface represents a
unique characteristic of the class implementation。 Ideas and interfaces also represent contracts
or standards。 The kernel defines a standard�察�and the ponent has the responsibility of imple
menting that standard。
Implementing a kernel is like being a coach。 A coach thinks of where to place players in the
field and develops strategies that the players need to implement。 But on game day�察�the players
will do what they think is best�察�and the coach is powerless。 A coach can teach the players�察�but
whether they apply that knowledge is up to the individual players。
In programming terms�察�the kernel is the coach�察�and the external implementations to
manipulate are the players。 When designing interfaces that other pieces of functionality will
implement�察�you cannot watch over the shoulders of the programmers and make sure they do
things correctly。 You need to trust�察�but you also need to implement a mode of programming
where you assume the programmers are going to do things incorrectly。 This has nothing to do
with the personal abilities of the other programmers。 It has to do with the ability to make sure
your kernel continues functioning�察�even when someone makes a mistake。
Remember that when implementing a kernel�察�you are implementing a controller�察�and you
are devising the strategy of the application。 You are just not doing all of the implementations。
When you write production code�察�and you have been put in charge of writing a kernel�察�count
yourself lucky。 But remember with the job of building a kernel es great responsibility。 If
your kernel is buggy or badly designed�察�then the external implementations will also be buggy
and potentially badly designed。 The kernel is the rock and foundation of the application。
Organizing the Lighting Application
For the moment�察�imagine we are not developing software�察�but actually building a house。 And
in this house�察�we are going to add a central lighting system。 The make of light bulbs�察�lamps�察�and
lighting controller are not identical。 This is interesting in that multiple panies make prod
ucts that work with each other¨s device�察�because all panies adhere to a particular standard。
The process of standardization is all around us�察�and is as simple as the electricity that es
out of our electrical outlets。 However�察�what is a standard in one country is not necessarily the
standard in another country。 Different countries will have different standard voltages。 In terms
of software�察�the kernel represents a standard that allows the integration of ponents。
The lighting application will include these features�此 �
o A room where the light can be controlled using either a simple on/off switch or a gradual
light´intensity mechanism。
o The controller represents a building�察�and the rooms within the building can be grouped�察 �
making it simpler to perform group operations。
o Rooms are associated with identifiers that allow each room to be isolated individually。
o Rooms can be associated with a set of attributes that indicate the behavior they do and
do not support。
´´´´´´´´´´´´´´´´´´´´´´Page 221´´´´´´´´´´´´´´´´´´´´´´´
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 199
The source code will implement the controller as a library project named LibLightingSystem。
This library project will also define the interfaces that the ponents will implement。 To
demonstrate building a plete working application�察�two other projects implement the inter
faces and represent ponents�此�Museum and Home。 The key characteristics of a museum are that
some rooms are made dark at night and are pletely managed by the controller�察�while other
rooms are controlled individually in the room itself and also can be managed by the controller。
The key characteristics of a home are that all of the rooms are individually controlled�察�but they
have sensors that allow automatic control。 Some of the lighting associated with the house depends
on certain prerequisites being fulfilled。 For example�察�night´lights might turn on depending on
the time of year�察�or room lights might be turned on and off automatically when no one is home。
As usual�察�we will use a testing console application�察�named TestLightingSystem�察�to test the three
assemblies ��it is the startup project��。 Figure 8´1 shows the project structure。
Figure 8´1。 Lighting system application project structure
Building the Kernel
The lighting system is a two´part implementation。 The first part is that we must write code that
works properly。 The second part is to integrate code that someone else has written and make
sure that if that code does not work properly for some reason�察�those problems will not affect the
code that we wrote。
This application is plicated by the fact that we are dealing with the unknown。 In the
previous examples�察�we had control of