小牛电子书 > 其他电子书 > VB2008从入门到精通(PDF格式英文版) >

第73章

VB2008从入门到精通(PDF格式英文版)-第73章

小说: VB2008从入门到精通(PDF格式英文版) 字数: 每页3500字

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




previous examples; we had control of every class; interface; and definition。 This time; we are  

not in control; and thus we need to use a defensive style of programming。 This means we need  

to write many tests and keep certain information private。  



Defining the Interfaces 



The core of the lighting controller is to control the lighting in a room that is part of the building。  

We can define and organize the rooms by using interfaces。 We need four interfaces:  



     o  IRoom: A placeholder interface for the idea of a room 



     o  INoRemoteControlRoom: An interface for rooms that should not be controlled by the  

        lighting controller 



     o  IRemoteControlRoom: An interface for rooms that should be pletely controlled by the  

        lighting controller 



     o  ISensorRoom: An interface for rooms whose control is based on state (whether or not a  

        person is in the room) 


…………………………………………………………Page 222……………………………………………………………

200       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 



                The interfaces for the rooms where lighting may be controlled; IRemoteControlRoom and  

           ISensorRoom; will depend on certain pieces of logic。 The interfaces will need to provide input  

           data and accept output data。 The logic might also seek input from other sources; such as time  

           of day or amount of sunlight outdoors。 It boils down to defining some type of logic that the kernel  

           implements。 This is key; and it relates to the children…and…parent issue。 While you accept your  

           children as being intelligent beings that can make decisions; at the end of the day; it is usually  

           the parent who makes the final decisions。 Likewise; while your kernel might accept input and  

           potential decisions; the kernel makes the final decisions。 



           Defining IRoom; a Placeholder Interface 



           For design purposes; the simplest and base idea is the room itself; which can be defined as  

           follows (in the controller library  LibLightingSystem):  



           Public Interface IRoom  

           End Interface 



                The interface does not have any methods or properties。 It is called a placeholder interface。  

          A placeholder type serves no other purpose than identifying that the implementation is of a  

           certain type。 Placeholder interfaces make it simpler to group objects that have certain capabilities。 

                Imagine defining objects without a placeholder interface; something like this: 



           Class Type1  

           End Class 

           Class Type2 

           End Class 



                Looking at Type1 and Type2; you cannot see any correlation between the two types; there  

           is no way to say that Type1 and Type2 have anything in mon。 (Well; technically there is a  

           correlation in that both types are derived from Object; but that type of correlation is like saying  

           that all people are humans。) Using a placeholder interface; Type1 and Type2 can be correlated;  

           as follows: 



           Class Type1  

              Implements IRoom 

           End Class 

           Class Type2 

              Implements IRoom 

           End Class 

           。 。 。 

           Dim rooms As IRoom() = New IRoom() {New Type1(); New Type2()} 



                Having Type1 and Type2 implement the  IRoom interface; which means do nothing other  

           than subclass IRoom; establishes a correlation between Type1 and Type2。 The correlation is that  

           both Type1 and Type2 are rooms。 We have no idea what kind of rooms; and we have no idea if  

           the rooms are in the same building。 We only know that they are rooms。 

                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 group

返回目录 上一页 下一页 回到顶部 2 2

你可能喜欢的