按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
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 ha