友情提示:如果本网页打开太慢或显示不完整,请尝试鼠标右键“刷新”本网页!阅读过程发现任何错误请告诉我们,谢谢!! 报告错误
狗狗书籍 返回本书目录 我的书架 我的书签 TXT全本下载 进入书吧 加入书签

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

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




or methods in a shared method。 

      Getting back to the Node declaration; the shared data member RootNodes is used to define  

a root for the search tree。 As when instantiating a type; there is a constructor for the shared type  

that is called before the first time it is needed。 The shared constructor is like the previously  

defined constructor; except the Public keyword is replaced with Shared。 For the search tree  

case; it is used to initialize the tree and state。 

      We now have a plete definition of the Node class; with the following source code。 Take  

a moment to look it over and fit the pieces together。 



Public Class Node 

    Shared Sub New() 

        Dim montreal As Node = New Node(〃Montreal〃; 0; 0) 

        Dim newyork As Node = New Node(〃New York〃; 0; …3) 

        Dim miami As Node = New Node(〃Miami〃; …1; …11) 

        Dim toronto As Node = New Node(〃Toronto〃; …4; …1) 

        Dim houston As Node = New Node(〃Houston〃; …10; …9) 

        Dim losangeles As Node = New Node(〃Los Angeles〃; …17; …6) 


…………………………………………………………Page 120……………………………………………………………

98        CH AP T E R   4   ■    L E A R N IN G   AB OU T   D AT A  S TR U CT U R E S;   DE CI SI ON S;   A N D   L O OP S 



                  Dim seattle As Node = New Node(〃Seattle〃; …16; …1) 

                  montreal。Connections = New Node() {newyork; toronto; losangeles} 

                  newyork。Connections = New Node() {montreal; houston; miami} 

                  miami。Connections = New Node() {toronto; houston; newyork} 

                  toronto。Connections = New Node() {miami; seattle; montreal} 

                  houston。Connections = New Node() {miami; seattle; newyork} 

                  seattle。Connections = New Node() {toronto; houston; losangeles} 

                  losangeles。Connections = New Node() {montreal; seattle; houston} 

                  Node。RootNodes = New Node() {montreal; newyork; miami; toronto; houston; _ 

                                               losangeles; seattle} 



              End Sub 



              Public Sub New(ByVal city As String; ByVal X As Double; ByVal Y As Double) 

                  Me。CityName = city 

                  Me。X = X 

                  Me。Y = Y 

                  Me。Connections = Nothing 

              End Sub 



              Public CityName As String 

              Public Connections As Node() 

              Public Shared RootNodes As Node() 

              Public X As Double 

              Public Y As Double 

          End Class 



          Defining the Algorithm Test 



          The Node type is a self…contained type; meaning that the algorithm does not need to instantiate  

          the tree structure (the Node constructor does it for you)。 This is an example of good design;  

          because if you had to add more cities; the only changes required would be to Node itself。 Any  

          search algorithm that uses the Node type does not need to be changed。  



          ■Note  When you have the ability to create code that localizes changes without affecting other pieces of  

          code; it is called decoupling code。 You want to write code that is decoupled from other code; so that when  

          changes are made in one piece of code; other pieces of code continue functioning。 As you will experience  

          when developing code; decoupling of code is a daily struggle。  



               For illustrative purposes; let’s try a first stab at the search algorithm and see where things  

          take us。 We could start by defining the search class or start by defining the test that will test the  

          search class。 Let’s define the test first; because it allows us to figure out what shape the search  

          class should take: 


…………………………………………………………Page 121……………………………………………………………

                       CH AP T E R   4   ■    L E A R N I N G   A B OU T   D AT A  S TR U CT U R E S;   DE CI SI ON S;   A N D   L O OP S 99 



Public Sub TestSearch()  

  SearchAlgorithm。DepthFirstFindRoute(〃Montreal〃; 〃Seattle〃) 

End Sub 



     In the test; the search algorithm is called directly using  

SearchAlgorithm。DepthFirstFindRoute()。 Here; SearchAlgorithm is the name of the class;  

and DepthFirstFindRoute() is the name of the method。 The class name implies that this class  

will contain all search algorithm implementations。 This is wrong; because most likely; each  

search algorithm will require multiple methods; leading to a very large and plicated  

SearchAlgorithm class。 If this is the case; then maintaining the  SearchAlgorithm class will  

bee a nightmare。 

     A better solution would be to identify a single class as being a single implementation of a  

search algorithm。 Then for each class; we can define a mon method identifier that is used  

to find the route between two points。 Doing this results in the following modified test: 



Public Sub TestSearch()  

  DepthFirstSearch。FindRoute(〃Montreal〃; 〃Seattle〃) 

End Sub  



     Now the test implies that the class DepthFirstSearch has a shared method FindRoute()。  

This is acceptable; and if you were to implement  BreadthFirstSearch; the naming would be  

BreadthFirstSearch。FindRoute()。 However; there is another problem; which relates to multiple  

users being able to use the algorithm during the execution of a program。 Going back to the  

push…to…talk feature of a cell phone; the method  FindRoute() is shared and thus a shared  

resource。 If multiple users do use this algorithm; they will share the resource。 This could be  

problematic if you are storing temporary data in the data members of the DepthFirstSearch  

class。 Using a shared method could corrupt your found search path。 

     The more appropriate solution is to define the method  FindRoute() as a non…shared  

method; implying that DepthFirstSearch must be instantiated before we can call  FindRoute()。  

We should modify the test again as follows: 



Public Sub TestSearch()  

  Di
返回目录 上一页 下一页 回到顶部 0 0
未阅读完?加入书签已便下次继续阅读!
温馨提示: 温看小说的同时发表评论,说出自己的看法和其它小伙伴们分享也不错哦!发表书评还可以获得积分和经验奖励,认真写原创书评 被采纳为精评可以获得大量金币、积分和经验奖励哦!