Dispatcher Pattern
出自VFP Wiki
Dispatcher Pattern
çÂÂå® RUN!PC å ÂæÂÂèÂÂç±æÂÂç¶Âå ÂçÂÂæÂÂ寫çÂÂ"ç±è»Âé«ÂÃ¥ÂÂ質檢é©Âè«ÂDesign PatternçÂÂæÂÂç¨"ä¸ÂæÂÂ,æÂÂ覺å¾Âç¨ VFP å°±å¯以å¾Âç°¡å®çÂÂä½Âå°,æÂÂ以就以 VFP 實ä½Â.
該æÂÂ以è¨Âç®ÂçÂÂ砧稠çºå¼Âå è¨Âç®ÂçÂÂ砧稠çÂÂæÂÂÃ¥ÂÂ,æÂÂéÂÂè¦Âä¾Â砧汽缸çÂÂccæ¸以åÂÂç§Â人/çÂÂæ¥Âç¨è»Âä¾Âå¤å®Âæ¶費çÂÂæ¨ÂæºÂ,ç §ä¸ÂèÂÂ寫æ³Â,å¾Âèªç¶就æÂÂç¨å°å¾Â夠If...Then...Else æ do case...endcase, å¯æ¯éÂÂ樣åÂÂç¨Âå¼Â碼就æÂÂè®Âå¾Âå¾Âé·å¾Âé·,èÂÂé£以ç¶Âè·. Ã¥ÂÂæ¯éÂÂ樣:
If cc < 500 money=900 else if cc < 600 money=1200 && ..... çÂÂ¥ endif endif
ç¨ do case çÂÂ話åÂÂæ¯éÂÂ樣
do case case between( 0, 500 ) money=900 case between( 501, 599 ) money=1200 && ... çÂÂ¥ endcase
æÂÂå ÂçÂÂ以ä¸Âå Dispatcher pattern 解決æ¤ä¸ÂÃ¥ÂÂé¡Â. å¨ VFP 裡é¢å¯以ç´æÂ¥çµÂÃ¥ÂÂè³ÂæÂÂ庫ä½Âæ´好çÂÂ解æ³Â
* * ITax.prg * DEFINE CLASS ITax as custom PROCEDURE Init() create cursor crTax( cc_lo I, cc_hi I, PrivateTax Y, BusinesTax Y ) insert into crTax values ( 0, 500, $ 1620, $ 900 ) insert into crTax values ( 501, 599, $ 2160, $ 1200 ) insert into crTax values ( 600, 1199, $ 4320, $ 2160 ) insert into crTax values ( 1200, 1799, $ 7120, $ 3060 ) insert into crTax values ( 1800, 2399, $11230, $ 6480 ) insert into crTax values ( 2400, 2999, $15210, $ 9900 ) insert into crTax values ( 3000, 4199, $28220, $16380 ) insert into crTax values ( 4200, 5399, $46170, $24300 ) insert into crTax values ( 5400, 6599, $69690, $33660 ) insert into crTax values ( 6600, 7799, $117000, $44460 ) insert into crTax values ( 7800, 9999999, $117000, $44460 ) ENDPROC PROCEDURE Destroy() use in crTax ENDPROC PROCEDURE GetTax( cc, theKind ) local ly_result local lc_oldalias lc_oldalias=alias() select("crTax") go top locate for between( cc, crTax.cc_lo, crTax.cc_hi ) if found() ly_result=crTax.&theKind else ly_result=0 endif select( lc_oldalias ) return ly_result ENDPROC PROCEDURE GetPrivateTax( cc ) return this.GetTax( cc, "PrivateTax" ) ENDPROC PROCEDURE GetBusinessTax( cc ) return this.GetTax( cc, "BusinesTax" ) ENDPROC ENDDEFINE
* * test_itax.prg * LOCAL lo_obj set procedure to itax.prg lo_obj=createobject("ITax") ? lo_obj.GetPrivateTax( 1000 ) ? lo_obj.GetBusinessTax( 2000 )
ç§,éÂÂ樣ä¸Âæ¯簡å®å¤ÂäºÂÃ¥ÂÂ?? å¨éÂÂ裡æÂÂç£Âå¼ÂçÂÂä¸Âä¸Â,å¸ÂæÂÂ大家å¦ÂæÂÂæÂÂæ´好çÂÂ解泠习post ä¸Âä¾Â,è®Â大家è§Âæ©ä¸Â丠^_^