{"rsdb":{"rid":"239651","subhead":"","postdate":"0","aid":"165142","fid":"49","uid":"1","topic":"1","content":"
\n

1.\u201c\u7ec4\u4ef6<\/a>\u534f\u4f5c\u201d\u6a21\u5f0f:<\/p> \n

\u73b0\u4ee3\u8f6f\u4ef6\u4e13\u4e1a\u5206\u5de5\u4e4b\u540e\u7684\u7b2c\u4e00\u4e2a\u7ed3\u679c\u662f\u201c\u6846\u67b6\u4e0e\u5e94\u7528\u7a0b\u5e8f\u7684\u5212\u5206\u201d\uff0c\u201c\u7ec4\u4ef6\u534f\u4f5c\u201d\u6a21\u5f0f\u901a\u8fc7\u665a\u671f\u7ed1\u5b9a\uff0c\u6765\u5b9e\u73b0\u6846\u67b6\u4e0e\u5e94\u7528\u7a0b\u5e8f\u4e4b \u95f4\u7684\u677e\u8026\u5408\uff0c\u662f\u4e8c\u8005\u4e4b\u95f4\u534f\u4f5c\u65f6\u5e38\u7528\u7684\u6a21\u5f0f\u3002<\/p> \n

\u5178\u578b\u6a21\u5f0f<\/p> \n

Template Method<\/p> \n

Strategy<\/p> \n

Observer \/ Event<\/p> \n

2.\u52a8\u673a(Motivation)<\/p> \n

?\u5728\u8f6f\u4ef6\u6784\u5efa\u8fc7\u7a0b\u4e2d\uff0c\u67d0\u4e9b\u5bf9\u8c61\u4f7f\u7528\u7684\u7b97\u6cd5\u53ef\u80fd\u591a\u79cd\u591a\u6837\uff0c\u7ecf\u5e38\u6539 \u53d8\uff0c\u5982\u679c\u5c06\u8fd9\u4e9b\u7b97\u6cd5\u90fd\u7f16\u7801\u5230\u5bf9\u8c61\u4e2d\uff0c\u5c06\u4f1a\u4f7f\u5bf9\u8c61\u53d8\u5f97\u5f02\u5e38\u590d\u6742; \u800c\u4e14\u6709\u65f6\u5019\u652f\u6301\u4e0d\u4f7f\u7528\u7684\u7b97\u6cd5\u4e5f\u662f\u4e00\u4e2a\u6027\u80fd\u8d1f\u62c5\u3002<\/p> \n

3.\u6a21\u5f0f\u5b9a\u4e49<\/p> \n

\u5b9a\u4e49\u4e00\u7cfb\u5217\u7b97\u6cd5\uff0c\u628a\u5b83\u4eec\u4e00\u4e2a\u4e2a\u5c01\u88c5\u8d77\u6765\uff0c\u5e76\u4e14\u4f7f\u5b83\u4eec\u53ef\u4e92 \u76f8\u66ff\u6362(\u53d8\u5316)\u3002\u8be5\u6a21\u5f0f\u4f7f\u5f97\u7b97\u6cd5\u53ef\u72ec\u7acb\u4e8e\u4f7f\u7528\u5b83\u7684\u5ba2\u6237\u7a0b \u5e8f(\u7a33\u5b9a)\u800c\u53d8\u5316(\u6269\u5c55\uff0c\u5b50\u7c7b\u5316)\u3002 \u2014\u2014\u300a\u8bbe\u8ba1\u6a21\u5f0f\u300bGoF<\/p> \n

4.\u7ed3\u6784<\/p> \n

\n \"\\\"\n <\/center> \n

5.\u6211\u7684\u7406\u89e3<\/p> \n

\u9996\u5148\uff0c\u53ef\u4ee5\u4ece\u7c7b\u56fe\u4e2d\u770b\u51fa\uff0c\u7236\u7c7b\u5b9a\u4e49\u4e00\u4e2a\u7a33\u5b9a\u7684\u65b9\u6cd5\uff0c\u5b50\u7c7b\u901a\u8fc7\u7ee7\u627f\u5b9e\u73b0\u5176\u5404\u81ea\u7684\u7279\u6027;<\/p> \n

\u7136\u540e\uff0c\u5b9a\u4e49\u4e00\u4e2a\u7b56\u7565\u7c7b\uff0c\u5b9e\u73b0\u4e00\u4e2a\u826f\u597d\u7684\u63a5\u53e3\u3002\u901a\u8fc7\u4f20\u5165\u7684\u5bf9\u8c61\uff0c\u6267\u884c\u54cd\u5e94\u7684\u64cd\u4f5c;<\/p> \n

\u8fd9\u91cc\u7528CS\u91cc\u7684\u4eba\u7269\u4f5c\u4e3a\u4f8b\u5b50\uff0c\u6bcf\u4e2a\u4eba\u90fd\u53ef\u4ee5\u6709\u51e0\u4e2a\u6b66\u5668\uff0c\u6b66\u5668\u4e4b\u95f4\u52a8\u6001\u5207\u6362\uff0c\u6b66\u5668\u62e5\u6709\u7edf\u4e00\u7684\u653b\u51fb\u547d\u4ee4\uff1a<\/p> \n

\r\n#include \n  \n   \r\nusing namespace std;\r\n\r\nclass WeaponBehavior\r\n{\r\npublic:\r\n\tvoid virtual useWeapon() = 0;\r\n};\r\n\r\nclass AK47:public WeaponBehavior\r\n{\r\npublic:\r\n\tvoid useWeapon()\r\n\t{\r\n\t\tcout << "Use AK47 to shoot!" << endl;\r\n\t}\r\n};\r\n\r\nclass Knife:public WeaponBehavior\r\n{\r\npublic:\r\n\tvoid useWeapon()\r\n\t{\r\n\t\tcout << "Use Knife to kill!" << endl;\r\n\t}\r\n};\r\n\r\nclass Character\r\n{\r\npublic:\r\n\tCharacter()\r\n\t{\r\n\t\tweapon = 0;\r\n\t}\r\n\tvoid setWeapon(WeaponBehavior *w)\r\n\t{\r\n\t\tthis->weapon = w;\r\n\t}\r\n\tvoid virtual fight() = 0;\r\nprotected:\r\n\tWeaponBehavior *weapon;\r\n};\r\n\r\nclass King:public Character\r\n{\r\npublic:\r\n\tvoid fight()\r\n\t{\r\n\t\tcout << "The king:" ;\r\n\t\tif ( this->weapon == NULL)\r\n\t\t{\r\n\t\t\tcout << "You don't have a weapon! Please Set Weapon!" << endl;\r\n\t\t}\r\n\t\telse\r\n\t\t{ \r\n\t\t\tweapon->useWeapon();\r\n\t\t}\r\n\t}\r\n};\r\nint main()\r\n{    \r\n\tWeaponBehavior *ak47 = new AK47();\r\n\tWeaponBehavior *knife = new Knife();     \r\n\r\n\tCharacter *kin = new King();      \r\n\r\n\tkin->fight();   \r\n\tcout << endl; \r\n\r\n\tkin->setWeapon(ak47);\r\n\tkin->fight();\r\n\tcout << endl;\r\n\r\n\tkin->setWeapon(knife);\r\n\tkin->fight();\r\n\r\n\treturn 0;\r\n}\n  <\/iostream><\/pre> \n<\/div>","orderid":"0","title":"C++\u7b56\u7565\u6a21\u5f0fStrategy\u4f7f\u7528\u6559\u7a0b","smalltitle":"","mid":"0","fname":"c++\u7f16\u7a0b\u57fa\u7840","special_id":"0","bak_id":"0","info":"0","hits":"2039","pages":"1","comments":"0","posttime":"2017-10-18 09:07:19","list":"1508288839","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"\u7b56\u7565<\/A> \u6a21\u5f0f<\/A> Strategy<\/A> \u4f7f\u7528<\/A> \u6559\u7a0b<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"113.108.110.181","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"C++\u7b56\u7565\u6a21\u5f0fStrategy\u4f7f\u7528\u6559\u7a0b","lastview":"1703098631","digg_num":"23","digg_time":"1710969132","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}