ÓÑÇéÌáʾ£ºÈç¹û±¾ÍøÒ³´ò¿ªÌ«Âý»òÏÔʾ²»ÍêÕû£¬Çë³¢ÊÔÊó±êÓÒ¼ü¡°Ë¢Ð¡±±¾ÍøÒ³£¡
VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ)-µÚ75²¿·Ö
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿! Èç¹û±¾ÊéûÓÐÔĶÁÍ꣬ÏëÏ´μÌÐø½Ó×ÅÔĶÁ£¬¿ÉʹÓÃÉÏ·½ "Êղص½ÎÒµÄä¯ÀÀÆ÷" ¹¦ÄÜ ºÍ "¼ÓÈëÊéÇ©" ¹¦ÄÜ£¡
types¡¡as¡¡general¡¡things¡£¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡following¡¡code¡¡demonstrates¡¡how¡¡to¡¡use¡¡GenericsContainer¡¡with¡¡MyType¡£¡¡
Dim¡¡container¡¡As¡¡GenericsContainer£¨Of¡¡MyType£©¡¡=¡¡_¡¡
¡¡¡¡¡¡¡¡New¡¡GenericsContainer£¨Of¡¡MyType£©£¨New¡¡MyType£¨£©£©¡¡
container¡£Managed¡£Method£¨£©¡¡
¡¡¡¡¡¡¡¡¡¡When¡¡instantiating¡¡GenericsContainer£»¡¡notice¡¡how¡¡the¡¡identifier¡¡ManagedType¡¡must¡¡be¡¡¡¡
replaced¡¡with¡¡an¡¡identifier¡¡that¡¡represents¡¡an¡¡already¡¡existing¡¡type¡£¡¡This¡¡identifier¡¡replacement¡¡¡¡
results¡¡in¡¡a¡¡new¡¡and¡¡unique¡¡type¡¡£¨also¡¡called¡¡a¡¡concrete¡¡type£©¡£¡¡The¡¡advantage¡¡of¡¡¡¡generics¡¡is¡¡¡¡
that¡¡when¡¡you¡¡provide¡¡a¡¡concrete¡¡type£»¡¡you¡¡don¡¯t¡¡need¡¡to¡¡check¡¡to¡¡make¡¡sure¡¡that¡¡everything¡¡is¡¡¡¡
correct¡£¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡¡¡runtime¡¡will¡¡generate¡¡a¡¡type¡¡that¡¡has¡¡an¡¡intent¡¡similar¡¡to¡¡the¡¡following¡¡code¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡310¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
288¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡1¡¡1¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡OU¡¡T¡¡¡¡¡¡¡£¡¡N¡¡E¡¡T¡¡¡¡G¡¡E¡¡N¡¡E¡¡R¡¡I¡¡CS¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Class¡¡GenericsContainerMyType¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡_managed¡¡As¡¡MyType¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Sub¡¡New£¨ByVal¡¡toManage¡¡As¡¡MyType£©¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_managed¡¡=¡¡toManage¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Sub¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡ReadOnly¡¡Property¡¡Managed£¨£©¡¡As¡¡MyType¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Get¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡_managed¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Get¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Property¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Class¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Visual¡¡Basic¡¡piles¡¡a¡¡¡¡generics¡¡type¡¡as¡¡an¡¡inplete¡¡type¡£¡¡When¡¡the¡¡inplete¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡type¡¡is¡¡concretized£»¡¡¡¡creates¡¡a¡¡brand¡new¡¡type£»¡¡and¡¡does¡¡this¡¡without¡¡requiring¡¡the¡¡devel
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡oper¡¡to¡¡do¡¡anything¡¡in¡¡particular¡£¡¡This¡¡means¡¡that¡¡if¡¡you¡¡use¡¡GenericsContainer¡¡with¡¡15¡¡different¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡types£»¡¡¡¡will¡¡generate¡¡15¡¡definitions¡¡of¡¡GenericsContainer¡¡while¡¡the¡¡program¡¡is¡¡executing¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ABSTRACTION¡¡AND¡¡¡¡GENERICS¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡With¡¡¡¡generics£»¡¡you¡¡can¡¡verify¡¡and¡¡ensure¡¡that¡¡everything¡¡is¡¡being¡¡said¡¡properly£»¡¡with¡¡the¡¡cost¡¡being¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡plexity¡£¡¡Consider¡¡the¡¡sentence¡¡that¡¡clearly¡¡described¡¡the¡¡duck¡£¡¡To¡¡produce¡¡it£»¡¡you¡¡need¡¡to¡¡have¡¡learned¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡additional¡¡rules¡¡regarding¡¡grammar¡£¡¡When¡¡using¡¡¡¡generics£»¡¡you¡¡need¡¡to¡¡master¡¡creating¡¡abstractions¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡generics¡¡are¡¡an¡¡abstraction¡£¡¡Just¡¡as¡¡interfaces¡¡are¡¡an¡¡abstraction¡¡of¡¡classes£»¡¡¡¡generics¡¡are¡¡an¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡abstraction¡¡above¡¡interfaces¡£¡¡Interfaces¡¡define¡¡an¡¡intention£»¡¡and¡¡¡¡generics¡¡define¡¡an¡¡abstract¡¡implemen
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡tation¡¡of¡¡an¡¡intention¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡What¡¡is¡¡challenging¡¡with¡¡¡¡generics¡¡is¡¡getting¡¡your¡¡thoughts¡¡together¡¡into¡¡an¡¡abstract¡¡intention¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡implementation¡£¡¡It¡¡is¡¡like¡¡writing¡¡a¡¡document¡ªyou¡¡write¡¡it¡¡once£»¡¡read¡¡it¡¡over£»¡¡rewrite¡¡it£»¡¡read¡¡it¡¡over£»¡¡and¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡rewrite¡¡it¡¡again¡£¡¡With¡¡¡¡generics£»¡¡you¡¡are¡¡gathering¡¡thoughts¡¡together¡¡into¡¡a¡¡general¡¡plan¡¡of¡¡action¡£¡¡This¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡is¡¡why¡¡some¡¡people¡¡are¡¡pletely¡¡confused¡¡and¡¡don¡¯t¡¡understand¡¡¡¡generics¡£¡¡Writing¡¡your¡¡own¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡generics¡¡code¡¡requires¡¡some¡¡forethought¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡Theory¡¡of¡¡a¡¡Server¡Side¡¡Spreadsheet¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡example¡¡in¡¡this¡¡chapter¡¡is¡¡a¡¡spreadsheet¡¡for¡¡security¡¡traders¡£¡¡When¡¡you¡¡trade¡¡securities¡ª¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡whether¡¡they¡¡are¡¡equities£»¡¡bonds£»¡¡options£»¡¡or¡¡futures¡ªyou¡¡will¡¡be¡¡confronted¡¡with¡¡information¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡overload¡£¡¡You¡¡might¡¡have¡¡seen¡¡pictures¡¡of¡¡traders¡¡with¡¡desks¡¡full¡¡of¡¡desktop¡¡monitors¡£¡¡A¡¡trader¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡might¡¡have¡¡seven¡¡to¡¡eight¡¡monitors¡¡displaying¡¡various¡¡bits¡¡of¡¡information¡£¡¡A¡¡trader¡¡is¡¡a¡¡very¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡specialized¡¡type¡¡of¡¡domain¡¡that¡¡requires¡¡its¡¡own¡¡ways¡¡of¡¡processing¡¡information¡£¡¡One¡¡aspect¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡that¡¡makes¡¡writing¡¡applications¡¡for¡¡traders¡¡difficult¡¡is¡¡that¡¡the¡¡nature¡¡of¡¡the¡¡data¡¡constantly¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡changes£»¡¡and¡¡types¡¡get¡¡more¡¡in¡¡the¡¡way¡¡than¡¡they¡¡help¡£¡¡As¡¡a¡¡result£»¡¡traders¡¡adore¡¡spreadsheets¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Spreadsheets¡¡are¡¡useful¡¡because¡¡they¡¡can¡¡process¡¡large¡¡amounts¡¡of¡¡information¡¡in¡¡a¡¡relatively¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ad¡¡hoc¡¡manner¡£¡¡However£»¡¡one¡¡downside¡¡to¡¡spreadsheets¡¡is¡¡that¡¡the¡¡processing¡¡time¡¡can¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡311¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡1¡¡1¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡O¡¡U¡¡T¡¡¡¡¡¡¡£¡¡N¡¡E¡¡T¡¡¡¡G¡¡E¡¡N¡¡E¡¡R¡¡I¡¡CS¡¡289¡¡
dramatically¡¡increase¡¡due¡¡to¡¡the¡¡constant¡¡pushing¡¡and¡¡pulling¡¡of¡¡the¡¡data¡¡to¡¡and¡¡from¡¡the¡¡¡¡
spreadsheet¡£¡¡To¡¡speed¡¡up¡¡processing£»¡¡we¡¡will¡¡define¡¡and¡¡implement¡¡a¡¡spreadsheet¡¡that¡¡has¡¡the¡¡¡¡
advantages¡¡of¡¡a¡¡traditional¡¡client¡side¡¡spreadsheet¡£¡¡
¡öNote¡¡¡¡The¡¡theory¡¡and¡¡solution¡¡presented¡¡here¡¡are¡¡specific¡¡to¡¡the¡¡domain¡¡of¡¡trading£»¡¡where¡¡the¡¡cost¡¡of¡¡hard
ware¡¡is¡¡well¡¡worth¡¡the¡¡ability¡¡to¡¡trade¡¡properly¡£¡¡Therefore£»¡¡specific¡¡design¡¡aspects¡¡assume¡¡that¡¡you¡¡have¡¡the¡¡¡¡
latest¡¡and¡¡greatest¡¡hardware¡£¡¡
¡¡¡¡¡¡¡¡¡¡An¡¡initial¡¡attempt¡¡at¡¡a¡¡spreadsheet¡¡would¡¡be¡¡the¡¡following¡¡code¡¡that¡¡uses¡¡¡¡generics£º¡¡
Class¡¡Spreadsheet¡¡
¡¡¡¡¡¡¡¡Public¡¡Cells¡¡As¡¡Func£¨Of¡¡Object£©£¨£»£©¡¡
¡¡¡¡¡¡¡¡Public¡¡State¡¡As¡¡Object£¨£»£©¡¡
¡¡¡¡¡¡¡¡Public¡¡Sub¡¡New£¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Cells¡¡=¡¡New¡¡Func£¨Of¡¡Object£©£¨10£»¡¡10£©¡¡£û£ý¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡State¡¡=¡¡New¡¡Object£¨10£»¡¡10£©¡¡£û£ý¡¡
¡¡¡¡¡¡¡¡End¡¡Sub¡¡
¡¡¡¡¡¡¡¡Public¡¡Sub¡¡Execute£¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡For¡¡col¡¡As¡¡Integer¡¡=¡¡0¡¡To¡¡Cells¡£GetLength£¨1£©¡¡1¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡For¡¡row¡¡As¡¡Integer¡¡=¡¡0¡¡To¡¡Cells¡£GetLength£¨0£©¡¡1¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£WriteLine£¨col¡¡&¡¡¡¨¡¡¡¨¡¡&¡¡¡¨¡¡¡¨¡¡&¡¡row£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡If¡¡Cells£¨row£»¡¡col£©¡¡IsNot¡¡Nothing¡¡Then¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡State£¨row£»¡¡col£©¡¡=¡¡Cells£¨row£»¡¡col£©£¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡If¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Next¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Next¡¡
¡¡¡¡¡¡¡¡End¡¡Sub¡¡
End¡¡Class¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡sample¡¡spreadsheet¡¡is¡¡defined¡¡using¡¡the¡¡data¡¡members¡¡Cells¡¡and¡¡State¡£¡¡Both¡¡data¡¡¡¡
members¡¡are¡¡arrays¡¡with¡¡two¡¡dimensions¡£¡¡The¡¡first¡¡dimension¡¡represents¡¡the¡¡rows£»¡¡and¡¡the¡¡¡¡
second¡¡dimension¡¡represents¡¡the¡¡columns¡£¡¡You¡¡could¡¡define¡¡as¡¡many¡¡dimensions¡¡as¡¡you¡¡wish£»¡¡¡¡
but¡¡for¡¡the¡¡scope¡¡of¡¡the¡¡server¡¡spreadsheet£»¡¡we¡¡take¡¡a¡¡two¡dimensional¡¡approach¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡¡¡Execute£¨£©¡¡method¡¡goes¡¡through¡¡the¡¡individual¡¡rows¡¡and¡¡columns¡¡of¡¡the¡¡Cells¡¡data¡¡¡¡
member£»¡¡calculates¡¡the¡¡state¡¡of¡¡the¡¡cell£»¡¡and¡¡assigns¡¡the¡¡state¡¡to¡¡the¡¡State¡¡data¡¡member¡£¡¡The¡¡¡¡
data¡¡member¡¡Cells¡¡represents¡¡a¡¡function¡¡that¡¡is¡¡executed¡¡to¡¡generate¡¡the¡¡result¡¡of¡¡a¡¡particular¡¡¡¡
cell¡¡that¡¡is¡¡assigned¡¡to¡¡the¡¡data¡¡member¡¡State¡£¡¡Both¡¡data¡¡members¡¡store¡¡and¡¡manipulate¡¡Objects£»¡¡¡¡
which¡¡makes¡¡the¡¡spreadsheet¡¡flexible¡£¡¡However£»¡¡a¡¡gain¡¡in¡¡one¡¡aspect¡¡means¡¡a¡¡reduction¡¡in¡¡¡¡
another¡¡aspect£»¡¡in¡¡this¡¡case£»¡¡the¡¡loss¡¡is¡¡in¡¡performance¡£¡¡But¡¡performance¡¡is¡¡what¡¡algorithmic¡¡¡¡
trading¡¡software¡¡cannot¡¡sacrifice£»¡¡and¡¡native¡¡types¡¡would¡¡be¡¡best¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡To¡¡make¡¡the¡¡spreadsheet¡¡perform¡¡as¡¡fast¡¡as¡¡it¡¡can£»¡¡we¡¡need¡¡to¡¡use¡¡fixed¡dimension¡¡arrays¡£¡¡¡¡
However£»¡¡with¡¡fixed¡dimension¡¡arrays£»¡¡we¡¡are¡¡moving¡¡away¡¡from¡¡a¡¡traditional¡¡object¡oriented¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡312¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
290¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡1¡¡1¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡OU¡¡T¡¡¡¡¡¡¡£¡¡N¡¡E¡¡T¡¡¡¡G¡¡E¡¡N¡¡E¡¡R¡¡I¡¡CS¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡approach¡£¡¡You¡¡could¡¡argue¡¡that¡¡spreadsheets¡¡are¡¡not¡¡object¡oriented¡¡at¡¡all¡¡and¡¡are¡¡a¡¡problem¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡with¡¡respect¡¡to¡¡programmability¡£¡¡I¡¡would¡¡agree¡¡with¡¡that¡¡ment£»¡¡but¡¡spreadsheets¡¡solve¡¡one¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡class¡¡of¡¡problems¡¡very¡¡elegantly¡£¡¡In¡¡the¡¡case¡¡of¡¡financial¡¡trading¡¡software£»¡¡they¡¡solve¡¡the¡¡problem¡¡of¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡managing¡¡very¡¡large¡¡amounts¡¡of¡¡data¡¡efficiently¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡öNote¡¡¡¡Object¡oriented¡¡code¡¡is¡¡maintainable¡¡and¡¡extendable¡£¡¡However£»¡¡object¡oriented¡¡code¡¡can¡¡be¡¡slow¡£¡¡I¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡have¡¡done¡¡tests¡¡where¡¡I¡¡found¡¡fixed¡dimension¡¡arrays¡¡perform¡¡two¡¡to¡¡three¡¡times¡¡faster¡¡than¡¡the¡¡equivalent¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡object¡oriented¡¡application¡£¡¡But¡¡performance¡¡is¡¡not¡¡always¡¡the¡¡primary¡¡consideration¡£¡¡Also£»¡¡fixed¡dimension¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡arrays¡¡will¡¡not¡¡always¡¡give¡¡you¡¡the¡¡desired¡¡performance¡¡boost£»¡¡because¡¡other¡¡parts¡¡of¡¡your¡¡code¡¡might¡¡be¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡much¡¡slower¡£¡¡Therefore£»¡¡generally£»¡¡you¡¡should¡¡not¡¡use¡¡fixed¡dimension¡¡arrays¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡Cells¡¡data¡¡member¡¡is¡¡a¡¡delegate£»¡¡or¡¡lambda¡¡expression£»¡¡that¡¡is¡¡defined¡¡using¡¡code¡¡similar¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡to¡¡the¡¡following¡£¡¡When¡¡we¡¡want¡¡to¡¡fill¡¡a¡¡cell£»¡¡we¡¡call¡¡the¡¡appropriate¡¡function¡¡on¡¡CellFactories£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡which¡¡in¡¡turn¡¡returns¡¡the¡¡lambda¡¡expression¡¡that¡¡represents¡¡the¡¡new¡¡value¡¡stored¡¡in¡¡the¡¡cell£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Module¡¡CellFactories¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Function¡¡DoAdd£¨ByVal¡¡cell1¡¡As¡¡Func£¨Of¡¡Object£©£»¡¡_¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ByVal¡¡cell2¡¡As¡¡Func£¨Of¡¡Object£©£©¡¡As¡¡Func£¨Of¡¡Object£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡Function£¨£©¡¡CType£¨cell1£¨£©£»¡¡Double£©¡¡£«¡¡CType£¨cell2£¨£©£»¡¡Double£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Function¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Function¡¡DoMultiply£¨ByVal¡¡cell1¡¡As¡¡Func£¨Of¡¡Object£©£»¡¡_¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ByVal¡¡cell2¡¡As¡¡Func£¨Of¡¡Object£©£©¡¡As¡¡Func£¨Of¡¡Object£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡Function£¨£©¡¡CType£¨cell1£¨£©£»¡¡Double£©¡¡*¡¡CType£¨cell2£¨£©£»¡¡Double£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Function¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡Function¡¡FixedValue£¨ByVal¡¡value¡¡As¡¡Object£©¡¡As¡¡Func£¨Of¡¡Object£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡Function£¨£©¡¡value¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Function¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Module¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡lambda¡¡expressions¡¡can¡¡be¡¡used¡¡to¡¡add¡¡two¡¡cells¡¡together£»¡¡multiply¡¡two¡¡cells¡¡together£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡or¡¡store¡¡a¡¡fixed¡¡value¡£¡¡With¡¡the¡¡lambda¡¡expressions¡¡and¡¡the¡¡spreadsheet£»¡¡you¡¡have¡¡two¡¡pieces¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡of¡¡source¡¡code¡¡that£»¡¡when¡¡bined£»¡¡have¡¡the¡¡ability¡¡to¡¡solve¡¡plicated¡¡problems¡£¡¡The¡¡key¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡idea¡¡that¡¡you¡¡need¡¡to¡¡take¡¡away¡¡is¡¡that¡¡the¡¡Spreadsheet¡¡class¡¡and¡¡the¡¡lambda¡¡expressions¡¡defined¡¡in¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CellFactories¡¡do¡¡not¡¡know¡¡about¡¡each¡¡other¡£¡¡The¡¡lambda¡¡expressions¡¡could¡¡be¡¡used¡¡in¡¡a¡¡context¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡other¡¡than¡¡a¡¡spreadsheet¡£¡¡The¡¡only¡¡requirement¡¡is¡¡that¡¡the¡¡function¡¡types¡¡and¡¡signatures¡¡match¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡sample¡¡spreadsheet¡¡that¡¡would¡¡be¡¡used¡¡to¡¡add¡¡and¡¡multiply¡¡some¡¡cells¡¡together¡¡would¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡be¡¡as¡¡follows£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡spreadsheet¡¡As¡¡Spreadsheet¡¡=¡¡New¡¡Spreadsheet£¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡spreadsheet¡£Cells£¨1£»¡¡0£©¡¡=¡¡CellFactories¡£FixedValue£¨10¡£0£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡spreadsheet¡£Cells£¨0£»¡¡1£©¡¡=¡¡CellFactories¡£FixedValue£¨10¡£0£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡313¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡1¡¡1¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡O¡¡U¡¡T¡¡¡¡¡¡¡£¡¡N¡¡E¡¡T¡¡¡¡G¡¡E¡¡N¡¡E¡¡R¡¡I¡¡CS¡¡291¡¡
spreadsheet¡£Cells£¨1£»¡¡2£©¡¡=¡¡¡¡_¡¡
¡¡¡¡¡¡¡¡CellFactories¡£DoAdd£¨spreadsheet¡£Cells£¨1£»¡¡0£©£»¡¡spreadsheet¡£Cells£¨0£»¡¡1£©£©¡¡
spreadsheet¡£Cells£¨2£»¡¡2£©¡¡=¡¡CellFactories¡£DoMultiply£¨spreadsheet¡£Cells£¨1£»¡¡2£©£»¡¡_¡¡
¡¡¡¡¡¡¡¡CellFactories¡£FixedValue£¨2¡£0£©£©¡¡
spreadsheet¡£Execute£¨£©¡¡
Console¡£WriteLine£¨¡¨Contents¡¡of¡¡£¨1£»¡¡2£©£º¡¡¡¨¡¡&¡¡spreadsheet¡£State£¨1£»¡¡2£©¡£ToString£¨£©£©¡¡
Console¡£WriteLine£¨¡¨Contents¡¡of¡¡£¨2£»¡¡2£©£º¡¡¡¨¡¡&¡¡spreadsheet¡£State£¨2£»¡¡2£©¡£ToString£¨£©£©¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡sample¡¡code¡¡illustrates¡¡a¡¡rudimentary¡¡example¡¡of¡¡a¡¡spreadsheet¡¡and¡¡how¡¡lambda¡¡¡¡
expressions¡¡can¡¡be¡¡used¡£¡¡In¡¡this¡¡chapter£»¡¡the¡¡focus¡¡will¡¡be¡¡on¡¡how¡¡to¡¡create¡¡a¡¡spreadsheet¡¡¡¡
implementation¡¡that¡¡is¡¡effective£»¡¡mostly¡¡object¡oriented£»¡¡and¡¡maintainable¡£¡¡
Architecting¡¡a¡¡Server¡Side¡¡Spreadsheet¡¡¡¡
To¡¡architect¡¡a¡¡server¡¡spreadsheet£»¡¡the¡¡following¡¡requirements¡¡must¡¡be¡¡met£º¡¡
¡¡¡¡¡¡¡¡¡¡o¡¡¡¡Performance£º¡¡Wherever¡¡possible£»¡¡the¡¡design¡¡should¡¡not¡¡sacrifice¡¡performance¡£¡¡
¡¡¡¡¡¡¡¡¡¡o¡¡¡¡Usability£º¡¡The¡¡server¡side¡¡spreadsheet¡¡must¡¡be
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿!
ÎÂÜ°Ìáʾ£º ο´Ð¡ËµµÄͬʱ·¢±íÆÀÂÛ£¬Ëµ³ö×Ô¼ºµÄ¿´·¨ºÍÆäËüС»ï°éÃÇ·ÖÏíÒ²²»´íŶ£¡·¢±íÊéÆÀ»¹¿ÉÒÔ»ñµÃ»ý·ÖºÍ¾Ñé½±Àø£¬ÈÏÕæдԴ´ÊéÆÀ ±»²ÉÄÉΪ¾«ÆÀ¿ÉÒÔ»ñµÃ´óÁ¿½ð±Ò¡¢»ý·ÖºÍ¾Ñé½±ÀøŶ£¡