]]>Abstract Factory Patternhttp://www.aygfsteel.com/TiGERTiAN/archive/2007/04/26/113903.htmlTiGERTiANTiGERTiANThu, 26 Apr 2007 13:11:00 GMThttp://www.aygfsteel.com/TiGERTiAN/archive/2007/04/26/113903.htmlhttp://www.aygfsteel.com/TiGERTiAN/comments/113903.htmlhttp://www.aygfsteel.com/TiGERTiAN/archive/2007/04/26/113903.html#Feedback0http://www.aygfsteel.com/TiGERTiAN/comments/commentRss/113903.htmlhttp://www.aygfsteel.com/TiGERTiAN/services/trackbacks/113903.html--Gamma, E., Helm, R., Johnson, R., Vlissides, J. Design Patterns: Elements of Reusable Object-Oriented Software, Boston: Addison-Wesley, 1995, p.87
]]>Bridge Patternhttp://www.aygfsteel.com/TiGERTiAN/archive/2007/04/08/109226.htmlTiGERTiANTiGERTiANSun, 08 Apr 2007 06:27:00 GMThttp://www.aygfsteel.com/TiGERTiAN/archive/2007/04/08/109226.htmlhttp://www.aygfsteel.com/TiGERTiAN/comments/109226.htmlhttp://www.aygfsteel.com/TiGERTiAN/archive/2007/04/08/109226.html#Feedback2http://www.aygfsteel.com/TiGERTiAN/comments/commentRss/109226.htmlhttp://www.aygfsteel.com/TiGERTiAN/services/trackbacks/109226.html妗ユ帴妯″紡錛屽紩鐢?#8220;鍥涗漢甯殑璇?#8221;灝辨槸 “decouple an abstraction from its implementation so that the two can vary independently”錛屾娊璞′笌琛屼負(fù)鍒嗙銆?br> 璇翠釜渚嬪瓙 瀹㈡埛鎯崇敾鍥撅紝鐢諱袱縐嶅渾鍜岀煩褰紝姣忕鐭╁艦閮借璋冪敤鐗瑰埆鐨勫垝綰匡紙draw_a_line()錛夋柟娉曪紝姣忎竴縐嶅渾涔熸槸銆傚鏋滃儚涓婇潰閭f牱錛屾槸涓嶆槸鎰熻娣蜂貢浜?jiǎn)鐐瑰Q熺湅鐪嬩笅闈㈢殑鍛€?br> 榪欎釜鏄笉鏄垝鏈嶇偣錛熺殑紜紝鍥懼艦鍜岀敾娉曢兘瀛樺湪涓涓瀵瑰簲鐨勫叧緋伙紝鎴戜滑瀹屽叏鍙互鎶婂浘褰㈣繖涓娊璞″拰鐢繪硶榪欎釜琛屼負(fù)鍒嗙錛岃屼笉闇瑕佷竴涓敾娉曞搴斿涓娊璞★紝濡備笂闈㈤偅涓?br>
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.[6]
publicclass TaskController { publicvoid process () { // this code is an emulation of a // processing task controller // . . . // figure out which country you are in CalcTax myTax; myTax= getTaxRulesForUS(); SalesOrder mySO=new SalesOrder(); mySO.process( myTax);// 褰撶劧浣犺繕鍙互璁﹎yTax=getTaxRulesForCan();榪欐牱mySo.process(myTax)灝變細(xì)鎸夌収鍔犳嬁澶х殑紼庣巼澶勭悊 } private CalcTax getTaxRulesForUS() { // In real life, get the tax rules based on // country you are in. You may have the // logic here or you may have it in a // configuration file // Here, just return a USTax so this // will compile. returnnew USTax(); } } publicclass SalesOrder { publicvoid process (CalcTax taxToUse) { long itemNumber=0; double price=0; // given the tax object to use // . . . // calculate tax double tax= taxToUse.taxAmount( itemNumber, price); } } publicabstractclass CalcTax { abstractpublicdouble taxAmount( long itemSold, double price); } publicclass CanTax extends CalcTax { publicdouble taxAmount( long itemSold, double price) { // in real life, figure out tax according to // the rules in Canada and return it // here, return 0 so this will compile return0.0; } } publicclass USTax extends CalcTax { publicdouble taxAmount( long itemSold, double price) { // in real life, figure out tax according to // the rules in the US and return it // here, return 0 so this will compile return0.0; } }