VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ)-µÚ30²¿·Ö
°´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ£¬°´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ£¬°´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿£¡
¡ª¡ª¡ª¡ªÎ´ÔĶÁÍꣿ¼ÓÈëÊéÇ©ÒѱãÏ´μÌÐøÔĶÁ£¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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£º¡¡
Publi