嗔秤戻幣�哉膵�云利匈嬉蝕湊蛸賜�塋床四衲���萩晦編報炎嘔囚^泡仟 ̄云利匈�《超噌�殻窟�嵌虜隆輓麈觚翹瀘卉韮�仍仍�。� 烏御危列
浪慕利 卦指云慕朕村 厘議慕尺 厘議慕禰 TXT畠云和墮 序秘慕杏 紗秘慕禰

VB2008貫秘壇欺娼宥(PDF鯉塀哂猟井)-及82嫗

梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��




                             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 225 



    Public Function CreatePrivateRoom�┌� As IRoom 

        Return New PrivateRoom�┌� 

    End Function 



    Public Function CreatePublicRoom�┌� As IRoom 

        Return New PublicRoom�┌� 

    End Function 

End Module 



     The implementation has three methods�此�CreateBuilding�┌��察�CreatePrivateRoom�┌��察�and  

CreatePublicRoom�┌��察� The fact that CreatePrivateRoom�┌� and PrivateRoom�┌� have similar naming is  

purely coincidental。 The method could just as well have been called CreateNonControlledRoom�┌�。  

The CreatePrivateRoom�┌� and CreatePublicRoom�┌� methods are intended to define method  

identifiers that users can understand。 What is instantiated in the method must return an IRoom  

instance。 

     The CreateBuilding�┌� method is a builder method�察�and it returns a  LightingController  

instance。 It is fine to return a LightingController instance�察�because it is a globally defined type  

and can serve as a basis for the builder method。 In the implementation of the builder method�察 �

the room groupings and rooms are instantiated and added to the LightingController instance。  

This is the work that the builder should save the end user。 Additionally�察�by providing a builder  

method�察�you avoid creating museum structures that have glaring errors in their structures。  



*Note  Factory types serve to instantiate types and define builder methods�察�but can also be used to perform  

generic structural operations。 Let¨s say that in your museum there is a wing�察�which contains three public  

rooms and a private room。 You could define a builder method that creates a wing�察�and the wing is added to  

an already created building。 The general idea behind the factory type is to avoid errors and centralize repeated  

instantiations。 



Learning More About Private Classes and  

Object Initialization 



In this chapter�察�you learned how to apply interfaces�察�implementations�察�and ponents in a  

kernel´type situation。 This is very much the type of programming that you will encounter as  

you continue using Visual Basic。 Here�察�I will provide a few more details about using private  

classes and initializing objects with nested data types。 



Private Classes 



The RoomGrouping and Room classes are defined in the  LibLightingController project and are  

private to the library。 This is because RoomGrouping and Room are classes only  LightingController  

needs to support its functionality。 The declaration of each class is internal to the assembly�察 �

which is good�察�but it still means that some developers could use the classes within the kernel  

assembly for their own purposes。 Sometimes that is a desirable feature�察�sometimes it is not。  


´´´´´´´´´´´´´´´´´´´´´´Page 248´´´´´´´´´´´´´´´´´´´´´´´

226       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 



               In the case of  LightingController�察�another approach is to declare the classes in the  

          context of LightingController�察�as follows�此�



          Public Class LightingController  

              Private Class RoomGrouping 

              End Class 

              Private Class Room 

              End Class 

          End Class 



               The Room and RoomGrouping classes are declared within the class�察�and their declarations are  

          Private。 This means that only LightingController can instantiate and use the classes�察�and  

          there can never be the situation where another class will instantiate the types。 In the case of  

          LightingController�察�this would have been a better solution。 

               Private classes are also used in the factory context。 For example�察�imagine the situation  

          where you don¨t ever want anyone but the factory to instantiate a room。 A possible  IRoom decla

          ration and factory could be as follows�此�



          Public Module Factory 

              Private Class MyRoom �此�Implements IRoom  

              End Class 

              Public Function CreateMyRoom�┌� As IRoom  

                  Return New MyRoom�┌� 

              End Function 

          End Module 



               In this implementation of MyRoom�察�you can be sure that only Factory can ever instantiate  

          MyRoom�察�and you can always be sure that the only way to manipulate MyRoom is through the IRoom  

          interface。 All too often�察�developers bee lazy and instantiate types within the assembly�察�and  

          switch to the implementation type whenever the interface does not have the methods or prop

          erties that they want。 



          Object Initialization with Nested Data Types 



          In this chapter�察�you saw how to use object initialization to assign data members in lieu of a  

          constructor。 Object initialization also works using nested data types。 Consider the situation  

          where a type references another type。 Using object initialization�察�you can instantiate and  

          assign multiple levels of objects。 

               Suppose you had this source code�此�



              Class MyType  

                  Private _dataMember As Integer 

                   

                  Public Property DataMember�┌� As Integer 

                      Get  

                          Return _dataMember 

                      End Get 


´´´´´´´´´´´´´´´´´´´´´´Page 249´´´´´´´´´´´´´´´´´´´´´´´

                            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 227 



            Set ��ByVal value as Integer�� 

                _dataMember = value 

            End Set 

        End Property 

    End Class 

     

    Class EmbeddedMyType  

        Private _embedded As MyType 

         

        Public Property MyTypeProperty�┌� As MyType 

            Get 

                Return _embedded 

            End Get 

            Set ��ByVal value As MyType�� 

                _embedded = value 

            End Get 

        End Property 

    End Class 



     The type  EmbeddedMyType has a property that references MyType。 If you were to instantiate  

EmbeddedMyType�察�you would probably also want to instantiate and assign the property MyType。  

You can do that with object initialization�察�like this�此�



        Dim cls As EmbeddedMyType = _ 

            New EmbeddedMyType�┌� With �� _ 

                。MyTypeProperty = _ 

                    New MyType�┌� With ��。DataMember = 10���� 



The Important Stuff to Remember 



In this chapter�察�you learned about writing a kernel�察�using Visual Basic default properties�察�and  

the implementing enumeration functionality。 The main items to remember are as follows�此�



     o A kernel is a ponent´oriented architecture where you are not in control of certain  

       implementations。 ponents make it possible to modularize a development process  

       so that separate teams have their own tasks。 



     o Interfaces are contracts between modules�察�and you test against the interface�察�not the  

       implementation。 



     o Placeholder interfaces are used to make it simpler to group object instances。 



     o Default properties are structural�察�and thus they serve as a tool to help you acplish a  

       task quicker。 



     o Default properties make it possible for your type to behave like an array。 



     o To iterate something that does not support iteration�察�you add enumeration function

       ality�察�which implies implementing the IEnumerable and  IEnumerator interfaces。 


´´´´´´´´´´´´´´´´´´´´´´Page 250´´´´´´´´´´´´´´´´´´´´´´´

228       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 



           Some Things for You to Do 



           The following are some exercises that will help you apply the concepts y
卦指朕村 貧匯匈 和匯匈 指欺競何 壘��0�� 家��0��
隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
梁椣戻幣�� 梁心弌傍議揖扮窟燕得胎��傍竃徭失議心隈才凪万弌誌育断蛍�輌臆惨軼僑〃�燕慕得珊辛參資誼持蛍才将刮襲潜��範寔亟圻幹慕得 瓜寡追葎娼得辛參資誼寄楚署衛、持蛍才将刮襲潜填��