VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ)-µÚ59²¿·Ö
°´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ£¬°´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ£¬°´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿£¡
¡ª¡ª¡ª¡ªÎ´ÔĶÁÍꣿ¼ÓÈëÊéÇ©ÒѱãÏ´μÌÐøÔĶÁ£¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡use¡¡of¡¡placeholder¡¡interfaces¡¡is¡¡very¡¡important¡¡in¡¡kernel¡¡design¡£¡¡Placeholders¡¡estab
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡lish¡¡that¡¡a¡¡type¡¡wants¡¡to¡¡be¡¡part¡¡of¡¡a¡¡grouping¡£¡¡The¡¡kernel¡¡can¡¡use¡¡that¡¡grouping¡¡to¡¡define¡¡a¡¡list¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡of¡¡elements¡¡that¡¡are¡¡all¡¡similar¡£¡¡It¡¡is¡¡like¡¡knowing¡¡the¡¡age¡¡of¡¡people¡¡to¡¡determine¡¡whether¡¡they¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡are¡¡eligible¡¡to¡¡drive¡£¡¡The¡¡age¡¡does¡¡not¡¡indicate¡¡the¡¡sex¡¡or¡¡intelligence£»¡¡nor¡¡if¡¡they¡¡are¡¡good¡¡or¡¡bad¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡223¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡201¡¡
drivers¡£¡¡The¡¡age¡¡is¡¡a¡¡placeholder¡¡that¡¡says£»¡¡¡°Yes¡¡you¡¡are¡¡part¡¡of¡¡a¡¡grouping¡¡that¡¡is¡¡allowed¡¡to¡¡take¡¡¡¡
a¡¡driving¡¡test¡¡to¡¡give¡¡you¡¡the¡¡right¡¡to¡¡drive¡£¡±¡¡
¡¡¡¡¡¡¡¡¡¡In¡¡the¡¡case¡¡of¡¡our¡¡lighting¡¡control£»¡¡defining¡¡the¡¡¡¡IRoom¡¡placeholder¡¡interface¡¡is¡¡saying¡¡that¡¡¡¡
whatever¡¡instance¡¡is¡¡associated¡¡with¡¡IRoom¡¡is¡¡indicating¡¡its¡¡interest¡¡in¡¡being¡¡part¡¡of¡¡the¡¡lighting¡¡¡¡
controller¡¡kernel¡£¡¡When¡¡you¡¡have¡¡identified¡¡a¡¡type¡¡using¡¡a¡¡placeholder¡¡interface£»¡¡you¡¡are¡¡saying¡¡¡¡
your¡¡type¡¡can¡¡be¡¡used¡¡in¡¡a¡¡certain¡¡context¡£¡¡The¡¡context¡¡is¡¡determined¡¡by¡¡the¡¡placeholder¡¡¡¡
interface¡£¡¡
Defining¡¡the¡¡INoRemoteControlRoom¡¡Interface¡¡
Although¡¡the¡¡purpose¡¡of¡¡the¡¡lighting¡¡system¡¡is¡¡to¡¡control¡¡the¡¡lighting£»¡¡some¡¡rooms¡¡should¡¡not¡¡¡¡
be¡¡controlled¡¡by¡¡the¡¡system¡£¡¡Perhaps¡¡the¡¡room¡¡is¡¡private£»¡¡or¡¡controlling¡¡its¡¡lighting¡¡would¡¡cause¡¡¡¡
problems¡£¡¡
¡¡¡¡¡¡¡¡¡¡For¡¡example£»¡¡should¡¡a¡¡bedroom¡¡in¡¡a¡¡house¡¡be¡¡controlled¡¡by¡¡the¡¡lighting¡¡controller£¿¡¡If¡¡the¡¡¡¡
lighting¡¡controller¡¡controls¡¡the¡¡lighting¡¡in¡¡the¡¡bedroom£»¡¡it¡¡might¡¡turn¡¡off¡¡the¡¡lights¡¡while¡¡a¡¡person¡¡is¡¡¡¡
reading¡£¡¡Or¡¡maybe¡¡it¡¡will¡¡turn¡¡on¡¡the¡¡lights¡¡when¡¡the¡¡person¡¡has¡¡decided¡¡to¡¡sleep¡¡in¡£¡¡Of¡¡course£»¡¡¡¡
the¡¡person¡¡could¡¡just¡¡switch¡¡the¡¡light¡¡on¡¡or¡¡off¡¡manually£»¡¡but¡¡that¡¡is¡¡disruptive¡£¡¡The¡¡inconve
nience¡¡of¡¡the¡¡controller¡¡getting¡¡it¡¡wrong¡¡outweighs¡¡the¡¡benefit¡¡of¡¡the¡¡controller¡¡getting¡¡it¡¡right£»¡¡¡¡
so¡¡the¡¡controller¡¡should¡¡not¡¡deal¡¡with¡¡this¡¡room¡£¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡definition¡¡of¡¡an¡¡interface¡¡that¡¡indicates¡¡that¡¡the¡¡controller¡¡should¡¡do¡¡nothing¡¡is¡¡as¡¡¡¡
follows¡¡£¨in¡¡the¡¡controller¡¡library¡¡¡¡LibLightingSystem£©£º¡¡
Public¡¡Interface¡¡INoRemoteControlRoom¡¡
¡¡¡¡¡¡¡¡Inherits¡¡IRoom¡¡
End¡¡Interface¡¡
¡¡¡¡¡¡¡¡¡¡As¡¡you¡¡can¡¡see£»¡¡¡¡INoRemoteControlRoom¡¡lacks¡¡methods¡¡and¡¡properties£»¡¡like¡¡our¡¡placeholder¡¡¡¡
interface¡¡IRoom¡£¡¡However£»¡¡in¡¡this¡¡case£»¡¡there¡¡are¡¡no¡¡methods¡¡or¡¡properties¡¡because¡¡the¡¡kernel¡¡¡¡
system¡¡does¡¡not¡¡require¡¡them¡£¡¡The¡¡idea¡¡behind¡¡the¡¡INoRemoteControlRoom¡¡interface¡¡is¡¡to¡¡indicate¡¡¡¡
that¡¡the¡¡type¡¡implementing¡¡the¡¡interface¡¡is¡¡a¡¡room£»¡¡but¡¡a¡¡room¡¡that¡¡should¡¡not¡¡be¡¡managed¡¡by¡¡¡¡
the¡¡controller¡£¡¡Using¡¡the¡¡bedroom¡¡as¡¡an¡¡example£»¡¡the¡¡implementation¡¡is¡¡as¡¡follows¡¡£¨defined¡¡in¡¡¡¡
the¡¡Home¡¡project£©£º¡¡
¡¡¡¡¡¡¡¡Imports¡¡LibLightingSystem¡¡
¡¡¡¡¡¡¡¡Public¡¡Class¡¡Bedroom¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Implements¡¡INoRemoteControlRoom¡¡
¡¡¡¡¡¡¡¡End¡¡Class¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡definition¡¡of¡¡the¡¡bedroom¡¡allows¡¡the¡¡kernel¡¡to¡¡use¡¡an¡¡instance¡¡of¡¡a¡¡room£»¡¡as¡¡follows£º¡¡
Dim¡¡rooms¡¡As¡¡IRoom£¨£©=¡¡New¡¡IRoom£¨10£©¡¡£û¡¡£ý¡¡
rooms£¨0£©¡¡=¡¡New¡¡Bedroom£¨£©¡¡
¡¡¡£¡¡¡£¡¡¡£¡¡
If¡¡TypeOf£¨rooms£¨0£©£©¡¡Is¡¡INoRemoteControlRoom¡¡Then¡¡
¡¡¡¡¡¡¡¡'¡¡Take¡¡appropriate¡¡action¡¡
End¡¡If¡¡
¡¡¡¡¡¡¡¡¡¡This¡¡code¡¡creates¡¡an¡¡array¡¡of¡¡rooms¡¡and¡¡assigns¡¡the¡¡index¡¡0¡¡to¡¡an¡¡instance¡¡of¡¡Bedroom¡£¡¡The¡¡¡¡
If¡¡statement¡¡asks¡¡if¡¡the¡¡¡¡IRoom¡¡instance¡¡in¡¡index¡¡0¡¡is¡¡of¡¡type¡¡INoRemoteControlRoom¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡224¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
202¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡öNote¡¡¡¡Using¡¡placeholder¡¡interfaces¡¡and¡¡inheritance¡¡sets¡¡up¡¡a¡¡very¡¡powerful¡¡architecture¡¡that¡¡allows¡¡you¡¡to¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡create¡¡groupings¡£¡¡You¡¡can¡¡then¡¡filter¡¡individual¡¡instances¡¡based¡¡on¡¡refinements¡¡of¡¡the¡¡grouping¡£¡¡All¡¡of¡¡this¡¡is¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡possible¡¡in¡¡the¡¡Visual¡¡Basic¡¡language¡¡using¡¡TryCast£¨£©¡¡and¡¡Is£»¡¡which¡¡allow¡¡queries¡¡of¡¡subclassed¡¡types¡¡of¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡an¡¡instance¡£¡¡The¡¡queries¡¡are¡¡noninvasive¡¡and¡¡do¡¡not¡¡cause¡¡exceptions¡¡to¡¡be¡¡thrown¡£¡¡The¡¡queries¡¡give¡¡you¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ability¡¡to¡¡make¡¡decisions¡¡based¡¡on¡¡whether¡¡an¡¡instance¡¡would¡¡like¡¡to¡¡be¡¡associated¡¡with¡¡a¡¡particular¡¡grouping¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡based¡¡on¡¡an¡¡interface¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Defining¡¡the¡¡IRemoteControlRoom¡¡Interface¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Another¡¡type¡¡of¡¡room¡¡is¡¡one¡¡where¡¡the¡¡lighting¡¡is¡¡pletely¡¡managed¡¡by¡¡the¡¡controller¡£¡¡The¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡controller¡¡does¡¡not¡¡seek¡¡the¡¡input¡¡of¡¡the¡¡room¡¡and¡¡manages¡¡the¡¡lighting¡¡based¡¡on¡¡the¡¡logic¡¡that¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡seems¡¡appropriate¡¡to¡¡it¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡For¡¡example£»¡¡a¡¡public¡viewing¡¡area¡¡in¡¡a¡¡museum¡¡does¡¡not¡¡require¡¡light¡¡at¡¡certain¡¡times¡¡of¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡day¡£¡¡When¡¡the¡¡museum¡¡is¡¡closed¡¡and¡¡the¡¡cleaners¡¡are¡¡finished£»¡¡the¡¡lights¡¡can¡¡be¡¡turned¡¡off¡£¡¡When¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡museum¡¡is¡¡open£»¡¡the¡¡lights¡¡are¡¡turned¡¡on¡£¡¡This¡¡is¡¡a¡¡simple¡¡logic¡¡and¡¡can¡¡be¡¡pletely¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡managed¡¡by¡¡the¡¡controller¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡interface¡¡for¡¡the¡¡controlled¡¡room¡¡is¡¡defined¡¡as¡¡follows¡¡£¨in¡¡¡¡LibLightingSystem£©£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Interface¡¡IRemoteControlRoom¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Inherits¡¡IRoom¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ReadOnly¡¡Property¡¡LightLevel£¨£©¡¡As¡¡Double¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Sub¡¡LightSwitch£¨ByVal¡¡lightState¡¡As¡¡Boolean£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Sub¡¡DimLight£¨ByVal¡¡level¡¡As¡¡Double£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Interface¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡only¡¡input¡¡that¡¡IRemoteControlRoom¡¡provides¡¡is¡¡information¡¡about¡¡whether¡¡the¡¡light¡¡is¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡on£»¡¡off£»¡¡or¡¡at¡¡a¡¡certain¡¡level¡£¡¡This¡¡is¡¡through¡¡the¡¡¡¡LightLevel¡¡property¡£¡¡The¡¡¡¡LightLevel¡¡property¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡is¡¡read¡only£»¡¡because¡¡the¡¡controller¡¡and¡¡the¡¡light¡¡level¡¡might¡¡bee¡¡out¡¡of¡¡sync¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡For¡¡example£»¡¡suppose¡¡it¡¯s¡¡time¡¡for¡¡the¡¡museum¡¡to¡¡close£»¡¡and¡¡the¡¡lights¡¡are¡¡switched¡¡off¡¡in¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡public¡viewing¡¡area¡£¡¡But¡¡today£»¡¡the¡¡cleaners¡¡took¡¡a¡¡little¡¡while¡¡longer¡¡than¡¡usual¡£¡¡They¡¡turn¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡lights¡¡back¡¡on¡¡so¡¡that¡¡they¡¡can¡¡see¡¡what¡¡they¡¡are¡¡doing¡£¡¡The¡¡local¡¡device¡¡can¡¡do¡¡one¡¡of¡¡two¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡things£º¡¡allow¡¡the¡¡light¡¡to¡¡be¡¡turned¡¡on¡¡without¡¡the¡¡approval¡¡of¡¡the¡¡controller£»¡¡or¡¡not¡¡allow¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡light¡¡to¡¡be¡¡turned¡¡on£»¡¡requiring¡¡a¡¡controller¡¡intervention¡£¡¡The¡¡best¡¡approach¡¡is¡¡to¡¡allow¡¡a¡¡local¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡override¡¡and¡¡let¡¡the¡¡cleaners¡¡turn¡¡on¡¡the¡¡light¡£¡¡The¡¡LightLevel¡¡property¡¡is¡¡necessary¡¡so¡¡that¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡controller¡¡can¡¡verify¡¡if¡¡the¡¡state¡¡of¡¡the¡¡light¡¡is¡¡what¡¡the¡¡controller¡¡expects¡¡it¡¡to¡¡be¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡öNote¡¡¡¡When¡¡you¡¡are¡¡defining¡¡a¡¡kernel£»¡¡sometimes¡¡it¡¡is¡¡necessary¡¡to¡¡add¡¡functionality¡¡into¡¡an¡¡interface¡¡that¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡verifies¡¡the¡¡state¡¡of¡¡the¡¡implementation¡£¡¡Because¡¡the¡¡kernel¡¡is¡¡not¡¡in¡¡control¡¡of¡¡the¡¡implementation£»¡¡the¡¡kernel¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡should¡¡not¡¡assume¡¡the¡¡state£»¡¡as¡¡the¡¡state¡¡could¡¡change¡¡for¡¡some¡¡reason¡£¡¡In¡¡the¡¡case¡¡of¡¡the¡¡lighting¡¡system£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡change¡¡could¡¡be¡¡due¡¡to¡¡a¡¡cleaner¡¡turning¡¡on¡¡the¡¡light¡¡after¡¡it¡¡was¡¡turned¡¡off¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡225¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡203¡¡
¡¡¡¡¡¡¡¡¡¡¡¡The¡¡¡¡IRemoteControlRoom¡¡methods¡¡¡¡LightSwitch£¨£©¡¡and¡¡DimLight£¨£©¡¡turn¡¡the¡¡light¡¡on¡¡or¡¡off¡¡¡¡
and¡¡set¡¡the¡¡light¡¡to¡¡a¡¡certain¡¡level£»¡¡respectively¡£¡¡These¡¡methods¡¡are¡¡used¡¡to¡¡control¡¡the¡¡state¡¡of¡¡¡¡
the¡¡implementation¡£¡¡
Defining¡¡the¡¡ISensorRoom¡¡Interface¡¡
Another¡¡type¡¡of¡¡room¡¡is¡¡one¡¡that¡¡can¡¡be¡¡controlled¡¡under¡¡certain¡¡circumstances¡£¡¡Let¡¯s¡¡go¡¡back¡¡¡¡
to¡¡the¡¡cleaner¡¡example¡¡where¡¡the¡¡cleaner¡¡turned¡¡on¡¡the¡¡light¡£¡¡If¡¡the¡¡controller¡¡notices¡¡that¡¡the¡¡¡¡
light¡¡is¡¡on£»¡¡even¡¡though¡¡it¡¡was¡¡turned¡¡off£»¡¡should¡¡the¡¡controller¡¡turn¡¡off¡¡the¡¡light£¿¡¡You¡¡might¡¡say¡¡¡¡
sure£»¡¡the¡¡controller¡¡should¡¡turn¡¡off¡¡the¡¡light¡£¡¡However£»¡¡that¡¡is¡¡not¡¡pletely¡¡correct¡£¡¡Imagine¡¡¡¡
the¡¡situation¡¡where¡¡the¡¡cleaner¡¡turns¡¡on¡¡the¡¡light¡¡and¡¡the¡¡controller¡¡turns¡¡it¡¡off¡£¡¡The¡¡cleaner¡¡would¡¡¡¡
immediately¡¡turn¡¡the¡¡light¡¡back¡¡on£»¡¡and¡¡the¡¡controller¡¡would¡¡turn¡¡it¡¡off¡£¡¡The¡¡cleaner¡¡would¡¡tape¡¡¡¡
the¡¡light¡¡switch¡¡down¡¡so¡¡that¡¡a¡¡constant¡¡battle¡¡of¡¡the¡¡light¡¡going¡¡on¡¡and¡¡off¡¡ensues¡¡£¨because¡¡this¡¡¡¡
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