嗔秤戻幣�哉膵�云利匈嬉蝕湊蛸賜�塋床四衲���萩晦編報炎嘔囚^泡仟 ̄云利匈��
VB2008貫秘壇欺娼宥(PDF鯉塀哂猟井)-及20何蛍
酔楯荷恬: 梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈 梓囚徒貧議 Enter 囚辛指欺云慕朕村匈 梓囚徒貧圭�鮗� ● 辛指欺云匈競何! 泌惚云慕短嗤堋響頼���誅卒亮茂�俊彭堋響��辛聞喘貧圭 "辺茄欺厘議箝誓匂" 孔嬬 才 "紗秘慕禰" 孔嬬��
Switching to a Class to Define a Node
To fix the chicken´and´egg problem�察�we need to use reference types instead of value types。 This
means we need to change the declaration of Node from a Structure to a Class�察�as follows�此�
Public Class Node
Public Shared RootNodes As Node�┌�
Public CityName As String
Public X As Double
Public Y As Double
Public Connections As Node�┌�
´´´´´´´´´´´´´´´´´´´´´´Page 117´´´´´´´´´´´´´´´´´´´´´´´
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 95
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
End Class
The change is a one´liner。 After the switch�察�if we executed the same assignment code as in
the previous section when Node was a value type�察�the data structure shown in Figure 4´12 would
be created。
Figure 4´12。 A valid state for the New York Node instance
Looking at the node structure in Figure 4´12�察�you can see that New York points to Montreal
and back again。 The infinite connection does not mean that you are using infinite resources。
Instead�察�it means one reference is being set to another�察�as illustrated in Figure 4´13。
Variable montreal points to heap
memory that contains the data for
There is a reference in newyork to
the city Montreal
montreal and vice versa
montreal newyork
Public CityName As String Public CityName As String
Public X As Double Public X As Double
Public Y As Double Public Y As Double
Public Connections As Node�┌� Public Connections As Node�┌�
newyork montreal
Array element is a reference to
the other heap memory
Figure 4´13。 Recursive assignment that seems like infinite resources
´´´´´´´´´´´´´´´´´´´´´´Page 118´´´´´´´´´´´´´´´´´´´´´´´
96 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
The apparent infinite resources are the cross´reference recursive assignment of two pieces
of heap memory。 It is fine to do this�察�and this ability is one of the reasons why people prefer
using reference types to value types。
Understanding Static Data Members and Methods
You¨ve seen how a constructor can be used to initialize the state of a particular instance of a
type。 Now we need to define a constructor for the tree structure shown in Figure 4´2。 A tree
implies a starting point�察�but the code for the flight connections does not imply a single starting
point。 Instead�察�we have the declaration of a number of variables where the identifier of each
variable is a city。
The problem with such a declaration is that�察�if you want to navigate a tree structure�察�you
need to know the individual names of the variables and navigate the tree structure of each vari
able。 This is not a plausible solution。 You want to create a single overall point from where all
other cities can be referenced。
The solution lies in using an array like that used for the data member Connections。 To address
the problem of providing a single access point�察�we declare a shared data member�察�as follows�此�
Public Class Node
Public Shared RootNodes As Node�┌�
Public CityName As String
Public X As Double
Public Y As Double
Public Connections As Node�┌�
Public Sub New��string city�察�double x�察�double y��
Me。CityName = city
Me。X = x
Me。Y = y
Me。Connections = Nothing
End Sub
End Class
The bold code shows the declaration of a data member with the modifier Shared to indi
cate that the data member is shared。 So�察�just what does shared mean in this context�拭�
Let¨s say that your family consists of your significant other and two kids。 One day�察�you
wander into the cell phone store and decide to buy four identical cell phones�察�as the store is
having a family deal。 When each phone is activated�察�the state of each cell phone will be unique。
Each person will have an individual number�察�address book�察�and so on。 In the analogy to objects�察 �
the cell phone represents a type�察�each person is an instance of the same type�察�but with a unique
state that represents an instantiation of the type。
Some cell phones have a feature called push´to´talk。 Essentially�察�you convert a cell phone
into a walkie´talkie。 After buying individual cell phones for the family�察�the push´to´talk feature is
activated for everyone in the family。 This means if one person is using the push´to´talk feature�察 �
all family members will hear the conversation。 The push´to´talk feature does not distinguish
between the cell phones�察�so if multiple people talk at the same time�察�so be it�察�you¨ll hear quite a
bit of noise。 Push´to´talk is a shared resource�察�not associated with a particular cell phone。 In the
same way�察�Shared refers to a shared resource that is not associated with a particular type instance。
´´´´´´´´´´´´´´´´´´´´´´Page 119´´´´´´´´´´´´´´´´´´´´´´´
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 97
When you associate Shared with a data member�察�as in the code example you just saw�察�you
are saying that�察�regardless of how many times you instantiate Node�察�there is always a single instance
of the data member RootNodes。 You don¨t even need to instantiate Node to access RootNodes。 Shared
methods are like shared data members�察�in that they are a shared resource and are not associated
with a particular object ��as illustrated by the Main�┌� method used to start an application��。
Figure 4´14 illustrates what you can and cannot do with shared and object instance data
members。
Class MySharedAndNonSharedClass Assignment allowed because shared
Public Shared value As Integer can reference shared
Public instanceValue As Integer
Public Shared Sub MyMethod�┌� Assignment not allowed because shared cannot
value = 10
instanceValue = 20 reference information that is not shared�察�unless the
End Sub containing type is instantiated with the New keyword
Public Sub MyInstanceMethod�┌�
value = 10 Assignment allowed because instance can reference shared
instanceValue = 20
End Sub
End Class Assignment allowed because
instance can reference instance
Class TestSharedVsNonShared Method call allowed because shared
Public Sub TestSimple�┌� means you do not need to instantiate
MySharedAndNonSharedClass。MyMethod�┌� the type using New keyword
MySharedAndNonSharedClass。MyInstanceMethod�┌�
Dim cls As MySharedAndNonSharedClass = New MySharedAndNonSharedClass�┌�
cls。MyInstanceMethod�┌�
End Sub
End Class Method call not allowed because method
can be called only when the type is
instantiated using New keyword
Figure 4´14。 Examples of shared and object instance data members
The general rule of thumb is that shared data members or methods can be accessed without
having to instantiate the type。 Also�察�don¨t attempt to reference object instance data members
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�此�
酔楯荷恬: 梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈 梓囚徒貧議 Enter 囚辛指欺云慕朕村匈 梓囚徒貧圭�鮗� ● 辛指欺云匈競何!
梁椣戻幣�� 梁心弌傍議揖扮窟燕得胎��傍竃徭失議心隈才凪万弌誌育断蛍�輌臆惨軼僑〃�燕慕得珊辛參資誼持蛍才将刮襲潜��範寔亟圻幹慕得 瓜寡追葎娼得辛參資誼寄楚署衛、持蛍才将刮襲潜填��