{"id":107567,"date":"2026-09-19T20:49:16","date_gmt":"2026-09-19T12:49:16","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/107567.html"},"modified":"2026-09-19T20:49:16","modified_gmt":"2026-09-19T12:49:16","slug":"%e3%80%90%e8%ae%be%e8%ae%a1%e6%a8%a1%e5%bc%8f%e7%b3%bb%e5%88%97-%e4%b8%80-%e3%80%91%e4%b8%87%e5%ad%97%e8%a7%a3%e6%9e%90%ef%bc%9a%e4%b8%80%e6%96%87%e5%b8%a6%e4%bd%a0%e6%90%9e%e5%ae%9a%e9%9d%a2","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/107567.html","title":{"rendered":"\u3010\u8bbe\u8ba1\u6a21\u5f0f\u7cfb\u5217 (\u4e00) \u3011\u4e07\u5b57\u89e3\u6790\uff1a\u4e00\u6587\u5e26\u4f60\u641e\u5b9a\u9762\u5411\u5bf9\u8c61\u8bbe\u8ba1\u539f\u5219"},"content":{"rendered":"<p style=\"text-align:center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"197\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260919124911-6aae84c79f3ad.png\" width=\"197\" \/><\/p>\n<p style=\"text-align:center\">\u2b50\ufe0f\u5728\u8fd9\u4e2a\u6000\u7591\u7684\u5e74\u4ee3&#xff0c;\u6211\u4eec\u4f9d\u7136\u9700\u8981\u4fe1\u4ef0\u3002<\/p>\n<p style=\"text-align:center\">\u4e2a\u4eba\u4e3b\u9875\u00a0&#xff1a;YYYing.<\/p>\n<p style=\"text-align:center\">\u2b50\ufe0f\u8bbe\u8ba1\u6a21\u5f0f\u7cfb\u5217\u4e13\u680f&#xff1a;\u8bbe\u8ba1\u6a21\u5f0f\u7cfb\u5217<\/p>\n<p style=\"text-align:center\">\u7cfb\u5217\u4e0b\u671f\u5185\u5bb9&#xff1a;\u6682\u65e0<\/p>\n<hr \/>\n<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"%E5%89%8D%E8%A8%80%EF%BC%9A-toc\" style=\"margin-left:0px\">\u524d\u8a00&#xff1a;<\/p>\n<p id=\"1.1%20%E4%BC%98%E7%A7%80%E8%AE%BE%E8%AE%A1%E7%9A%84%E7%89%B9%E5%BE%81-toc\" style=\"margin-left:0px\">1.1 \u4f18\u79c0\u8bbe\u8ba1\u7684\u7279\u5f81<\/p>\n<p id=\"1.1.1%20%E4%BB%A3%E7%A0%81%E5%A4%8D%E7%94%A8%EF%BC%88Code%20Reuse%EF%BC%89-toc\" style=\"margin-left:80px\">1.1.1 \u4ee3\u7801\u590d\u7528&#xff08;Code Reuse&#xff09;<\/p>\n<p id=\"1.1.2%20%E6%89%A9%E5%B1%95%E6%80%A7%EF%BC%88Extensibility%EF%BC%89-toc\" style=\"margin-left:80px\">1.1.2 \u6269\u5c55\u6027&#xff08;Extensibility&#xff09;<\/p>\n<p id=\"1.1.3%20%E5%8F%AF%E7%BB%B4%E6%8A%A4%E6%80%A7%E4%B8%8E%E5%8F%AF%E5%A4%8D%E7%94%A8%E6%80%A7%EF%BC%9A%E4%B8%80%E5%AF%B9%E5%AD%AA%E7%94%9F%E7%9B%AE%E6%A0%87-toc\" style=\"margin-left:80px\">1.1.3 \u53ef\u7ef4\u62a4\u6027\u4e0e\u53ef\u590d\u7528\u6027&#xff1a;\u4e00\u5bf9\u5b6a\u751f\u76ee\u6807<\/p>\n<p id=\"1.2%20%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E8%AE%BE%E8%AE%A1%E5%8E%9F%E5%88%99%E6%80%BB%E8%A7%88-toc\" style=\"margin-left:0px\">1.2 \u9762\u5411\u5bf9\u8c61\u8bbe\u8ba1\u539f\u5219\u603b\u89c8<\/p>\n<p id=\"1.3%20SOLID%20%E5%8E%9F%E5%88%99-toc\" style=\"margin-left:0px\">1.3 SOLID \u539f\u5219<\/p>\n<p id=\"1.3.1%20%E5%8D%95%E4%B8%80%E8%81%8C%E8%B4%A3%E5%8E%9F%E5%88%99%20SRP-toc\" style=\"margin-left:80px\">1.3.1 \u5355\u4e00\u804c\u8d23\u539f\u5219 SRP<\/p>\n<p id=\"%E5%8F%8D%E4%BE%8B%EF%BC%9ARoleDataOperation-toc\" style=\"margin-left:120px\">\u53cd\u4f8b&#xff1a;RoleDataOperation<\/p>\n<p id=\"%E9%87%8D%E6%9E%84%E5%90%8E-toc\" style=\"margin-left:120px\">\u91cd\u6784\u540e<\/p>\n<p id=\"%E2%9A%A0%EF%B8%8F%20%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9-toc\" style=\"margin-left:120px\">\u26a0\ufe0f \u6ce8\u610f\u4e8b\u9879<\/p>\n<p id=\"1.3.2%20%E5%BC%80%E9%97%AD%E5%8E%9F%E5%88%99%20OCP-toc\" style=\"margin-left:80px\">1.3.2 \u5f00\u95ed\u539f\u5219 OCP<\/p>\n<p id=\"%E5%8F%8D%E4%BE%8B%EF%BC%9A%E5%88%B7%E6%80%AA%E5%A1%94-toc\" style=\"margin-left:120px\">\u53cd\u4f8b&#xff1a;\u5237\u602a\u5854<\/p>\n<p id=\"%E9%87%8D%E6%9E%84%EF%BC%9A%E5%A4%9A%E6%80%81%20%2B%20%E6%B3%A8%E5%86%8C%E8%A1%A8-toc\" style=\"margin-left:120px\">\u91cd\u6784&#xff1a;\u591a\u6001 &#043; \u6ce8\u518c\u8868<\/p>\n<p id=\"%E2%9A%A0%EF%B8%8F%20%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9-toc\" style=\"margin-left:120px\">\u26a0\ufe0f \u6ce8\u610f\u4e8b\u9879<\/p>\n<p id=\"1.3.3%20%E9%87%8C%E6%B0%8F%E6%9B%BF%E6%8D%A2%E5%8E%9F%E5%88%99%20LSP-toc\" style=\"margin-left:80px\">1.3.3 \u91cc\u6c0f\u66ff\u6362\u539f\u5219 LSP<\/p>\n<p id=\"%E6%9B%BF%E6%8D%A2%E8%A7%84%E5%88%99-toc\" style=\"margin-left:120px\">\u66ff\u6362\u89c4\u5219<\/p>\n<p id=\"%E6%AD%A3%E9%9D%A2%E7%A4%BA%E4%BE%8B%EF%BC%9A%E5%85%BC%E5%AE%B9%20LSP%20%E7%9A%84%E8%AE%BE%E8%AE%A1-toc\" style=\"margin-left:120px\">\u6b63\u9762\u793a\u4f8b&#xff1a;\u517c\u5bb9 LSP \u7684\u8bbe\u8ba1<\/p>\n<p id=\"%E5%8F%8D%E9%9D%A2%E7%A4%BA%E4%BE%8B%EF%BC%9A%E4%B8%8D%E5%85%BC%E5%AE%B9%20LSP-toc\" style=\"margin-left:120px\">\u53cd\u9762\u793a\u4f8b&#xff1a;\u4e0d\u517c\u5bb9 LSP<\/p>\n<p id=\"TIP%EF%BC%9A%E7%8E%B0%E5%9C%A8%E7%9A%84%E9%97%AE%E9%A2%98%E6%98%AF%E8%B0%81%E5%BA%94%E8%AF%A5%E5%AF%B9%E8%BF%9D%E5%8F%8D%20LSP%20%E8%B4%9F%E8%B4%A3%EF%BC%9F-toc\" style=\"margin-left:120px\">TIP&#xff1a;\u73b0\u5728\u7684\u95ee\u9898\u662f\u8c01\u5e94\u8be5\u5bf9\u8fdd\u53cd LSP \u8d1f\u8d23&#xff1f;<\/p>\n<p id=\"TIP%EF%BC%9A%E5%B8%B8%E8%A7%81%E7%9A%84%20LSP%20%E8%BF%9D%E8%A7%84-toc\" style=\"margin-left:120px\">TIP&#xff1a;\u5e38\u89c1\u7684 LSP \u8fdd\u89c4<\/p>\n<p id=\"1.3.4%20%E4%BE%9D%E8%B5%96%E5%80%92%E7%BD%AE%E5%8E%9F%E5%88%99%20DIP-toc\" style=\"margin-left:80px\">1.3.4 \u4f9d\u8d56\u5012\u7f6e\u539f\u5219 DIP<\/p>\n<p id=\"%E8%90%BD%E5%9C%B0%E8%A6%81%E6%B1%82-toc\" style=\"margin-left:120px\">\u843d\u5730\u8981\u6c42<\/p>\n<p id=\"%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%EF%BC%88Dependency%20Injection%2C%20DI%EF%BC%89-toc\" style=\"margin-left:120px\">\u4f9d\u8d56\u6ce8\u5165&#xff08;Dependency Injection, DI&#xff09;<\/p>\n<p id=\"%E5%8F%8D%E4%BE%8B%EF%BC%9ADataBiz%20%E9%92%88%E5%AF%B9%E5%85%B7%E4%BD%93%E5%AE%9E%E7%8E%B0%E7%BC%96%E7%A8%8B-toc\" style=\"margin-left:120px\">\u53cd\u4f8b&#xff1a;DataBiz \u9488\u5bf9\u5177\u4f53\u5b9e\u73b0\u7f16\u7a0b<\/p>\n<p id=\"%E9%87%8D%E6%9E%84%EF%BC%9A%E6%8A%BD%E8%B1%A1%20%2B%20%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%20%2B%20%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6-toc\" style=\"margin-left:120px\">\u91cd\u6784&#xff1a;\u62bd\u8c61 &#043; \u4f9d\u8d56\u6ce8\u5165 &#043; \u914d\u7f6e\u6587\u4ef6<\/p>\n<p id=\"C%2B%2B%20%E8%A7%86%E8%A7%92%E7%9A%84%E5%85%B3%E9%94%AE%E6%94%B6%E7%9B%8A-toc\" style=\"margin-left:120px\">C&#043;&#043; \u89c6\u89d2\u7684\u5173\u952e\u6536\u76ca<\/p>\n<p id=\"1.3.5%20%E6%8E%A5%E5%8F%A3%E9%9A%94%E7%A6%BB%E5%8E%9F%E5%88%99%20ISP-toc\" style=\"margin-left:80px\">1.3.5 \u63a5\u53e3\u9694\u79bb\u539f\u5219 ISP<\/p>\n<p id=\"%22%E6%8E%A5%E5%8F%A3%22%E7%9A%84%E4%B8%A4%E7%A7%8D%E4%B8%8D%E5%90%8C%E5%90%AB%E4%B9%89-toc\" style=\"margin-left:120px\">&#034;\u63a5\u53e3&#034;\u7684\u4e24\u79cd\u4e0d\u540c\u542b\u4e49<\/p>\n<p id=\"%E5%8F%8D%E4%BE%8B%EF%BC%9A%E4%B8%80%E4%B8%AA%E5%8C%85%E5%8A%9E%E6%89%80%E6%9C%89%E7%9A%84%E8%83%96%E6%8E%A5%E5%8F%A3-toc\" style=\"margin-left:120px\">\u53cd\u4f8b&#xff1a;\u4e00\u4e2a\u5305\u529e\u6240\u6709\u7684\u80d6\u63a5\u53e3<\/p>\n<p id=\"%E9%87%8D%E6%9E%84%E5%90%8E%EF%BC%9A%E6%8C%89%E8%A7%92%E8%89%B2%E6%8B%86%E5%88%86%E6%88%90%E5%B0%8F%E6%8E%A5%E5%8F%A3-toc\" style=\"margin-left:120px\">\u91cd\u6784\u540e&#xff1a;\u6309\u89d2\u8272\u62c6\u5206\u6210\u5c0f\u63a5\u53e3<\/p>\n<p id=\"%E2%9A%A0%EF%B8%8F%20%E4%BD%BF%E7%94%A8%E6%8E%A5%E5%8F%A3%E9%9A%94%E7%A6%BB%E5%8E%9F%E5%88%99%E6%97%B6%E7%9A%84%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9-toc\" style=\"margin-left:120px\">\u26a0\ufe0f \u4f7f\u7528\u63a5\u53e3\u9694\u79bb\u539f\u5219\u65f6\u7684\u6ce8\u610f\u4e8b\u9879<\/p>\n<p id=\"1.4%20%E7%BB%84%E5%90%88%E5%A4%8D%E7%94%A8%E5%8E%9F%E5%88%99%20CRP-toc\" style=\"margin-left:0px\">1.4 \u7ec4\u5408\u590d\u7528\u539f\u5219 CRP<\/p>\n<p id=\"%E4%B8%BA%E4%BB%80%E4%B9%88%E4%BC%98%E5%85%88%E7%BB%84%E5%90%88%EF%BC%9F-toc\" style=\"margin-left:80px\">\u4e3a\u4ec0\u4e48\u4f18\u5148\u7ec4\u5408&#xff1f;<\/p>\n<p id=\"%E7%99%BD%E7%AE%B1%E5%A4%8D%E7%94%A8%20vs%20%E9%BB%91%E7%AE%B1%E5%A4%8D%E7%94%A8-toc\" style=\"margin-left:80px\">\u767d\u7bb1\u590d\u7528 vs \u9ed1\u7bb1\u590d\u7528<\/p>\n<p id=\"%E4%B8%80%E8%88%AC%E8%80%8C%E8%A8%80%E7%9A%84%E5%88%A4%E6%96%AD%E6%A0%87%E5%87%86-toc\" style=\"margin-left:80px\">\u4e00\u822c\u800c\u8a00\u7684\u5224\u65ad\u6807\u51c6<\/p>\n<p id=\"%E6%A1%88%E4%BE%8B%EF%BC%9A%E5%A4%9A%E7%BB%B4%E5%BA%A6%E6%89%A9%E5%B1%95%E5%AF%BC%E8%87%B4%E7%9A%84%E5%AD%90%E7%B1%BB%E7%88%86%E7%82%B8-toc\" style=\"margin-left:80px\">\u6848\u4f8b&#xff1a;\u591a\u7ef4\u5ea6\u6269\u5c55\u5bfc\u81f4\u7684\u5b50\u7c7b\u7206\u70b8<\/p>\n<p id=\"%E9%87%8D%E6%9E%84%EF%BC%9A%E7%94%A8%E7%BB%84%E5%90%88%E6%9B%BF%E4%BB%A3%E5%A4%9A%E7%BB%B4%E7%BB%A7%E6%89%BF-toc\" style=\"margin-left:80px\">\u91cd\u6784&#xff1a;\u7528\u7ec4\u5408\u66ff\u4ee3\u591a\u7ef4\u7ee7\u627f<\/p>\n<p id=\"C%2B%2B%20%E8%A7%86%E8%A7%92%E7%9A%84%E8%A1%A5%E5%85%85%E6%94%B6%E7%9B%8A-toc\" style=\"margin-left:120px\">C&#043;&#043; \u89c6\u89d2\u7684\u8865\u5145\u6536\u76ca<\/p>\n<p id=\"1.5%20%E8%BF%AA%E7%B1%B3%E7%89%B9%E6%B3%95%E5%88%99%20LOD-toc\" style=\"margin-left:0px\">1.5 \u8fea\u7c73\u7279\u6cd5\u5219 LOD<\/p>\n<p id=\"1.5.1%20%E6%B3%95%E5%88%99%E7%9B%AE%E6%A0%87-toc\" style=\"margin-left:80px\">1.5.1 \u6cd5\u5219\u76ee\u6807<\/p>\n<p id=\"1.5.2%20%E7%8B%AD%E4%B9%89%E7%9A%84%E8%BF%AA%E7%B1%B3%E7%89%B9%E6%B3%95%E5%88%99-toc\" style=\"margin-left:80px\">1.5.2 \u72ed\u4e49\u7684\u8fea\u7c73\u7279\u6cd5\u5219<\/p>\n<p id=\"1.5.3%20%E5%B9%BF%E4%B9%89%E7%9A%84%E8%BF%AA%E7%B1%B3%E7%89%B9%E6%B3%95%E5%88%99-toc\" style=\"margin-left:80px\">1.5.3 \u5e7f\u4e49\u7684\u8fea\u7c73\u7279\u6cd5\u5219<\/p>\n<p id=\"1.5.4%20%E6%A1%88%E4%BE%8B-toc\" style=\"margin-left:80px\">1.5.4 \u6848\u4f8b<\/p>\n<p id=\"1.5.4.1%20%E4%B8%8E%E7%9B%B4%E6%8E%A5%E6%9C%8B%E5%8F%8B%E9%80%9A%E4%BF%A1-toc\" style=\"margin-left:120px\">1.5.4.1 \u4e0e\u76f4\u63a5\u670b\u53cb\u901a\u4fe1<\/p>\n<p id=\"1.5.4.2%20%E5%87%8F%E5%B0%91%E5%AF%B9%E6%9C%8B%E5%8F%8B%E7%9A%84%E4%BA%86%E8%A7%A3-toc\" style=\"margin-left:120px\">1.5.4.2 \u51cf\u5c11\u5bf9\u670b\u53cb\u7684\u4e86\u89e3<\/p>\n<p id=\"1.5.5%20%E4%BD%BF%E7%94%A8%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9-toc\" style=\"margin-left:80px\">1.5.5 \u4f7f\u7528\u6ce8\u610f\u4e8b\u9879<\/p>\n<p id=\"1.6%20%E5%B0%8F%E7%BB%93-toc\" style=\"margin-left:0px\">1.6 \u5c0f\u7ed3<\/p>\n<p id=\"%E9%9D%A2%E8%AF%95%2F%E5%A4%8D%E4%B9%A0%E5%85%AB%E9%97%AE-toc\" style=\"margin-left:80px\">\u9762\u8bd5\/\u590d\u4e60\u516b\u95ee<\/p>\n<p id=\"%E4%B8%80%E5%8F%A5%E8%AF%9D%E8%AE%B0%E5%BF%86%E7%89%88-toc\" style=\"margin-left:80px\">\u4e00\u53e5\u8bdd\u8bb0\u5fc6\u7248<\/p>\n<p id=\"%E5%8E%9F%E5%88%99%E4%B9%8B%E9%97%B4%E7%9A%84%E5%85%B3%E7%B3%BB-toc\" style=\"margin-left:80px\">\u539f\u5219\u4e4b\u95f4\u7684\u5173\u7cfb<\/p>\n<p id=\"%E9%99%84%E5%BD%95%EF%BC%9A%E4%B8%83%E5%A4%A7%E5%8E%9F%E5%88%99%E9%80%9F%E6%9F%A5%E8%A1%A8%EF%BC%88%E5%A4%8D%E4%B9%A0%E7%94%A8%EF%BC%89-toc\" style=\"margin-left:40px\">\u9644\u5f55&#xff1a;\u4e03\u5927\u539f\u5219\u901f\u67e5\u8868&#xff08;\u590d\u4e60\u7528&#xff09;<\/p>\n<hr id=\"hr-toc\" \/>\n<p class=\"img-center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260919124912-6aae84c817e5e.gif\" \/><\/p>\n<\/p>\n<hr \/>\n<h2 id=\"%E5%89%8D%E8%A8%80%EF%BC%9A\">\u524d\u8a00&#xff1a;<\/h2>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u76f8\u4fe1\u6211\u4eec\u5f88\u591a\u4eba\u5728\u5b66\u4e60\u7f51\u4e0a\u7684\u9879\u76ee\u7684\u65f6\u5019&#xff0c;\u4f1a\u6709\u4e00\u4e2a\u6700\u5927\u7684\u8ff7\u60d1&#xff0c;\u5c31\u662f\u201c\u4e3a\u4ec0\u4e48\u8fd9\u91cc\u8981\u8fd9\u4e48\u8bbe\u8ba1&#xff1f;\u201d&#xff0c;\u8bbe\u8ba1\u6a21\u5f0f\u5c31\u662f\u6765\u56de\u7b54\u8fd9\u4e2a\u95ee\u9898\u7684&#xff0c;\u6211\u76f8\u4fe1\u54ea\u6015\u53ea\u770b\u4e0b\u53bb\u8fd9\u4e00\u7bc7&#xff0c;\u90a3\u4e48\u5bf9\u4e8e\u505a\u8fc7\u4e00\u4e9b\u8d28\u91cf\u6bd4\u8f83\u9ad8\u7684\u9879\u76ee\u7684\u4eba&#xff0c;\u90fd\u4f1a\u5728\u4f60\u7684\u9879\u76ee\u4e2d\u627e\u5230\u5bf9\u5e94\u7684\u8bbe\u8ba1\u70b9&#xff0c;\u4e0d\u8bf4\u4e86\u6211\u4eec\u9a6c\u4e0a\u5f00\u59cb\u3002<\/p>\n<hr \/>\n<h2 id=\"1.1%20%E4%BC%98%E7%A7%80%E8%AE%BE%E8%AE%A1%E7%9A%84%E7%89%B9%E5%BE%81\">1.1 \u4f18\u79c0\u8bbe\u8ba1\u7684\u7279\u5f81<\/h2>\n<p>\u5728\u5f00\u59cb\u5b66\u4e60\u5b9e\u9645\u7684\u6a21\u5f0f\u4e4b\u524d&#xff0c;\u5148\u770b\u770b\u8f6f\u4ef6\u67b6\u6784\u7684\u8bbe\u8ba1\u8fc7\u7a0b \u2014\u2014 \u641e\u6e05\u695a\u8981\u8fbe\u6210\u4ec0\u4e48\u76ee\u6807&#xff0c;\u4ee5\u53ca\u8981\u907f\u5f00\u54ea\u4e9b\u9677\u9631\u3002<\/p>\n<h4 id=\"1.1.1%20%E4%BB%A3%E7%A0%81%E5%A4%8D%E7%94%A8%EF%BC%88Code%20Reuse%EF%BC%89\">1.1.1 \u4ee3\u7801\u590d\u7528&#xff08;Code Reuse&#xff09;<\/h4>\n<p>\u65e0\u8bba\u662f\u5f00\u53d1\u4f55\u79cd\u8f6f\u4ef6\u4ea7\u54c1&#xff0c;\u6210\u672c\u548c\u65f6\u95f4\u90fd\u662f\u6700\u91cd\u8981\u7684\u4e24\u4e2a\u7ef4\u5ea6&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u8f83\u77ed\u7684\u5f00\u53d1\u65f6\u95f4\u610f\u5473\u7740\u53ef\u4ee5\u6bd4\u7ade\u4e89\u5bf9\u624b\u66f4\u65e9\u8fdb\u5165\u5e02\u573a&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u8f83\u4f4e\u7684\u5f00\u53d1\u6210\u672c\u610f\u5473\u7740\u80fd\u591f\u7559\u51fa\u66f4\u591a\u8425\u9500\u8d44\u91d1&#xff0c;\u4ece\u800c\u66f4\u5e7f\u6cdb\u5730\u8986\u76d6\u6f5c\u5728\u5ba2\u6237\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u800c\u4ee3\u7801\u590d\u7528\u662f\u964d\u4f4e\u5f00\u53d1\u6210\u672c\u65f6\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u4e4b\u4e00\u3002<\/p>\n<p>&#x1f4ac; \u4eba\u8bdd&#xff1a;\u590d\u7528\u4e0d\u662f&#034;\u6284\u4ee3\u7801&#034;&#xff0c;\u800c\u662f&#034;\u8fd9\u6bb5\u903b\u8f91\u6211\u53ea\u7ef4\u62a4\u4e00\u4efd&#034;\u3002 \u5224\u65ad\u4e00\u6bb5\u4ee3\u7801\u503c\u4e0d\u503c\u5f97\u62bd\u51fa\u6765\u590d\u7528&#xff0c;\u5c31\u95ee\u4e00\u53e5&#xff1a;\u540c\u6837\u7684 bug&#xff0c;\u4f60\u613f\u610f\u4fee\u51e0\u6b21&#xff1f; \u5982\u679c\u7b54\u6848\u662f&#034;\u4e24\u6b21\u4ee5\u4e0a&#034;&#xff0c;\u90a3\u5c31\u8be5\u62bd\u51fa\u6765\u3002<\/p>\n<h4 id=\"1.1.2%20%E6%89%A9%E5%B1%95%E6%80%A7%EF%BC%88Extensibility%EF%BC%89\">1.1.2 \u6269\u5c55\u6027&#xff08;Extensibility&#xff09;<\/h4>\n<p>\u53d8\u5316\u662f\u7a0b\u5e8f\u5458\u751f\u547d\u4e2d\u552f\u4e00\u4e0d\u53d8\u7684\u4e8b\u60c5\u3002<\/p>\n<ul>\n<li>\n<p>\u6bd4\u5982\u4f60\u5728 Windows \u5e73\u53f0\u4e0b\u53d1\u5e03\u4e86\u4e00\u6b3e\u6e38\u620f\u706b\u4e86&#xff0c;\u73a9\u5bb6\u8981\u6c42\u53d1\u5e03 Mac \u7248&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u6bd4\u5982\u4f60\u8bbe\u8ba1\u4e86\u4e00\u6b3e\u6f02\u4eae\u7684\u6570\u5b57\u9a8c\u8bc1\u7801\u63d2\u4ef6&#xff0c;\u4f46\u662f\u7528\u6237\u8981\u6c42\u6ed1\u5757\u9a8c\u8bc1\u7801&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u6bd4\u5982\u4f60\u521a\u521a\u5b8c\u6210\u4e00\u4e2a\u529f\u80fd&#xff0c;\u6b64\u65f6\u9700\u6c42\u53d8\u4e86&#xff08;\u592a\u5e38\u89c1\u4e86&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u56e0\u6b64\u5728\u8bbe\u8ba1\u7a0b\u5e8f\u67b6\u6784\u65f6&#xff0c;\u6240\u6709\u6709\u7ecf\u9a8c\u7684\u5f00\u53d1\u8005\u4f1a\u5c3d\u91cf\u9009\u62e9\u652f\u6301\u672a\u6765\u4efb\u4f55\u53ef\u80fd\u53d8\u66f4\u7684\u65b9\u5f0f\u3002<\/p>\n<p>\u5728\u652f\u6301\u53ef\u7ef4\u62a4\u6027&#xff08;Maintainability&#xff09;\u7684\u540c\u65f6&#xff0c;\u63d0\u9ad8\u7cfb\u7edf\u7684\u53ef\u590d\u7528\u6027&#xff08;Reusability&#xff09;\u662f\u4e00\u4e2a\u81f3\u5173\u91cd\u8981\u7684\u95ee\u9898\u3002\u5982\u4f55\u540c\u65f6\u63d0\u9ad8\u4e00\u4e2a\u8f6f\u4ef6\u7cfb\u7edf\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u590d\u7528\u6027&#xff0c;\u662f\u9762\u5411\u5bf9\u8c61\u8bbe\u8ba1\u9700\u8981\u89e3\u51b3\u7684\u6838\u5fc3\u95ee\u9898\u4e4b\u4e00\u3002<\/p>\n<p>&#x1f4ac; \u4eba\u8bdd&#xff1a;\u9700\u6c42\u53d8\u5316\u4e0d\u662f&#034;\u610f\u5916&#034;&#xff0c;\u800c\u662f&#034;\u5e38\u6001&#034;\u3002\u597d\u7684\u8bbe\u8ba1\u4e0d\u662f\u7948\u7977\u9700\u6c42\u522b\u53d8&#xff0c;\u800c\u662f\u8ba9\u53d8\u5316\u7684\u4ee3\u4ef7\u5c3d\u53ef\u80fd\u5c0f\u3002 \u4e00\u4e2a\u6734\u7d20\u7684\u8861\u91cf\u6807\u51c6&#xff1a;\u9700\u6c42\u53d8\u4e86&#xff0c;\u4f60\u8981\u52a8\u51e0\u4e2a\u6587\u4ef6&#xff1f;\u6539\u5b8c\u4e4b\u540e&#xff0c;\u4f60\u6562\u4e0d\u6562\u4e0d\u91cd\u65b0\u6d4b\u4e00\u904d\u5168\u90e8\u529f\u80fd&#xff1f;<\/p>\n<h4 id=\"1.1.3%20%E5%8F%AF%E7%BB%B4%E6%8A%A4%E6%80%A7%E4%B8%8E%E5%8F%AF%E5%A4%8D%E7%94%A8%E6%80%A7%EF%BC%9A%E4%B8%80%E5%AF%B9%E5%AD%AA%E7%94%9F%E7%9B%AE%E6%A0%87\">1.1.3 \u53ef\u7ef4\u62a4\u6027\u4e0e\u53ef\u590d\u7528\u6027&#xff1a;\u4e00\u5bf9\u5b6a\u751f\u76ee\u6807<\/h4>\n<table>\n<tr>\u76ee\u6807\u82f1\u6587\u542b\u4e49\u4eba\u8bdd<\/tr>\n<tbody>\n<tr>\n<td>\u53ef\u7ef4\u62a4\u6027<\/td>\n<td>Maintainability<\/td>\n<td>\u7cfb\u7edf\u80fd\u88ab\u7406\u89e3\u3001\u4fee\u6539\u3001\u6392\u9519\u3001\u5347\u7ea7\u7684\u96be\u6613\u7a0b\u5ea6<\/td>\n<td>\u534a\u5e74\u540e\u4f60\u8fd8\u80fd\u770b\u61c2\u81ea\u5df1\u5199\u7684\u4ee3\u7801<\/td>\n<\/tr>\n<tr>\n<td>\u53ef\u590d\u7528\u6027<\/td>\n<td>Reusability<\/td>\n<td>\u5df2\u6709\u7684\u8bbe\u8ba1\/\u5b9e\u73b0\u80fd\u88ab\u7528\u5728\u522b\u7684\u573a\u666f<\/td>\n<td>\u8fd9\u6bb5\u4ee3\u7801\u6362\u4e2a\u9879\u76ee\u8fd8\u80fd\u76f4\u63a5\u7528<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u9762\u5411\u5bf9\u8c61\u8bbe\u8ba1\u539f\u5219&#xff0c;\u5c31\u662f\u4e3a\u4e86\u652f\u6301&#034;\u53ef\u7ef4\u62a4\u7684\u590d\u7528&#034;\u800c\u8bde\u751f\u7684\u3002<\/p>\n<hr \/>\n<h2 id=\"1.2%20%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E8%AE%BE%E8%AE%A1%E5%8E%9F%E5%88%99%E6%80%BB%E8%A7%88\" style=\"background-color:transparent\">1.2 \u9762\u5411\u5bf9\u8c61\u8bbe\u8ba1\u539f\u5219\u603b\u89c8<\/h2>\n<p>\u9762\u5411\u5bf9\u8c61\u8bbe\u8ba1\u539f\u5219\u4e3a\u652f\u6301\u53ef\u7ef4\u62a4\u6027\u590d\u7528\u800c\u8bde\u751f\u3002\u8fd9\u4e9b\u539f\u5219\u8574\u542b\u5728\u5f88\u591a\u8bbe\u8ba1\u6a21\u5f0f\u4e2d&#xff0c;\u5b83\u4eec\u662f\u4ece\u8bb8\u591a\u8bbe\u8ba1\u65b9\u6848\u4e2d\u603b\u7ed3\u51fa\u7684\u6307\u5bfc\u6027\u539f\u5219\u3002<\/p>\n<p>\u9762\u5411\u5bf9\u8c61\u8bbe\u8ba1\u539f\u5219\u4e5f\u662f\u540e\u7eed\u8bbe\u8ba1\u6a21\u5f0f\u5b66\u4e60\u7684\u57fa\u7840 \u2014\u2014 \u6bcf\u4e00\u4e2a\u8bbe\u8ba1\u6a21\u5f0f\u90fd\u7b26\u5408\u4e00\u4e2a\u6216\u591a\u4e2a\u8bbe\u8ba1\u539f\u5219&#xff0c;\u662f\u8bc4\u4ef7\u8bbe\u8ba1\u6a21\u5f0f\u4f7f\u7528\u6548\u679c\u7684\u91cd\u8981\u6307\u6807\u4e4b\u4e00\u3002<\/p>\n<p>\u5e38\u89c1\u7684\u8bbe\u8ba1\u539f\u5219\u5982\u4e0b\u56fe\u6240\u793a&#xff1a;<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260919124912-6aae84c82ae34.png\" \/><\/p>\n<table>\n<tr>\u539f\u5219\u82f1\u6587 \/ \u7f29\u5199\u4e00\u53e5\u8bdd<\/tr>\n<tbody>\n<tr>\n<td>\u5355\u4e00\u804c\u8d23\u539f\u5219<\/td>\n<td>Single Responsibility Principle, SRP<\/td>\n<td>\u4e00\u4e2a\u7c7b\u53ea\u5e72\u4e00\u4ef6\u4e8b<\/td>\n<\/tr>\n<tr>\n<td>\u5f00\u95ed\u539f\u5219<\/td>\n<td>Open-Closed Principle, OCP<\/td>\n<td>\u5bf9\u6269\u5c55\u5f00\u653e&#xff0c;\u5bf9\u4fee\u6539\u5173\u95ed<\/td>\n<\/tr>\n<tr>\n<td>\u91cc\u6c0f\u66ff\u6362\u539f\u5219<\/td>\n<td>Liskov Substitution Principle, LSP<\/td>\n<td>\u5b50\u7c7b\u5fc5\u987b\u80fd\u66ff\u6362\u7236\u7c7b<\/td>\n<\/tr>\n<tr>\n<td>\u4f9d\u8d56\u5012\u7f6e\u539f\u5219<\/td>\n<td>Dependence Inversion Principle, DIP<\/td>\n<td>\u4f9d\u8d56\u62bd\u8c61&#xff0c;\u522b\u4f9d\u8d56\u5177\u4f53<\/td>\n<\/tr>\n<tr>\n<td>\u63a5\u53e3\u9694\u79bb\u539f\u5219<\/td>\n<td>Interface Segregation Principle, ISP<\/td>\n<td>\u63a5\u53e3\u8981\u5c0f\u800c\u4e13<\/td>\n<\/tr>\n<tr>\n<td>\u7ec4\u5408\/\u805a\u5408\u590d\u7528\u539f\u5219<\/td>\n<td>Composite\/Aggregate Reuse Principle, CRP\/CARP<\/td>\n<td>\u4f18\u5148\u7ec4\u5408&#xff0c;\u614e\u7528\u7ee7\u627f<\/td>\n<\/tr>\n<tr>\n<td>\u8fea\u7c73\u7279\u6cd5\u5219<\/td>\n<td>Law Of Demeter, LOD<\/td>\n<td>\u53ea\u548c\u76f4\u63a5\u670b\u53cb\u8bf4\u8bdd<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5176\u4e2d\u524d\u4e94\u4e2a&#xff08;SRP\u3001OCP\u3001LSP\u3001DIP\u3001ISP&#xff09;\u5408\u79f0 SOLID \u539f\u5219&#xff0c;\u662f\u9762\u5411\u5bf9\u8c61\u8bbe\u8ba1\u7684\u7eb2\u9886\u3002<\/p>\n<p>&#x1f4ac; \u4eba\u8bdd&#xff1a;\u8fd9\u4e03\u6761\u4e0d\u662f&#034;\u8003\u8bd5\u63d0\u7eb2&#034;&#xff0c;\u800c\u662f\u4e03\u79cd\u4e0d\u540c\u7684&#034;\u95fb\u5473\u9053&#034;\u7684\u65b9\u6cd5\u3002 \u4f60\u5199\u5b8c\u4e00\u6bb5\u4ee3\u7801\u89c9\u5f97&#034;\u54ea\u91cc\u4e0d\u5bf9\u4f46\u53c8\u8bf4\u4e0d\u4e0a\u6765&#034;\u65f6&#xff0c;\u9010\u6761\u5bf9\u4e00\u904d&#xff0c;\u901a\u5e38\u80fd\u5b9a\u4f4d\u5230\u95ee\u9898&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u4e00\u4e2a\u7c7b\u6539\u8d77\u6765\u7275\u8fde\u5f88\u591a \u2192 \u53ef\u80fd\u662f SRP \u6216 LOD \u7684\u95ee\u9898<\/p>\n<\/li>\n<li>\n<p>\u52a0\u4e2a\u529f\u80fd\u8981\u52a8\u8001\u4ee3\u7801 \u2192 OCP \u7684\u95ee\u9898<\/p>\n<\/li>\n<li>\n<p>\u91cd\u5199\u5b50\u7c7b\u65b9\u6cd5\u65f6\u5fc3\u91cc\u53d1\u865a \u2192 LSP \u7684\u95ee\u9898<\/p>\n<\/li>\n<li>\n<p>\u6362\u4e2a\u5b9e\u73b0\u8981\u91cd\u65b0\u7f16\u8bd1\u534a\u4e2a\u9879\u76ee \u2192 DIP \u7684\u95ee\u9898<\/p>\n<\/li>\n<li>\n<p>\u5b9e\u73b0\u4e00\u4e2a\u63a5\u53e3\u88ab\u8feb\u5199\u4e00\u5806\u7a7a\u65b9\u6cd5 \u2192 ISP \u7684\u95ee\u9898<\/p>\n<\/li>\n<li>\n<p>\u7ee7\u627f\u5c42\u6b21\u6df1\u5230\u770b\u4e0d\u6e05 \u2192 CRP \u7684\u95ee\u9898<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h2 id=\"1.3%20SOLID%20%E5%8E%9F%E5%88%99\" style=\"background-color:transparent\">1.3 SOLID \u539f\u5219<\/h2>\n<h4 id=\"1.3.1%20%E5%8D%95%E4%B8%80%E8%81%8C%E8%B4%A3%E5%8E%9F%E5%88%99%20SRP\" style=\"background-color:transparent\">1.3.1 \u5355\u4e00\u804c\u8d23\u539f\u5219 SRP<\/h4>\n<p>\u5355\u4e00\u804c\u8d23\u539f\u5219&#xff08;Single Responsibility Principle, SRP&#xff09;\u662f\u6700\u7b80\u5355\u7684\u8bbe\u8ba1\u539f\u5219&#xff0c;\u5b83\u7528\u6765\u63a7\u5236\u7c7b\u7684\u9897\u7c92\u5ea6\u5927\u5c0f\u3002<\/p>\n<p>\u5b98\u65b9\u5b9a\u4e49&#xff1a;<\/p>\n<p>Every object should have a single responsibility, and that responsibility should be entirely encapsulated by the class.<\/p>\n<p>\u4e00\u4e2a\u5bf9\u8c61\u5e94\u8be5\u53ea\u5305\u542b\u5355\u4e00\u7684\u804c\u8d23&#xff0c;\u5e76\u4e14\u8be5\u804c\u8d23\u88ab\u5b8c\u7f8e\u5730\u5c01\u88c5\u5728\u4e00\u4e2a\u7c7b\u4e2d\u3002<\/p>\n<p>\u53e6\u4e00\u79cd\u66f4\u5b9e\u7528\u7684\u8868\u8ff0&#xff08;Robert C. Martin&#xff09;&#xff1a;<\/p>\n<p>A class should have only one reason to change.<\/p>\n<p>\u4e00\u4e2a\u7c7b\u5e94\u8be5\u53ea\u6709\u4e00\u4e2a\u5f15\u8d77\u5b83\u53d8\u5316\u7684\u539f\u56e0\u3002<\/p>\n<p>&#x1f4ac; \u4eba\u8bdd\u7248 &#034;\u804c\u8d23&#034;\u4e0d\u662f&#034;\u65b9\u6cd5\u5c11&#034;&#xff0c;\u800c\u662f&#034;\u53d8\u5316\u7684\u6765\u6e90&#034;\u53ea\u6709\u4e00\u4e2a\u3002 \u4e00\u4e2a\u7c7b\u91cc&#xff0c;\u5982\u679c\u56e0\u4e3a&#034;\u6570\u636e\u5e93\u6362\u4e86&#034;\u8981\u6539\u5b83&#xff0c;\u56e0\u4e3a&#034;\u4e1a\u52a1\u89c4\u5219\u53d8\u4e86&#034;\u4e5f\u8981\u6539\u5b83&#xff0c;\u56e0\u4e3a&#034;\u62a5\u8868\u683c\u5f0f\u6539\u4e86&#034;\u8fd8\u8981\u6539\u5b83 \u2014\u2014 \u90a3\u5b83\u5c31\u6709\u4e09\u4e2a\u804c\u8d23\u3002 \u53cd\u8fc7\u6765\u8bf4&#xff1a;\u5982\u679c\u4e24\u4e2a\u529f\u80fd\u6c38\u8fdc\u662f\u540c\u65f6\u53d8\u3001\u540c\u65f6\u4e0d\u53d8&#xff0c;\u90a3\u653e\u5728\u4e00\u4e2a\u7c7b\u91cc\u4e5f\u6ca1\u95ee\u9898\u3002 \u62c6\u5206\u4e0d\u662f\u76ee\u7684&#xff0c;&#034;\u8ba9\u53d8\u5316\u4e92\u4e0d\u5e72\u6270&#034;\u624d\u662f\u76ee\u7684\u3002<\/p>\n<h5 id=\"%E5%8F%8D%E4%BE%8B%EF%BC%9ARoleDataOperation\">\u53cd\u4f8b&#xff1a;RoleDataOperation<\/h5>\n<p>RoleDataOperation \u7c7b\u540c\u65f6\u627f\u8f7d\u4e86\u4e09\u79cd\u529f\u80fd\u804c\u8d23&#xff1a;<\/p>\n<li>\n<p>\u6570\u636e\u5e93\u8fde\u63a5 \u2014\u2014 \u62fc\u8fde\u63a5\u4e32\u3001\u52a0\u8f7d\u9a71\u52a8\u3001\u7ba1\u8d26\u53f7\u5bc6\u7801\u3001\u5f00\u5173\u8fde\u63a5<\/p>\n<\/li>\n<li>\n<p>\u6570\u636e\u5e93\u6570\u636e\u64cd\u4f5c \u2014\u2014 \u5bf9 Role \u8868\u505a\u589e\u5220\u6539\u67e5<\/p>\n<\/li>\n<li>\n<p>\u5b58\u6863\u6570\u636e\u4e1a\u52a1\u64cd\u4f5c \u2014\u2014 \u5b58\u6863\u6821\u9a8c\u3001\u5e8f\u5217\u5316\u3001\u52a0\u9501\u3001\u8bfb\u6863\u6570\u636e\u4fee\u590d\u7b49\u4e1a\u52a1\u89c4\u5219<\/p>\n<\/li>\n<p>\/\/ \u274c \u8fdd\u53cd SRP&#xff1a;\u4e00\u4e2a\u7c7b\u4e09\u4e2a\u53d8\u5316\u7684\u6765\u6e90<br \/>\nclass RoleDataOperation {<br \/>\npublic:<br \/>\n \u00a0 \u00a0\/\/ \u2500\u2500 \u804c\u8d23\u4e00&#xff1a;\u6570\u636e\u5e93\u8fde\u63a5 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500<br \/>\n \u00a0 \u00a0Connection* getConnection() {<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u62fc URL\u3001\u52a0\u8f7d\u9a71\u52a8\u3001\u8d26\u53f7\u5bc6\u7801\u2026\u2026<br \/>\n \u00a0  }<br \/>\n \u00a0 \u00a0void closeConnection();<br \/>\n\u200b<br \/>\n \u00a0 \u00a0\/\/ \u2500\u2500 \u804c\u8d23\u4e8c&#xff1a;\u6570\u636e\u5e93\u6570\u636e\u64cd\u4f5c&#xff08;CRUD&#xff09;\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500<br \/>\n \u00a0 \u00a0void insert(const Role&amp; role);<br \/>\n \u00a0 \u00a0void update(const Role&amp; role);<br \/>\n \u00a0 \u00a0void deleteById(int id);<br \/>\n \u00a0 \u00a0Role \u00a0selectById(int id);<br \/>\n\u200b<br \/>\n \u00a0 \u00a0\/\/ \u2500\u2500 \u804c\u8d23\u4e09&#xff1a;\u5b58\u6863\u4e1a\u52a1\u903b\u8f91 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500<br \/>\n \u00a0 \u00a0void saveRole(Role&amp; role) {<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u5408\u6cd5\u6027\u6821\u9a8c \u2192 \u5e8f\u5217\u5316 \u2192 \u52a0\u5206\u5e03\u5f0f\u9501 \u2192 \u8c03 insert\/update<br \/>\n \u00a0  }<br \/>\n \u00a0 \u00a0Role loadRole(int id) {<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u8c03 selectById \u2192 \u53cd\u5e8f\u5217\u5316 \u2192 \u65e7\u7248\u672c\u6570\u636e\u517c\u5bb9\u4fee\u590d<br \/>\n \u00a0  }<br \/>\n}; <\/p>\n<p>\u5b83\u6709\u4ec0\u4e48\u95ee\u9898&#xff1f;<\/p>\n<table>\n<tr>\u53d8\u5316\u6765\u6e90\u8981\u6539\u8fd9\u4e2a\u7c7b\u5417<\/tr>\n<tbody>\n<tr>\n<td>\u6570\u636e\u5e93\u4ece MySQL \u6362\u6210 PostgreSQL<\/td>\n<td>\u2705 \u8981\u6539<\/td>\n<\/tr>\n<tr>\n<td>Role \u8868\u52a0\u4e86\u4e00\u4e2a\u5b57\u6bb5<\/td>\n<td>\u2705 \u8981\u6539<\/td>\n<\/tr>\n<tr>\n<td>\u5b58\u6863\u89c4\u5219\u6539\u4e86&#xff08;\u6bd4\u5982\u8981\u52a0\u5bc6&#xff09;<\/td>\n<td>\u2705 \u8981\u6539<\/td>\n<\/tr>\n<tr>\n<td>\u60f3\u628a\u5b58\u6863\u903b\u8f91\u590d\u7528\u5230\u53e6\u4e00\u4e2a\u6a21\u5757<\/td>\n<td>\u274c \u65e0\u6cd5\u590d\u7528&#xff0c;\u88ab\u8fde\u63a5\u548c CRUD \u7ed1\u6b7b\u4e86<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u56db\u4e2a\u95ee\u9898\u91cc\u4e09\u4e2a\u8981\u6539\u540c\u4e00\u4e2a\u7c7b \u2014\u2014 \u8fd9\u5c31\u662f&#034;\u804c\u8d23\u8fc7\u591a&#034;\u7684\u4fe1\u53f7\u3002<\/p>\n<h5 id=\"%E9%87%8D%E6%9E%84%E5%90%8E\">\u91cd\u6784\u540e<\/h5>\n<p>\/\/ \u2705 \u804c\u8d23\u4e00&#xff1a;\u53ea\u7ba1\u8fde\u63a5<br \/>\nclass DBUtil {<br \/>\npublic:<br \/>\n \u00a0 \u00a0static Connection* getConnection();<br \/>\n \u00a0 \u00a0static void closeConnection(Connection* conn);<br \/>\n};<br \/>\n\u200b<br \/>\n\/\/ \u2705 \u804c\u8d23\u4e8c&#xff1a;\u53ea\u7ba1 Role \u7684 CRUD<br \/>\nclass RoleDAO {<br \/>\npublic:<br \/>\n \u00a0 \u00a0void \u00a0insert(const Role&amp; role);<br \/>\n \u00a0 \u00a0void \u00a0update(const Role&amp; role);<br \/>\n \u00a0 \u00a0void \u00a0deleteById(int id);<br \/>\n \u00a0 \u00a0Role \u00a0selectById(int id);<br \/>\n};<br \/>\n\u200b<br \/>\n\/\/ \u2705 \u804c\u8d23\u4e09&#xff1a;\u53ea\u7ba1\u5b58\u6863\u4e1a\u52a1<br \/>\nclass RoleDataOperation {<br \/>\n \u00a0 \u00a0DBUtil* \u00a0m_db;<br \/>\n \u00a0 \u00a0RoleDAO* m_dao;<br \/>\npublic:<br \/>\n \u00a0 \u00a0void saveRole(Role&amp; role) {<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0checkRole(role); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u4e1a\u52a1\u6821\u9a8c<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0std::string blob &#061; serialize(role);<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0m_dao-&gt;insert(role); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u53ea\u8c03 DAO&#xff0c;\u4e0d\u78b0\u8fde\u63a5\u7ec6\u8282<br \/>\n \u00a0  }<br \/>\n \u00a0 \u00a0Role loadRole(int id) {<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0Role r &#061; m_dao-&gt;selectById(id);<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0fixLegacyData(r); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ \u65e7\u7248\u672c\u517c\u5bb9<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0return r;<br \/>\n \u00a0  }<br \/>\n}; <\/p>\n<p>\u91cd\u6784\u6536\u76ca&#xff08;\u4eba\u8bdd\u7248&#xff09;&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u6570\u636e\u5e93\u6362\u4ee3 \u2192 \u53ea\u52a8 DBUtil<\/p>\n<\/li>\n<li>\n<p>\u8868\u7ed3\u6784\u53d8\u66f4 \u2192 \u53ea\u52a8 RoleDAO<\/p>\n<\/li>\n<li>\n<p>\u5b58\u6863\u89c4\u5219\u8c03\u6574 \u2192 \u53ea\u52a8 RoleDataOperation<\/p>\n<\/li>\n<li>\n<p>\u5b58\u6863\u903b\u8f91\u8981\u590d\u7528\u5230&#034;\u89d2\u8272\u80cc\u5305&#034;\u6a21\u5757 \u2192 \u76f4\u63a5\u62ff\u8d70 RoleDataOperation<\/p>\n<\/li>\n<\/ul>\n<p>&#x1f4ac; \u7c7b\u6bd4 \u4e00\u5bb6\u9910\u5385\u91cc&#xff0c;\u91c7\u8d2d\u5458\u3001\u53a8\u5e08\u3001\u670d\u52a1\u5458\u662f\u4e09\u4e2a\u4eba\u3002\u5982\u679c\u8ba9\u540c\u4e00\u4e2a\u4eba\u5168\u5305&#xff1a;\u91c7\u8d2d\u51fa\u95ee\u9898&#xff0c;\u5168\u5e97\u505c\u6446&#xff1b;\u53a8\u5e08\u8bf7\u5047&#xff0c;\u6ca1\u4eba\u63a5\u5f85\u5ba2\u4eba\u3002 \u800c\u4e14&#034;\u8981\u6362\u4e2a\u4f9b\u5e94\u5546&#034;\u8fd9\u4ef6\u4e8b&#xff0c;\u672c\u4e0d\u8be5\u60ca\u52a8\u53a8\u5e08\u3002<\/p>\n<h5 id=\"%E2%9A%A0%EF%B8%8F%20%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9\">\u26a0\ufe0f \u6ce8\u610f\u4e8b\u9879<\/h5>\n<ul>\n<li>\n<p>\u804c\u8d23\u5212\u5206\u6ca1\u6709\u7edd\u5bf9\u6807\u51c6&#xff0c;\u53d6\u51b3\u4e8e\u4e1a\u52a1\u573a\u666f\u548c\u53d8\u5316\u9891\u7387&#xff0c;\u4e0d\u8981\u4e3a\u4e86&#034;\u62c6&#034;\u800c\u62c6\u3002<\/p>\n<\/li>\n<li>\n<p>\u65b9\u6cd5\u7ea7\u4e5f\u9002\u7528 SRP&#xff1a;\u4e00\u4e2a\u65b9\u6cd5\u540c\u65f6\u5e72\u4e86&#034;\u53d6\u6570\u636e &#043; \u7b97\u4e1a\u52a1 &#043; \u62fc\u8f93\u51fa&#034;&#xff0c;\u540c\u6837\u8be5\u62c6\u3002<\/p>\n<\/li>\n<li>\n<p>\u5e38\u89c1\u9519\u8bef&#xff1a;\u628a SRP \u7406\u89e3\u6210&#034;\u4e00\u4e2a\u7c7b\u53ea\u80fd\u6709\u4e00\u4e2a public \u65b9\u6cd5&#034; \u2014\u2014 \u90a3\u4f1a\u5bfc\u81f4\u7c7b\u7206\u70b8\u3002<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h4 id=\"1.3.2%20%E5%BC%80%E9%97%AD%E5%8E%9F%E5%88%99%20OCP\" style=\"background-color:transparent\">1.3.2 \u5f00\u95ed\u539f\u5219 OCP<\/h4>\n<p>\u5f00\u95ed\u539f\u5219&#xff08;Open-Closed Principle, OCP&#xff09;\u662f\u9762\u5411\u5bf9\u8c61\u7684\u53ef\u590d\u7528\u8bbe\u8ba1\u7684\u7b2c\u4e00\u5757\u57fa\u77f3&#xff0c;\u5b83\u662f\u6700\u91cd\u8981\u7684\u9762\u5411\u5bf9\u8c61\u8bbe\u8ba1\u539f\u5219\u3002<\/p>\n<p>\u5f00\u95ed\u539f\u5219\u7531 Bertrand Meyer \u4e8e 1988 \u5e74\u63d0\u51fa&#xff0c;\u5176\u5b9a\u4e49\u5982\u4e0b&#xff1a;<\/p>\n<p>Software entities should be open for extension, but closed for modification.<\/p>\n<p>\u8f6f\u4ef6\u5b9e\u4f53\u5e94\u5bf9\u6269\u5c55\u5f00\u653e&#xff0c;\u800c\u5bf9\u4fee\u6539\u5173\u95ed\u3002<\/p>\n<p>\u5728\u5f00\u95ed\u539f\u5219\u7684\u5b9a\u4e49\u4e2d&#xff0c;&#034;\u8f6f\u4ef6\u5b9e\u4f53&#034;\u53ef\u4ee5\u6307\u4e00\u4e2a\u8f6f\u4ef6\u6a21\u5757\u3001\u4e00\u4e2a\u7531\u591a\u4e2a\u7c7b\u7ec4\u6210\u7684\u5c40\u90e8\u7ed3\u6784\u6216\u4e00\u4e2a\u72ec\u7acb\u7684\u7c7b\u3002<\/p>\n<p>\u5f00\u95ed\u539f\u5219\u5c31\u662f\u6307&#xff1a;\u8f6f\u4ef6\u5b9e\u4f53\u5c3d\u91cf\u5728\u4e0d\u4fee\u6539\u6e90\u7801\u7684\u60c5\u51b5\u4e0b\u8fdb\u884c\u6269\u5c55\u3002<\/p>\n<p>\u5f00\u95ed\u539f\u5219\u662f\u8bc4\u4ef7\u57fa\u4e8e\u67d0\u4e2a\u8bbe\u8ba1\u6a21\u5f0f\u8bbe\u8ba1\u7684\u7cfb\u7edf\u662f\u5426\u5177\u5907\u7075\u6d3b\u6027\u548c\u53ef\u6269\u5c55\u6027\u7684\u91cd\u8981\u4f9d\u636e\u3002<\/p>\n<p>&#x1f4ac; \u4eba\u8bdd\u7248 \u5f00\u95ed\u539f\u5219\u8981\u8868\u8fbe\u7684\u662f&#xff1a;\u5f53\u65b0\u9700\u6c42\u6765\u65f6&#xff0c;\u4f60\u53ea&#034;\u52a0\u4ee3\u7801&#034;&#xff0c;\u4e0d&#034;\u6539\u8001\u4ee3\u7801&#034;\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48\u5f3a\u8c03&#034;\u4e0d\u6539\u8001\u4ee3\u7801&#034;&#xff1f;\u56e0\u4e3a\u8001\u4ee3\u7801\u662f\u5df2\u7ecf\u6d4b\u8fc7\u3001\u5df2\u7ecf\u4e0a\u7ebf\u3001\u6b63\u5728\u8dd1\u7684\u3002\u6539\u4e00\u884c&#xff0c;\u5c31\u53ef\u80fd\u8981\u56de\u5f52\u6d4b\u8bd5\u6574\u4e2a\u6a21\u5757 \u2014\u2014 \u800c\u7eaf\u65b0\u589e\u7684\u4ee3\u7801&#xff0c;\u98ce\u9669\u8303\u56f4\u662f\u53ef\u63a7\u7684\u3002<\/p>\n<p>\u26a0\ufe0f \u4f46\u8981\u7834\u9664\u4e00\u4e2a\u8bef\u89e3&#xff1a;\u7edd\u5bf9\u7684\u5f00\u95ed\u662f\u4e0d\u53ef\u80fd\u7684\u3002 \u4f60\u4e0d\u53ef\u80fd\u9884\u77e5\u6240\u6709\u53d8\u5316\u3002\u6b63\u786e\u7684\u59ff\u52bf\u662f&#xff1a;\u8bc6\u522b\u51fa\u8fd9\u4e2a\u7cfb\u7edf\u4e2d&#034;\u53ef\u9884\u671f\u7684\u53d8\u5316\u70b9&#034;&#xff0c;\u628a\u5b83\u8bbe\u8ba1\u6210\u6269\u5c55\u70b9&#xff1b;\u5bf9\u4e8e\u9884\u6599\u4e4b\u5916\u7684\u53d8\u5316&#xff0c;\u5766\u7136\u63a5\u53d7\u4fee\u6539\u3002 \u4e3a\u60f3\u8c61\u4e2d\u7684\u53d8\u5316\u8fc7\u5ea6\u8bbe\u8ba1&#xff08;\u6bd4\u5982\u5230\u5904\u52a0\u62bd\u8c61\u5c42&#xff09;&#xff0c;\u6bd4\u4e0d\u8bbe\u8ba1\u66f4\u7cdf \u2014\u2014 \u8fd9\u53eb\u8fc7\u5ea6\u5de5\u7a0b&#xff08;Over-engineering&#xff09;\u3002<\/p>\n<h5 id=\"%E5%8F%8D%E4%BE%8B%EF%BC%9A%E5%88%B7%E6%80%AA%E5%A1%94\">\u53cd\u4f8b&#xff1a;\u5237\u602a\u5854<\/h5>\n<p>\u5047\u8bbe\u6709\u4e00\u4e2a&#034;\u5237\u602a\u5854&#034;&#xff0c;createMonster \u7528\u5982\u4e0b\u5b9e\u73b0&#xff1a;<\/p>\n<p>\/\/ \u274c \u8fdd\u53cd OCP<br \/>\nMonster* createMonster(int type) {<br \/>\n \u00a0 \u00a0Monster* monster &#061; nullptr;<br \/>\n\u200b<br \/>\n \u00a0 \u00a0if (type &#061;&#061; 1) {<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0Boss* boss &#061; new Boss();<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0monster &#061; boss;<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0\/\/ Boss \u6269\u5c55\u5904\u7406<br \/>\n \u00a0  } else if (type &#061;&#061; 2) {<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0Elite* elite &#061; new Elite();<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0monster &#061; elite;<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u7cbe\u82f1\u6269\u5c55\u5904\u7406<br \/>\n \u00a0  } else {<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0Soldier* soldier &#061; new Soldier();<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0monster &#061; soldier;<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u666e\u901a\u602a\u6269\u5c55\u5904\u7406<br \/>\n \u00a0  }<br \/>\n\u200b<br \/>\n \u00a0 \u00a0\/\/ \u602a\u7269\u5bf9\u8c61\u901a\u7528\u5904\u7406&#xff08;\u521d\u59cb\u5316\u6570\u636e\u3001\u6dfb\u52a0\u5230\u5bf9\u8c61\u6c60&#xff09;<br \/>\n \u00a0 \u00a0monster-&gt;init();<br \/>\n \u00a0 \u00a0m_pool.add(monster);<br \/>\n \u00a0 \u00a0return monster;<br \/>\n} <\/p>\n<p>\u6b64\u65f6\u5982\u679c\u65b0\u589e\u4e00\u79cd\u7c7b\u578b\u7684\u602a\u7269&#xff0c;\u90a3\u4e48\u5fc5\u7136\u4f1a\u4fee\u6539 createMonster \u7684\u4e1a\u52a1\u903b\u8f91&#xff0c;\u4e0d\u7b26\u5408\u5f00\u95ed\u539f\u5219\u3002<\/p>\n<p>\u66f4\u7cdf\u7684\u662f&#xff0c;\u6539\u52a8\u5f15\u5165\u4e86\u56de\u5f52\u98ce\u9669&#xff1a;\u4f60\u53ea\u662f\u5728\u672b\u5c3e\u52a0\u4e86\u4e2a else if&#xff0c;\u5374\u5fc5\u987b\u91cd\u65b0\u9a8c\u8bc1 type &#061;&#061; 1 \/ type &#061;&#061; 2 \/ else \u4e09\u6761\u8001\u5206\u652f\u6709\u6ca1\u6709\u88ab\u5f71\u54cd\u3002<\/p>\n<p>&#x1f4ac; \u8fd9\u6bb5\u4ee3\u7801\u7684\u672c\u8d28\u95ee\u9898 \u5b83\u628a&#034;\u6709\u54ea\u4e9b\u602a\u7269&#034;\u548c&#034;\u602a\u7269\u600e\u4e48\u521b\u5efa\u3001\u600e\u4e48\u901a\u7528\u5904\u7406&#034;\u6df7\u5728\u4e86\u4e00\u4e2a\u51fd\u6570\u91cc\u3002 &#034;\u6709\u54ea\u4e9b\u602a\u7269&#034;\u662f\u4f1a\u53d8\u7684&#xff1b;&#034;\u521b\u5efa\u5b8c\u8981\u521d\u59cb\u5316\u3001\u8981\u8fdb\u5bf9\u8c61\u6c60&#034;\u662f\u4e0d\u53d8\u7684\u3002 \u5f00\u95ed\u539f\u5219\u7684\u505a\u6cd5\u5c31\u662f&#xff1a;\u628a\u4f1a\u53d8\u7684\u90e8\u5206\u62bd\u51fa\u53bb&#xff0c;\u8ba9\u4e0d\u53d8\u7684\u9aa8\u67b6\u7559\u4e0b\u6765\u3002<\/p>\n<h5 id=\"%E9%87%8D%E6%9E%84%EF%BC%9A%E5%A4%9A%E6%80%81%20%2B%20%E6%B3%A8%E5%86%8C%E8%A1%A8\">\u91cd\u6784&#xff1a;\u591a\u6001 &#043; \u6ce8\u518c\u8868<\/h5>\n<p>\/\/ \u2500\u2500 \u62bd\u8c61\u5c42&#xff1a;\u4e0d\u53d8\u7684\u90e8\u5206 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500<br \/>\nclass Monster {<br \/>\npublic:<br \/>\n \u00a0 \u00a0virtual ~Monster() &#061; default;<br \/>\n \u00a0 \u00a0virtual void init() &#061; 0; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u5404\u81ea\u7684\u6269\u5c55\u5904\u7406<br \/>\n \u00a0 \u00a0virtual Monster* clone() const &#061; 0;<br \/>\n};<br \/>\n\u200b<br \/>\n\/\/ \u2500\u2500 \u5177\u4f53\u602a\u7269&#xff1a;\u4f1a\u53d8\u7684\u90e8\u5206&#xff0c;\u7528&#034;\u65b0\u589e\u7c7b&#034;\u6765\u627f\u8f7d \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500<br \/>\nclass Boss : public Monster {<br \/>\npublic:<br \/>\n \u00a0 \u00a0void init() override { \/* Boss \u6269\u5c55\u5904\u7406 *\/ }<br \/>\n \u00a0 \u00a0Monster* clone() const override { return new Boss(*this); }<br \/>\n};<br \/>\n\u200b<br \/>\nclass Elite : public Monster {<br \/>\npublic:<br \/>\n \u00a0 \u00a0void init() override { \/* \u7cbe\u82f1\u6269\u5c55\u5904\u7406 *\/ }<br \/>\n \u00a0 \u00a0Monster* clone() const override { return new Elite(*this); }<br \/>\n};<br \/>\n\u200b<br \/>\nclass Soldier : public Monster {<br \/>\npublic:<br \/>\n \u00a0 \u00a0void init() override { \/* \u666e\u901a\u602a\u6269\u5c55\u5904\u7406 *\/ }<br \/>\n \u00a0 \u00a0Monster* clone() const override { return new Soldier(*this); }<br \/>\n};<br \/>\n\u200b<br \/>\n\/\/ \u2500\u2500 \u5de5\u5382&#xff1a;\u521b\u5efa\u903b\u8f91\u7684\u9aa8\u67b6&#xff0c;\u2605 \u4ece\u6b64\u4e0d\u518d\u6539\u52a8 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500<br \/>\nclass MonsterFactory {<br \/>\npublic:<br \/>\n \u00a0 \u00a0using Creator &#061; std::function&lt;Monster*()&gt;;<br \/>\n\u200b<br \/>\n \u00a0 \u00a0static MonsterFactory&amp; instance() {<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0static MonsterFactory inst;<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0return inst;<br \/>\n \u00a0  }<br \/>\n\u200b<br \/>\n \u00a0 \u00a0void registerMonster(int type, Creator creator) {<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0m_registry[type] &#061; std::move(creator);<br \/>\n \u00a0  }<br \/>\n\u200b<br \/>\n \u00a0 \u00a0Monster* createMonster(int type) {<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0auto it &#061; m_registry.find(type);<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0if (it &#061;&#061; m_registry.end()) return nullptr;<br \/>\n\u200b<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0Monster* monster &#061; it-&gt;second()(); \u00a0 \/\/ \u591a\u6001\u521b\u5efa&#xff0c;\u4e0d\u9700\u8981 if-else<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0monster-&gt;init(); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \/\/ \u901a\u7528\u5904\u7406&#xff0c;\u9aa8\u67b6\u4e0d\u53d8<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0m_pool.add(monster);<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0return monster;<br \/>\n \u00a0  }<br \/>\n\u200b<br \/>\nprivate:<br \/>\n \u00a0 \u00a0std::map&lt;int, Creator&gt; m_registry;<br \/>\n}; <\/p>\n<p>\u65b0\u589e\u4e00\u79cd\u602a\u7269\u65f6&#xff08;\u6bd4\u5982&#034;\u68a6\u9b47&#034;&#xff09;&#xff1a;<\/p>\n<p>class Nightmare : public Monster {<br \/>\npublic:<br \/>\n \u00a0 \u00a0void init() override { \/* \u68a6\u9b47\u6269\u5c55\u5904\u7406 *\/ }<br \/>\n \u00a0 \u00a0Monster* clone() const override { return new Nightmare(*this); }<br \/>\n};<br \/>\n\u200b<br \/>\n\/\/ \u53ea\u9700\u65b0\u589e\u4e00\u4e2a\u7c7b &#043; \u4e00\u884c\u6ce8\u518c&#xff0c;createMonster \u4e00\u4e2a\u5b57\u90fd\u4e0d\u52a8<br \/>\nstatic bool _reg &#061; [] {<br \/>\n \u00a0 \u00a0MonsterFactory::instance().registerMonster(4, [] { return new Nightmare(); });<br \/>\n \u00a0 \u00a0return true;<br \/>\n}(); <\/p>\n<p>\u5bf9\u6bd4&#xff1a;<\/p>\n<table>\n<tr>\u8fdd\u53cd OCP\u7b26\u5408 OCP<\/tr>\n<tbody>\n<tr>\n<td>\u65b0\u589e\u602a\u7269<\/td>\n<td>\u6539 createMonster \u52a0 else if<\/td>\n<td>\u65b0\u589e\u4e00\u4e2a\u7c7b &#043; \u6ce8\u518c<\/td>\n<\/tr>\n<tr>\n<td>\u8001\u5206\u652f\u56de\u5f52\u98ce\u9669<\/td>\n<td>\u6709<\/td>\n<td>\u65e0<\/td>\n<\/tr>\n<tr>\n<td>\u901a\u7528\u5904\u7406\u903b\u8f91<\/td>\n<td>\u6563\u5728 if-else \u4e4b\u95f4<\/td>\n<td>\u96c6\u4e2d\u5728\u5de5\u5382\u91cc<\/td>\n<\/tr>\n<tr>\n<td>\u914d\u7f6e\u6587\u4ef6\u9a71\u52a8<\/td>\n<td>\u505a\u4e0d\u5230<\/td>\n<td>\u53ef\u914d\u5408 conf.properties &#043; \u53cd\u5c04&#xff0c;\u6539\u914d\u7f6e\u91cd\u542f\u5373\u53ef<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&#x1f4ac; \u4eba\u8bdd\u7248\u603b\u7ed3 \u5f00\u95ed\u539f\u5219\u4e0d\u662f&#034;\u7981\u6b62\u4fee\u6539\u4ee3\u7801&#034;&#xff0c;\u800c\u662f&#034;\u8ba9\u5927\u591a\u6570\u53d8\u5316\u4ee5\u65b0\u589e\u7684\u65b9\u5f0f\u53d1\u751f&#034;\u3002 \u5224\u65ad\u6807\u51c6\u5f88\u7b80\u5355&#xff1a;\u8fd9\u6b21\u9700\u6c42\u53d8\u66f4&#xff0c;\u6211\u6539\u7684\u662f&#034;\u65b0\u6587\u4ef6&#034;\u8fd8\u662f&#034;\u8001\u6587\u4ef6&#034;&#xff1f;<\/p>\n<h5 id=\"%E2%9A%A0%EF%B8%8F%20%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9\">\u26a0\ufe0f \u6ce8\u610f\u4e8b\u9879<\/h5>\n<ul>\n<li>\n<p>\u62bd\u8c61\u4e0d\u80fd\u51ed\u7a7a\u8bbe\u8ba1&#xff0c;\u8981\u4ece\u5df2\u6709\u7684\u5177\u4f53\u5b9e\u73b0\u4e2d\u63d0\u70bc&#xff08;\u5148\u5199\u4e24\u4e09\u4e2a\u5177\u4f53\u7c7b&#xff0c;\u518d\u62bd\u516c\u5171\u7236\u7c7b&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u4e0d\u8981\u4e3a&#034;\u5c06\u6765\u53ef\u80fd\u6709\u7684\u53d8\u5316&#034;\u63d0\u524d\u9020\u62bd\u8c61\u5c42 \u2014\u2014 \u90a3\u53eb\u731c\u6d4b&#xff0c;\u4e0d\u53eb\u8bbe\u8ba1\u3002<\/p>\n<\/li>\n<li>\n<p>\u5e38\u89c1\u5b9e\u73b0\u624b\u6bb5&#xff1a;\u62bd\u8c61\u7c7b\/\u63a5\u53e3 &#043; \u591a\u6001 &#043; \u914d\u7f6e\u6587\u4ef6 &#043; \u53cd\u5c04\/\u6ce8\u518c\u8868&#xff08;\u540e\u9762\u51e0\u79cd\u8bbe\u8ba1\u6a21\u5f0f\u4f1a\u53cd\u590d\u7528\u5230&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h4 id=\"1.3.3%20%E9%87%8C%E6%B0%8F%E6%9B%BF%E6%8D%A2%E5%8E%9F%E5%88%99%20LSP\" style=\"background-color:transparent\">1.3.3 \u91cc\u6c0f\u66ff\u6362\u539f\u5219 LSP<\/h4>\n<p>\u91cc\u6c0f\u66ff\u6362\u539f\u5219&#xff08;Liskov Substitution Principle, LSP&#xff09;\u7531 2008 \u5e74\u56fe\u7075\u5956\u5f97\u4e3b\u3001\u7f8e\u56fd\u7b2c\u4e00\u4f4d\u8ba1\u7b97\u673a\u79d1\u5b66\u5973\u535a\u58eb Barbara Liskov&#xff08;\u82ad\u82ad\u62c9\u00b7\u5229\u65af\u79d1\u592b&#xff09;\u6559\u6388\u548c\u5361\u5185\u57fa\u00b7\u6885\u9686\u5927\u5b66 Jeannette Wing&#xff08;\u73cd\u59ae\u7279\u00b7\u6e29&#xff09;\u6559\u6388\u4e8e 1994 \u5e74\u63d0\u51fa\u3002<\/p>\n<p>\u4e25\u683c\u8868\u8ff0&#xff1a;<\/p>\n<p>If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2 then S is a subtype of T.<\/p>\n<p>\u5982\u679c\u5bf9\u6bcf\u4e00\u4e2a\u7c7b\u578b\u4e3a S \u7684\u5bf9\u8c61 o1&#xff0c;\u90fd\u6709\u7c7b\u578b\u4e3a T \u7684\u5bf9\u8c61 o2&#xff0c;\u4f7f\u5f97\u4ee5 T \u5b9a\u4e49\u7684\u6240\u6709\u7a0b\u5e8f P \u5728\u6240\u6709\u7684\u5bf9\u8c61 o1 \u66ff\u6362 o2 \u65f6&#xff0c;\u7a0b\u5e8f P \u7684\u884c\u4e3a\u6ca1\u6709\u53d8\u5316&#xff0c;\u90a3\u4e48\u7c7b\u578b S \u662f\u7c7b\u578b T \u7684\u5b50\u7c7b\u578b\u3002<\/p>\n<p>\u8fd9\u4e2a\u5b9a\u4e49\u6bd4\u8f83\u62d7\u53e3\u4e14\u96be\u4ee5\u7406\u89e3&#xff0c;\u56e0\u6b64\u6211\u4eec\u4e00\u822c\u4f7f\u7528\u5b83\u7684\u53e6\u4e00\u4e2a\u901a\u4fd7\u7248\u5b9a\u4e49&#xff1a;<\/p>\n<p>Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it.<\/p>\n<p>\u6240\u6709\u5f15\u7528\u57fa\u7c7b&#xff08;\u7236\u7c7b&#xff09;\u7684\u5730\u65b9\u5fc5\u987b\u80fd\u900f\u660e\u5730\u4f7f\u7528\u5176\u5b50\u7c7b\u7684\u5bf9\u8c61\u3002<\/p>\n<p>&#x1f4ac; \u4eba\u8bdd\u7248 \u628a\u7236\u7c7b\u6362\u6210\u5b50\u7c7b&#xff0c;\u8c03\u7528\u65b9\u7684\u4ee3\u7801\u4e00\u884c\u90fd\u4e0d\u7528\u6539&#xff0c;\u7a0b\u5e8f\u884c\u4e3a\u4e5f\u4e0d\u80fd\u53d8\u3002<\/p>\n<p>\u6362\u53e5\u8bdd\u8bf4&#xff1a;\u5b50\u7c7b\u5fc5\u987b&#034;\u5c11\u8981\u6c42\u3001\u591a\u4fdd\u8bc1&#034;&#xff0c;\u7edd\u4e0d\u80fd&#034;\u591a\u8981\u6c42\u3001\u5c11\u4fdd\u8bc1&#034;\u3002<\/p>\n<ul>\n<li>\n<p>\u8f93\u5165&#xff08;\u524d\u7f6e\u6761\u4ef6&#xff09;\u53ea\u80fd\u653e\u5bbd&#xff0c;\u4e0d\u80fd\u6536\u7d27 \u2192 \u7236\u7c7b\u63a5\u53d7\u4efb\u610f\u8def\u5f84&#xff0c;\u5b50\u7c7b\u4e0d\u80fd\u8981\u6c42&#034;\u5fc5\u987b .mp3 \u7ed3\u5c3e&#034;<\/p>\n<\/li>\n<li>\n<p>\u8f93\u51fa&#xff08;\u540e\u7f6e\u6761\u4ef6&#xff09;\u53ea\u80fd\u52a0\u5f3a&#xff0c;\u4e0d\u80fd\u653e\u677e \u2192 \u7236\u7c7b\u4fdd\u8bc1&#034;\u8fd4\u56de\u975e\u7a7a\u5217\u8868&#034;&#xff0c;\u5b50\u7c7b\u4e0d\u80fd\u8fd4\u56de\u7a7a<\/p>\n<\/li>\n<\/ul>\n<p>\u8fd8\u6709\u4e00\u53e5\u5927\u767d\u8bdd&#xff1a;\u7ee7\u627f\u4e0d\u662f&#034;\u8e6d&#034;\u7236\u7c7b\u7684\u4ee3\u7801\u7701\u70b9\u4e8b&#xff0c;\u800c\u662f\u5f53\u4f17\u7acb\u4e0b\u5b57\u636e \u2014\u2014&#034;\u8c01\u8981\u4e00\u4e2a A&#xff0c;\u62ff\u6211\u53bb\u9876\u5c31\u884c\u3002&#034; \u9876\u4e0d\u4f4f\u8fd9\u4e2a\u4f4d\u7f6e&#xff0c;\u5c31\u522b\u7ee7\u627f\u3002<\/p>\n<h5 id=\"%E6%9B%BF%E6%8D%A2%E8%A7%84%E5%88%99\" style=\"background-color:transparent\">\u66ff\u6362\u89c4\u5219<\/h5>\n<p>\u91cc\u6c0f\u66ff\u6362\u539f\u5219\u544a\u8bc9\u6211\u4eec&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u6b63\u5411\u6210\u7acb&#xff1a;\u5c06\u4e00\u4e2a\u57fa\u7c7b\u5bf9\u8c61\u66ff\u6362\u6210\u5b83\u7684\u5b50\u7c7b\u5bf9\u8c61&#xff0c;\u7a0b\u5e8f\u5c06\u4e0d\u4f1a\u4ea7\u751f\u4efb\u4f55\u9519\u8bef\u548c\u5f02\u5e38\u3002<\/p>\n<\/li>\n<li>\n<p>\u53cd\u5411\u4e0d\u6210\u7acb&#xff1a;\u5982\u679c\u4e00\u4e2a\u8f6f\u4ef6\u5b9e\u4f53\u4f7f\u7528\u7684\u662f\u4e00\u4e2a\u5b50\u7c7b\u5bf9\u8c61&#xff0c;\u90a3\u4e48\u5b83\u4e0d\u4e00\u5b9a\u80fd\u591f\u4f7f\u7528\u57fa\u7c7b\u5bf9\u8c61\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u4f8b\u5982&#xff1a;<\/p>\n<li>\n<p>\u6709\u4e24\u4e2a\u7c7b&#xff0c;\u4e00\u4e2a\u7c7b\u4e3a BaseClass&#xff0c;\u53e6\u4e00\u4e2a\u662f SubClass \u7c7b&#xff0c;\u5e76\u4e14 SubClass \u7c7b\u662f BaseClass \u7c7b\u7684\u5b50\u7c7b\u3002\u90a3\u4e48\u4e00\u4e2a\u65b9\u6cd5\u5982\u679c\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a BaseClass \u7c7b\u578b\u7684\u57fa\u7c7b\u5bf9\u8c61 base \u7684\u8bdd&#xff0c;\u5982&#xff1a;method1(base)&#xff0c;\u90a3\u4e48\u5b83\u5fc5\u7136\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a BaseClass \u7c7b\u578b\u7684\u5b50\u7c7b\u5bf9\u8c61 sub&#xff0c;method1(sub) \u80fd\u591f\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n<\/li>\n<li>\n<p>\u53cd\u8fc7\u6765\u7684\u4ee3\u6362\u4e0d\u6210\u7acb\u3002\u5982\u679c\u4e00\u4e2a\u65b9\u6cd5 method2 \u63a5\u53d7 BaseClass \u7c7b\u578b\u7684\u5b50\u7c7b\u5bf9\u8c61 sub \u4e3a\u53c2\u6570&#xff1a;method2(sub)&#xff0c;\u90a3\u4e48\u4e00\u822c\u800c\u8a00\u4e0d\u53ef\u4ee5\u6709 method2(base)&#xff0c;\u9664\u975e\u662f\u91cd\u8f7d\u65b9\u6cd5\u3002<\/p>\n<\/li>\n<p>\u91cc\u6c0f\u66ff\u6362\u539f\u5219\u662f\u5b9e\u73b0\u5f00\u95ed\u539f\u5219\u7684\u91cd\u8981\u65b9\u5f0f\u4e4b\u4e00\u3002\u7531\u4e8e\u4f7f\u7528\u57fa\u7c7b\u5bf9\u8c61\u7684\u5730\u65b9\u90fd\u53ef\u4ee5\u4f7f\u7528\u5b50\u7c7b\u5bf9\u8c61&#xff0c;\u56e0\u6b64\u5728\u7a0b\u5e8f\u4e2d\u5c3d\u91cf\u4f7f\u7528\u57fa\u7c7b\u7c7b\u578b\u6765\u5bf9\u5bf9\u8c61\u8fdb\u884c\u5b9a\u4e49&#xff0c;\u800c\u5728\u8fd0\u884c\u65f6\u518d\u786e\u5b9a\u5176\u5b50\u7c7b\u7c7b\u578b&#xff0c;\u7528\u5b50\u7c7b\u5bf9\u8c61\u6765\u66ff\u6362\u7236\u7c7b\u5bf9\u8c61\u3002<\/p>\n<p>&#x1f4ac; \u4eba\u8bdd\u7248 \u8fd9\u5c31\u662f&#034;\u591a\u6001&#034;\u80fd\u6210\u7acb\u7684\u5951\u7ea6\u4fdd\u969c\u3002 \u56e0\u4e3a\u4f60\u5199\u597d download(Product* p) \u4e4b\u540e&#xff0c;\u8111\u5b50\u91cc\u5047\u8bbe\u7684\u662f&#034;\u53ea\u8981\u662f\u4e2a Product&#xff0c;\u5c31\u80fd download&#034;\u3002 LSP \u5c31\u662f\u5728\u8bf4&#xff1a;\u8fd9\u4e2a\u5047\u8bbe&#xff0c;\u5b50\u7c7b\u5fc5\u987b\u7ed9\u6211\u5151\u73b0\u3002<\/p>\n<h5 id=\"%E6%AD%A3%E9%9D%A2%E7%A4%BA%E4%BE%8B%EF%BC%9A%E5%85%BC%E5%AE%B9%20LSP%20%E7%9A%84%E8%AE%BE%E8%AE%A1\" style=\"background-color:transparent\">\u6b63\u9762\u793a\u4f8b&#xff1a;\u517c\u5bb9 LSP \u7684\u8bbe\u8ba1<\/h5>\n<p>\u7c7b\u56fe&#xff1a;<\/p>\n<p class=\"img-center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260919124912-6aae84c84b7b6.png\" \/><\/p>\n<p>\u517c\u5bb9 LSP \u7684 execute \u65b9\u6cd5&#xff1a;<\/p>\n<p>\/\/ \u521b\u5efa Audio \u4ea7\u54c1\u5bf9\u8c61<br \/>\nProduct* audio &#061; new Audio();<br \/>\naudio-&gt;setFilePath(&#034;audio.mp3&#034;);<br \/>\n\u200b<br \/>\n\/\/ \u521b\u5efa Video \u4ea7\u54c1\u5bf9\u8c61<br \/>\nProduct* video &#061; new Video();<br \/>\nvideo-&gt;setFilePath(&#034;video.mp4&#034;);<br \/>\n\u200b<br \/>\n\/\/ \u547c\u53eb\u4e0b\u8f7d\u65b9\u6cd5\u4e0b\u8f7d\u4e0d\u540c\u7684\u4ea7\u54c1<br \/>\ndownload(audio); \u00a0 \/\/ \u2705 \u6b63\u5e38<br \/>\ndownload(video); \u00a0 \/\/ \u2705 \u6b63\u5e38 <\/p>\n<p>\u5bf9\u5e94\u7684\u62bd\u8c61\u4e0e\u5b9e\u73b0&#xff1a;<\/p>\n<p>class Product {<br \/>\npublic:<br \/>\n \u00a0 \u00a0virtual ~Product() &#061; default;<br \/>\n\u200b<br \/>\n \u00a0 \u00a0virtual void setFilePath(const std::string&amp; path) { m_path &#061; path; }<br \/>\n\u200b<br \/>\n \u00a0 \u00a0\/**<br \/>\n \u00a0 \u00a0 * \u6b64\u65b9\u6cd5\u4ec5\u7528\u4e8e\u4e0b\u8f7d&#034;\u53ef\u4e0b\u8f7d\u7684\u4ea7\u54c1&#034;\u3002<br \/>\n \u00a0 \u00a0 * \u5982\u679c\u4ea7\u54c1\u4e0d\u53ef\u4e0b\u8f7d&#xff0c;\u5219\u629b\u51fa\u5f02\u5e38\u3002<br \/>\n \u00a0 \u00a0 *\/<br \/>\n \u00a0 \u00a0virtual void download() {<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0if (!canBeDownloaded())<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0throw std::logic_error(&#034;product can not be downloaded&#034;);<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0\/\/ &#8230; \u771f\u6b63\u4e0b\u8f7d m_path \u6307\u5411\u7684\u8d44\u6e90<br \/>\n \u00a0  }<br \/>\n\u200b<br \/>\nprotected:<br \/>\n \u00a0 \u00a0std::string m_path;<br \/>\n \u00a0 \u00a0virtual bool canBeDownloaded() const { return true; }<br \/>\n};<br \/>\n\u200b<br \/>\nclass Audio : public Product { \/* \u97f3\u9891\u4e0b\u8f7d\u903b\u8f91 *\/ };<br \/>\nclass Video : public Product { \/* \u89c6\u9891\u4e0b\u8f7d\u903b\u8f91 *\/ }; <\/p>\n<h5 id=\"%E5%8F%8D%E9%9D%A2%E7%A4%BA%E4%BE%8B%EF%BC%9A%E4%B8%8D%E5%85%BC%E5%AE%B9%20LSP\" style=\"background-color:transparent\">\u53cd\u9762\u793a\u4f8b&#xff1a;\u4e0d\u517c\u5bb9 LSP<\/h5>\n<p>\/\/ \u274c \u8fdd\u53cd LSP<br \/>\nclass Chocolate : public Product { \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u2190 \u9519\u8bef\u4ece\u8fd9\u4e2a\u7ee7\u627f\u51b3\u5b9a\u5f00\u59cb<br \/>\npublic:<br \/>\n \u00a0 \u00a0\/\/ \u6536\u5230\u4ec0\u4e48\u8def\u5f84\u90fd\u65e0\u89c6 \u2014\u2014 \u524d\u7f6e\u6761\u4ef6\u88ab&#034;\u4f5c\u5e9f&#034;\u4e86<br \/>\n \u00a0 \u00a0void setFilePath(const std::string&amp;) override {<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0m_path &#061; &#034;Chocolate can not be downloaded.&#034;;<br \/>\n \u00a0  }<br \/>\n\u200b<br \/>\n \u00a0 \u00a0\/\/ \u57fa\u7c7b\u5951\u7ea6\u8bf4&#034;\u4e0d\u53ef\u4e0b\u8f7d\u5219\u629b\u5f02\u5e38&#034;&#xff0c;\u4f46\u5b83\u8fde&#034;\u53ef\u4e0b\u8f7d&#034;\u7684\u60c5\u51b5\u90fd\u4e0d\u5b58\u5728<br \/>\n \u00a0 \u00a0void download() override {<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0throw std::logic_error(&#034;Chocolate can not be downloaded.&#034;);<br \/>\n \u00a0  }<br \/>\n};<br \/>\n\u200b<br \/>\n\/\/ \u5ba2\u6237\u7aef\u4ee3\u7801<br \/>\nProduct* chocolate &#061; new Chocolate();<br \/>\nchocolate-&gt;setFilePath(&#034;Chocolate can not be downloaded.&#034;);<br \/>\ndownload(chocolate); \u00a0 \u00a0 \u00a0 \u00a0\/\/ &#x1f4a5; \u629b\u5f02\u5e38 \u2014\u2014 \u5ba2\u6237\u7aef\u63aa\u624b\u4e0d\u53ca <\/p>\n<p>\u4e3a\u4ec0\u4e48\u8fd9\u8fdd\u53cd\u4e86 LSP&#xff1f;<\/p>\n<p>\u8c03\u7528\u65b9 download(Product*) \u662f\u5728&#034;\u4ee5 T&#xff08;Product&#xff09;\u5b9a\u4e49\u7684\u7a0b\u5e8f P&#034;\u91cc\u5de5\u4f5c\u7684\u3002\u5b83\u8ba4\u4e3a Product \u90fd\u80fd\u4e0b\u8f7d\u3002\u628a Chocolate \u585e\u8fdb\u53bb&#xff0c;\u7a0b\u5e8f\u884c\u4e3a\u53d8\u4e86&#xff08;\u629b\u5f02\u5e38&#xff09;\u2014\u2014 \u8fd9\u6b63\u662f\u4e25\u683c\u5b9a\u4e49\u91cc\u8bf4\u7684&#034;behavior of P is unchanged&#034;\u88ab\u7834\u574f\u4e86\u3002<\/p>\n<p>&#x1f4ac; \u4eba\u8bdd\u7248 Chocolate \u8ba9 download() \u4ece&#034;\u80fd\u4e0b\u8f7d&#034;\u53d8\u6210\u4e86&#034;\u5fc5\u5b9a\u5931\u8d25&#034;\u3002\u80fd\u529b\u53d8\u5f31\u4e86&#xff0c;\u5c31\u662f\u8fdd\u7ea6\u3002<\/p>\n<h5 id=\"TIP%EF%BC%9A%E7%8E%B0%E5%9C%A8%E7%9A%84%E9%97%AE%E9%A2%98%E6%98%AF%E8%B0%81%E5%BA%94%E8%AF%A5%E5%AF%B9%E8%BF%9D%E5%8F%8D%20LSP%20%E8%B4%9F%E8%B4%A3%EF%BC%9F\">TIP&#xff1a;\u73b0\u5728\u7684\u95ee\u9898\u662f\u8c01\u5e94\u8be5\u5bf9\u8fdd\u53cd LSP \u8d1f\u8d23&#xff1f;<\/h5>\n<ul>\n<li>\n<p>\u662f download() \u51fd\u6570\u7684\u521b\u5efa\u8005\u5417&#xff1f;<\/p>\n<\/li>\n<li>\n<p>\u662f\u4ea7\u54c1\u7c7b\u7684\u521b\u9020\u8005\u5417&#xff1f;<\/p>\n<\/li>\n<li>\n<p>\u8fd8\u662f\u5de7\u514b\u529b\u7c7b\u7684\u521b\u9020\u8005&#xff1f;<\/p>\n<\/li>\n<\/ul>\n<p>\u5206\u6790&#xff1a;<\/p>\n<li>\n<p>\u6709\u4eba\u53ef\u80fd\u4f1a\u8bf4\u662f download() \u51fd\u6570\u7684\u521b\u5efa\u8005\u3002<\/p>\n<\/li>\n<li>\n<p>\u56e0\u4e3a Chocolate \u548c Product \u4e4b\u95f4\u5b58\u5728 IS-A \u5173\u7cfb\u3002<\/p>\n<\/li>\n<li>\n<p>download() \u51fd\u6570\u7684\u521b\u5efa\u8005\u6709\u6743\u505a\u51fa\u8fd9\u6837\u7684\u5047\u8bbe&#xff1a;download() \u51fd\u6570\u4ec5\u7528\u4e8e\u7ef4\u62a4\u53ef\u4e0b\u8f7d\u7684\u4ea7\u54c1\u3002<\/p>\n<\/li>\n<li>\n<p>\u53e6\u4e00\u65b9\u9762&#xff0c;\u603b\u7684\u6765\u8bf4&#xff0c;\u5de7\u514b\u529b\u7c7b\u7684\u521b\u9020\u8005\u6ca1\u6709\u8fdd\u53cd LSP \u2014\u2014 \u5de7\u514b\u529b\u786e\u5b9e\u4e0d\u80fd\u4e0b\u8f7d&#xff0c;\u8fd9\u4e2a\u4e8b\u5b9e\u6ca1\u9519\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f46\u662f&#xff0c;\u662f\u8c01\u4ece\u4ea7\u54c1\u7c7b\u6269\u5c55\u4e86\u5de7\u514b\u529b\u7c7b&#xff1f;<\/p>\n<\/li>\n<li>\n<p>\u8bf7\u6ce8\u610f&#xff0c;Product \u7c7b\u7684 download() \u65b9\u6cd5\u7684\u6587\u6863\u5757&#xff0c;\u5b83\u8868\u793a\u6b64\u65b9\u6cd5\u5c06\u4ec5\u7528\u4e8e\u4e0b\u8f7d\u53ef\u4e0b\u8f7d\u7684\u4ea7\u54c1&#xff0c;\u5982\u679c\u4ea7\u54c1\u4e0d\u53ef\u4e0b\u8f7d&#xff0c;\u5219\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u4f5c\u8005\u9700\u8981\u4e0d\u540c\u7684\u4e0b\u8f7d\u903b\u8f91&#xff0c;\u90a3\u4e48\u4ed6\/\u5979\u53ef\u4ee5\u5728\u5b50\u7c7b\u4e2d\u505a&#xff0c;\u6216\u5b9e\u73b0\u5168\u65b0\u7684\u903b\u8f91\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f46\u662f&#xff0c;\u5982\u679c\u4f5c\u8005\u5b9e\u73b0\u7684\u5185\u5bb9\u5c11\u4e8e\u6216\u591a\u4e8e\u57fa\u7c7b\u7684 doc \u5757\u6240\u8bf4\u7684\u5185\u5bb9&#xff0c;\u90a3\u4e48\u8fd9\u6837\u505a\u5c31\u8fdd\u53cd\u4e86 LSP\u3002<\/p>\n<\/li>\n<p>&#x1f4ac; \u4eba\u8bdd\u7248&#xff08;\u8d23\u4efb\u5f52\u5c5e&#xff09;<\/p>\n<ul>\n<li>\n<p>download(Product*) \u7684\u4f5c\u8005\u6ca1\u9519\u3002\u4ed6\u8bfb\u4e86 Product \u7684\u6587\u6863&#xff0c;\u6587\u6863\u767d\u7eb8\u9ed1\u5b57\u5199\u7740&#034;\u4ec5\u4e0b\u8f7d\u53ef\u4e0b\u8f7d\u7684\u4ea7\u54c1&#034;\u3002\u4ed6\u6709\u6743\u8fd9\u4e48\u5047\u8bbe\u3002<\/p>\n<\/li>\n<li>\n<p>Chocolate \u7684\u4f5c\u8005\u4e5f\u6ca1\u9519\u3002\u5de7\u514b\u529b\u4e0d\u80fd\u4e0b\u8f7d&#xff0c;\u8fd9\u662f\u5ba2\u89c2\u4e8b\u5b9e\u3002<\/p>\n<\/li>\n<li>\n<p>\u9519\u7684\u662f&#034;\u8ba9 Chocolate \u7ee7\u627f Product&#034;\u8fd9\u4e2a\u51b3\u5b9a\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u5de7\u514b\u529b\u4e0d\u662f\u4e00\u79cd&#034;&#xff08;\u53ef\u4e0b\u8f7d\u7684&#xff09;\u4ea7\u54c1&#034;\u3002\u8fd9\u662f\u4e00\u4e2a IS-A \u5173\u7cfb\u5224\u65ad\u5931\u8bef\u3002<\/p>\n<p>\u2705 \u6b63\u786e\u505a\u6cd5&#xff1a;\u628a\u804c\u8d23\u62c6\u5f00&#xff0c;\u8ba9\u7ee7\u627f\u5173\u7cfb\u56de\u5230\u771f\u5b9e\u7684\u8bed\u4e49\u4e0a\u3002<\/p>\n<p> class Downloadable { public: virtual void download() &#061; 0; }; \u00a0 \/\/ \u53ef\u4e0b\u8f7d\u7684<br \/>\nclass Deliverable  { public: virtual void deliver() \u00a0&#061; 0; }; \u00a0 \/\/ \u53ef\u914d\u9001\u7684<br \/>\n\u200b<br \/>\nclass Audio  : public Downloadable { \/* &#8230; *\/ };<br \/>\nclass Video  : public Downloadable { \/* &#8230; *\/ };<br \/>\nclass Chocolate : public Deliverable { \/* \u5de7\u514b\u529b\u8d70\u914d\u9001&#xff0c;\u4e0d\u8d70\u4e0b\u8f7d *\/ }; <\/p>\n<p>&#x1f511; \u4e00\u53e5\u8bdd \u6587\u6863\u5757&#xff08;\u5951\u7ea6&#xff09;\u5c31\u662f\u5408\u540c\u3002\u5b50\u7c7b\u505a\u7684\u6bd4\u5408\u540c\u5c11\u6216\u591a&#xff0c;\u90fd\u7b97\u8fdd\u7ea6\u3002<\/p>\n<h5 id=\"TIP%EF%BC%9A%E5%B8%B8%E8%A7%81%E7%9A%84%20LSP%20%E8%BF%9D%E8%A7%84\">TIP&#xff1a;\u5e38\u89c1\u7684 LSP \u8fdd\u89c4<\/h5>\n<p>\u2460 \u5b50\u7c7b\u4e2d\u7684\u9000\u5316\u65b9\u6cd5<\/p>\n<p>\u5982\u679c\u57fa\u7c7b\u6709\u4e00\u4e2a\u65b9\u6cd5&#xff0c;\u4f46\u57fa\u7c7b\u7684\u5b50\u7c7b\u4e0d\u9700\u8981\u8be5\u65b9\u6cd5&#xff0c;\u90a3\u4e48\u5982\u679c\u5b50\u7c7b\u7684\u4f5c\u8005\u518d\u6b21\u9000\u5316\u8be5\u65b9\u6cd5&#xff0c;\u8fd9\u5c06\u662f\u53ef\u66ff\u4ee3\u7684\u8fdd\u89c4\u3002<\/p>\n<p>class Bird {<br \/>\npublic:<br \/>\n \u00a0 \u00a0virtual void fly() { \/* \u62cd\u7fc5\u8180\u98de *\/ }<br \/>\n};<br \/>\n\u200b<br \/>\nclass Ostrich : public Bird { \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u274c \u9e35\u9e1f\u662f\u9e1f&#xff0c;\u4f46\u4e0d\u4f1a\u98de<br \/>\npublic:<br \/>\n \u00a0 \u00a0void fly() override {<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0throw std::logic_error(&#034;ostrich can not fly&#034;); \u00a0 \/\/ \u9000\u5316\u5b9e\u73b0<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u6216\u8005\u5199\u6210\u7a7a\u51fd\u6570\u4f53 \u2014\u2014 \u540c\u6837\u8fdd\u89c4&#xff0c;\u800c\u4e14\u66f4\u9690\u853d<br \/>\n \u00a0  }<br \/>\n}; <\/p>\n<p>\u2461 \u4ece\u5b50\u7c7b\u629b\u51fa\u5f02\u5e38<\/p>\n<p>LSP \u8fdd\u89c4\u7684\u53e6\u4e00\u79cd\u5f62\u5f0f\u662f\u5411\u5b50\u7c7b\u6dfb\u52a0\u5f02\u5e38&#xff0c;\u800c\u57fa\u7c7b\u4e0d\u5e0c\u671b\u8fd9\u6837\u3002\u56e0\u4e3a\u90a3\u65f6\u57fa\u7c7b\u4e0d\u80fd\u88ab\u5b50\u7c7b\u66ff\u4ee3\u3002<\/p>\n<p>class FileLoader {<br \/>\npublic:<br \/>\n \u00a0 \u00a0virtual std::string read(const std::string&amp; path); \u00a0 \/\/ \u5951\u7ea6&#xff1a;\u8bfb\u4e0d\u5230\u8fd4\u56de\u7a7a\u4e32&#xff0c;\u4e0d\u629b\u5f02\u5e38<br \/>\n};<br \/>\n\u200b<br \/>\nclass NetworkFileLoader : public FileLoader { \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ \u274c \u51ed\u7a7a\u591a\u51fa\u5f02\u5e38<br \/>\npublic:<br \/>\n \u00a0 \u00a0std::string read(const std::string&amp; path) override {<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0throw std::runtime_error(&#034;network unreachable&#034;); \/\/ \u57fa\u7c7b\u5951\u7ea6\u91cc\u6ca1\u8fd9\u6761<br \/>\n \u00a0  }<br \/>\n}; <\/p>\n<p>C&#043;&#043; \u89c6\u89d2\u7684\u8865\u5145&#xff1a;<\/p>\n<table>\n<tr>\u8fdd\u89c4\u5f62\u5f0f\u8bf4\u660e\u4eba\u8bdd<\/tr>\n<tbody>\n<tr>\n<td>\u57fa\u7c7b\u6790\u6784\u51fd\u6570\u975e virtual<\/td>\n<td>delete basePtr \u53ea\u6790\u6784\u57fa\u7c7b\u90e8\u5206&#xff0c;\u6d3e\u751f\u7c7b\u8d44\u6e90\u6cc4\u6f0f<\/td>\n<td>\u6700\u9690\u853d\u7684\u4e00\u79cd LSP \u8fdd\u89c4<\/td>\n<\/tr>\n<tr>\n<td>\u5b50\u7c7b\u628a public \u65b9\u6cd5\u6539 private<\/td>\n<td>C&#043;&#043; \u5141\u8bb8\u6536\u7d27\u8bbf\u95ee\u6743\u9650&#xff08;Java \u4e0d\u5141\u8bb8\u7f16\u8bd1&#xff09;<\/td>\n<td>\u7236\u7c7b\u8bf4&#034;\u4f60\u80fd\u53eb\u8fd9\u4e2a\u670d\u52a1&#034;&#xff0c;\u5b50\u7c7b\u8bf4&#034;\u4e0d\u7ed9\u4f60\u53eb&#034;<\/td>\n<\/tr>\n<tr>\n<td>\u524d\u7f6e\u6761\u4ef6\u6536\u7d27<\/td>\n<td>\u7236\u7c7b\u63a5\u53d7\u4efb\u610f\u8def\u5f84&#xff0c;\u5b50\u7c7b\u8981\u6c42\u5fc5\u987b\u662f .mp3<\/td>\n<td>\u8981\u6c42\u53d8\u591a\u4e86<\/td>\n<\/tr>\n<tr>\n<td>\u540e\u7f6e\u6761\u4ef6\u653e\u677e<\/td>\n<td>\u7236\u7c7b\u4fdd\u8bc1\u8fd4\u56de\u975e\u7a7a&#xff0c;\u5b50\u7c7b\u53ef\u80fd\u8fd4\u56de\u7a7a<\/td>\n<td>\u4fdd\u8bc1\u53d8\u5c11\u4e86<\/td>\n<\/tr>\n<tr>\n<td>\u6539\u53d8\u7236\u7c7b\u7684\u8bed\u4e49<\/td>\n<td>Circle \u7ee7\u627f Ellipse&#xff0c;\u4f46 setWidth \u4f1a\u540c\u65f6\u6539\u9ad8<\/td>\n<td>\u957f\u5f97\u50cf \u2260 IS-A<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&#x1f4ac; \u5706-\u692d\u5706\u95ee\u9898 \u6570\u5b66\u4e0a&#034;\u5706\u662f\u692d\u5706\u7684\u4e00\u79cd&#034;&#xff0c;\u4f46\u4ee3\u7801\u91cc Ellipse \u6709 setWidth \/ setHeight \u4e24\u4e2a\u72ec\u7acb\u65b9\u6cd5&#xff0c;\u800c Circle \u5fc5\u987b\u8ba9\u4e24\u8005\u76f8\u7b49 \u2014\u2014 \u8c03\u7528\u65b9\u62ff\u5230 Ellipse* \u540e\u8c03 setWidth(5) \u518d\u8c03 setHeight(3)&#xff0c;\u671f\u671b\u5f97\u5230\u4e00\u4e2a 5\u00d73 \u7684\u692d\u5706&#xff0c;\u7ed3\u679c Circle \u7ed9\u4e0d\u4e86\u3002 \u8fd9\u5c31\u662f\u6700\u7ecf\u5178\u7684 LSP \u6096\u8bba&#xff1a;\u6570\u5b66\u4e0a\u7684 IS-A&#xff0c;\u4e0d\u7b49\u4e8e\u4ee3\u7801\u5951\u7ea6\u4e0a\u7684 IS-A\u3002<\/p>\n<hr \/>\n<h4 id=\"1.3.4%20%E4%BE%9D%E8%B5%96%E5%80%92%E7%BD%AE%E5%8E%9F%E5%88%99%20DIP\" style=\"background-color:transparent\">1.3.4 \u4f9d\u8d56\u5012\u7f6e\u539f\u5219 DIP<\/h4>\n<p>\u5982\u679c\u8bf4\u5f00\u95ed\u539f\u5219\u662f\u9762\u5411\u5bf9\u8c61\u8bbe\u8ba1\u7684&#034;\u76ee\u6807&#034;\u7684\u8bdd&#xff0c;\u90a3\u4e48\u4f9d\u8d56\u5012\u7f6e\u539f\u5219&#xff08;Dependence Inversion Principle, DIP&#xff09;\u5c31\u662f\u9762\u5411\u5bf9\u8c61\u8bbe\u8ba1\u7684&#034;\u4e3b\u8981\u5b9e\u73b0\u673a\u5236&#034;\u4e4b\u4e00&#xff0c;\u5b83\u662f\u7cfb\u7edf\u62bd\u8c61\u5316\u7684\u5177\u4f53\u5b9e\u73b0\u3002<\/p>\n<p>\u4f9d\u8d56\u5012\u7f6e\u539f\u5219\u662f Robert C. Martin&#xff08;Bob \u5927\u53d4&#xff09;\u5728 1996 \u5e74\u4e3a &#034;C&#043;&#043; Reporter&#034; \u6240\u5199\u7684\u4e13\u680f Engineering Notebook \u7684\u7b2c\u4e09\u7bc7&#xff0c;\u540e\u6765\u52a0\u5165\u5230\u4ed6\u5728 2002 \u5e74\u51fa\u7248\u7684\u7ecf\u5178\u8457\u4f5c Agile Software Development, Principles, Patterns, and Practices \u4e00\u4e66\u4e2d\u3002<\/p>\n<p>\u5b9a\u4e49\u5982\u4e0b&#xff1a;<\/p>\n<p>High-level modules should not import anything from low-level modules. Both should depend on abstractions. Abstractions should not depend on details. Details should depend on abstractions.<\/p>\n<p>\u9ad8\u5c42\u6a21\u5757\u4e0d\u5e94\u8be5\u4ece\u4f4e\u5c42\u6a21\u5757\u5bfc\u5165\u4efb\u4f55\u4e1c\u897f&#xff0c;\u4e24\u8005\u90fd\u5e94\u8be5\u4f9d\u8d56\u4e8e\u62bd\u8c61\u3002\u62bd\u8c61\u4e0d\u5e94\u8be5\u4f9d\u8d56\u4e8e\u7ec6\u8282&#xff0c;\u7ec6\u8282\u5e94\u5f53\u4f9d\u8d56\u4e8e\u62bd\u8c61\u3002<\/p>\n<p>\u53ef\u80fd\u6709\u70b9\u4e0d\u662f\u4ee4\u4eba\u7406\u89e3&#xff0c;\u53e6\u4e00\u53e5\u5e7f\u4e3a\u6d41\u4f20\u7684\u8868\u8ff0&#xff1a;<\/p>\n<p>Program to an interface, not an implementation.<\/p>\n<p>\u5373&#xff1a;\u9488\u5bf9\u63a5\u53e3\u7f16\u7a0b&#xff0c;\u800c\u4e0d\u662f\u9488\u5bf9\u5b9e\u73b0\u7f16\u7a0b\u3002<\/p>\n<p>&#x1f4ac; \u4eba\u8bdd\u7248 \u5148\u89e3\u91ca&#034;\u5012\u7f6e&#034;\u5012\u7684\u662f\u4ec0\u4e48\u3002<\/p>\n<p>\u4f20\u7edf\u5199\u6cd5&#xff08;\u4e0d\u5012\u7f6e&#xff09;&#xff1a; \u4e1a\u52a1\u5c42\u8bf4&#034;\u6211\u9700\u8981\u4e00\u4e2a XML \u8f6c\u6362\u5668&#034;&#xff0c;\u4e8e\u662f #include &#034;XMLConvert.h&#034;&#xff0c;\u76f4\u63a5 new XMLConvert()\u3002 \u4f9d\u8d56\u7bad\u5934\u662f&#xff1a;\u4e1a\u52a1\u5c42 \u2500\u2500\u2192 \u5177\u4f53\u5b9e\u73b0\u3002\u4e1a\u52a1\u5c42\u88ab\u5177\u4f53\u5b9e\u73b0&#034;\u9501\u6b7b&#034;\u4e86\u3002<\/p>\n<p>\u5012\u7f6e\u5199\u6cd5&#xff1a; \u4e1a\u52a1\u5c42\u8bf4&#034;\u6211\u9700\u8981\u4e00\u4e2a\u8f6c\u6362\u5668&#034;&#xff0c;\u4e8e\u662f\u4f9d\u8d56 DataConvert \u62bd\u8c61\u3002 \u4f9d\u8d56\u7bad\u5934\u53d8\u6210&#xff1a;\u4e1a\u52a1\u5c42 \u2500\u2500&gt;\u00a0\u00a0DataConvert\u00a0 &lt;\u2500\u2500 XMLConvert\u3002 \u5177\u4f53\u5b9e\u73b0\u53cd\u8fc7\u6765\u4f9d\u8d56\u62bd\u8c61 \u2014\u2014 \u7bad\u5934\u88ab&#034;\u5012&#034;\u8fc7\u6765\u4e86\u3002<\/p>\n<p>\u597d\u5904&#xff1a; \u4e1a\u52a1\u5c42\u7684\u4ee3\u7801\u91cc&#xff0c;\u6c38\u8fdc\u4e0d\u51fa\u73b0\u4efb\u4f55\u4e00\u4e2a\u5177\u4f53\u7c7b\u7684\u540d\u5b57\u3002\u6362\u5b9e\u73b0\u3001\u52a0\u5b9e\u73b0&#xff0c;\u4e1a\u52a1\u5c42\u4e00\u4e2a\u5b57\u90fd\u4e0d\u7528\u6539&#xff0c;\u751a\u81f3\u4e0d\u7528\u91cd\u65b0\u7f16\u8bd1\u3002<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u4e5f\u8bb8\u53ef\u80fd\u8fd8\u662f\u6709\u70b9\u62bd\u8c61&#xff0c;\u6ca1\u4e8b\u6211\u4eec\u5148\u8bb0\u4f4f\u6982\u5ff5&#xff0c;\u7b49\u4f1a\u770b\u4ee3\u7801\u548c\u7c7b\u56fe\u3002<\/p>\n<h5 id=\"%E8%90%BD%E5%9C%B0%E8%A6%81%E6%B1%82\">\u843d\u5730\u8981\u6c42<\/h5>\n<p>\u4f9d\u8d56\u5012\u7f6e\u539f\u5219\u8981\u6c42\u6211\u4eec\u5728\u7a0b\u5e8f\u4ee3\u7801\u4e2d\u4f20\u9012\u53c2\u6570\u65f6\u6216\u5728\u5173\u8054\u5173\u7cfb\u4e2d&#xff0c;\u5c3d\u91cf\u5f15\u7528\u5c42\u6b21\u9ad8\u7684\u62bd\u8c61\u5c42\u7c7b&#xff0c;\u5373&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u4f7f\u7528\u63a5\u53e3\u548c\u62bd\u8c61\u7c7b\u8fdb\u884c\u53d8\u91cf\u7c7b\u578b\u58f0\u660e\u3001\u53c2\u6570\u7c7b\u578b\u58f0\u660e\u3001\u65b9\u6cd5\u8fd4\u56de\u7c7b\u578b\u58f0\u660e&#xff0c;\u4ee5\u53ca\u6570\u636e\u7c7b\u578b\u7684\u8f6c\u6362\u7b49&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u800c\u4e0d\u8981\u7528\u5177\u4f53\u7c7b\u6765\u505a\u8fd9\u4e9b\u4e8b\u60c5\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u4e3a\u4e86\u786e\u4fdd\u8be5\u539f\u5219\u7684\u5e94\u7528&#xff0c;\u4e00\u4e2a\u5177\u4f53\u7c7b\u5e94\u5f53\u53ea\u5b9e\u73b0\u63a5\u53e3\u6216\u62bd\u8c61\u7c7b\u4e2d\u58f0\u660e\u8fc7\u7684\u65b9\u6cd5&#xff0c;\u800c\u4e0d\u8981\u7ed9\u51fa\u591a\u4f59\u7684\u65b9\u6cd5&#xff0c;\u5426\u5219\u5c06\u65e0\u6cd5\u8c03\u7528\u5230\u5728\u5b50\u7c7b\u4e2d\u589e\u52a0\u7684\u65b0\u65b9\u6cd5\u3002<\/p>\n<li>\n<p>\u5728\u5f15\u5165\u62bd\u8c61\u5c42\u540e&#xff0c;\u7cfb\u7edf\u5c06\u5177\u6709\u5f88\u597d\u7684\u7075\u6d3b\u6027\u3002\u5728\u7a0b\u5e8f\u4e2d\u5c3d\u91cf\u4f7f\u7528\u62bd\u8c61\u5c42\u8fdb\u884c\u7f16\u7a0b&#xff0c;\u800c\u5c06\u5177\u4f53\u7c7b\u5199\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u3002\u8fd9\u6837\u4e00\u6765&#xff0c;\u5982\u679c\u7cfb\u7edf\u884c\u4e3a\u53d1\u751f\u53d8\u5316&#xff0c;\u53ea\u9700\u8981\u5bf9\u62bd\u8c61\u5c42\u8fdb\u884c\u6269\u5c55&#xff0c;\u5e76\u4fee\u6539\u914d\u7f6e\u6587\u4ef6&#xff0c;\u800c\u65e0\u987b\u4fee\u6539\u539f\u6709\u7cfb\u7edf\u7684\u6e90\u4ee3\u7801&#xff0c;\u5728\u4e0d\u4fee\u6539\u7684\u60c5\u51b5\u4e0b\u6765\u6269\u5c55\u7cfb\u7edf\u7684\u529f\u80fd&#xff0c;\u6ee1\u8db3\u5f00\u95ed\u539f\u5219\u7684\u8981\u6c42\u3002<\/p>\n<\/li>\n<h5 id=\"%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%EF%BC%88Dependency%20Injection%2C%20DI%EF%BC%89\">\u4f9d\u8d56\u6ce8\u5165&#xff08;Dependency Injection, DI&#xff09;<\/h5>\n<p>\u5728\u5b9e\u73b0\u4f9d\u8d56\u5012\u7f6e\u539f\u5219\u65f6&#xff0c;\u6211\u4eec\u9700\u8981\u9488\u5bf9\u62bd\u8c61\u5c42\u7f16\u7a0b&#xff0c;\u800c\u5c06\u5177\u4f53\u7c7b\u7684\u5bf9\u8c61\u901a\u8fc7\u4f9d\u8d56\u6ce8\u5165\u7684\u65b9\u5f0f\u6ce8\u5165\u5230\u5176\u4ed6\u5bf9\u8c61\u4e2d\u3002<\/p>\n<p>\u4f9d\u8d56\u6ce8\u5165\u662f\u6307&#xff1a;\u5f53\u4e00\u4e2a\u5bf9\u8c61\u8981\u4e0e\u5176\u4ed6\u5bf9\u8c61\u53d1\u751f\u4f9d\u8d56\u5173\u7cfb\u65f6&#xff0c;\u901a\u8fc7\u62bd\u8c61\u6765\u6ce8\u5165\u6240\u4f9d\u8d56\u7684\u5bf9\u8c61\u3002<\/p>\n<p>\u5e38\u7528\u7684\u6ce8\u5165\u65b9\u5f0f\u6709\u4e09\u79cd&#xff1a;<\/p>\n<table>\n<tr>\u6ce8\u5165\u65b9\u5f0f\u505a\u6cd5\u4f18\u70b9\u7f3a\u70b9<\/tr>\n<tbody>\n<tr>\n<td>\u6784\u9020\u6ce8\u5165<\/td>\n<td>\u901a\u8fc7\u6784\u9020\u51fd\u6570\u4f20\u5165\u5177\u4f53\u7c7b\u5bf9\u8c61<\/td>\n<td>\u5bf9\u8c61\u4e00\u65e6\u9020\u597d\u5c31\u5904\u4e8e&#034;\u5b8c\u6574\u53ef\u7528&#034;\u72b6\u6001&#xff0c;\u4e0d\u53ef\u53d8&#xff0c;\u7ebf\u7a0b\u5b89\u5168\u53cb\u597d<\/td>\n<td>\u4f9d\u8d56\u591a\u65f6\u6784\u9020\u51fd\u6570\u53c2\u6570\u4f1a\u5f88\u957f<\/td>\n<\/tr>\n<tr>\n<td>\u8bbe\u503c\u6ce8\u5165&#xff08;Setter \u6ce8\u5165&#xff09;<\/td>\n<td>\u901a\u8fc7 Setter \u65b9\u6cd5\u4f20\u5165\u5177\u4f53\u7c7b\u5bf9\u8c61<\/td>\n<td>\u7075\u6d3b&#xff0c;\u53ef\u968f\u65f6\u66ff\u6362<\/td>\n<td>\u5bf9\u8c61\u53ef\u80fd\u5904\u4e8e&#034;\u534a\u6210\u54c1&#034;\u72b6\u6001&#xff0c;\u7528\u4e4b\u524d\u5fc5\u987b\u5148\u8bbe\u597d<\/td>\n<\/tr>\n<tr>\n<td>\u63a5\u53e3\u6ce8\u5165<\/td>\n<td>\u901a\u8fc7\u5728\u63a5\u53e3\u4e2d\u58f0\u660e\u7684\u4e1a\u52a1\u65b9\u6cd5\u6765\u4f20\u5165\u5177\u4f53\u7c7b\u5bf9\u8c61<\/td>\n<td>\u8bed\u4e49\u660e\u786e<\/td>\n<td>\u9700\u8981\u989d\u5916\u5b9a\u4e49\u6ce8\u5165\u63a5\u53e3&#xff0c;\u4fb5\u5165\u6027\u8f83\u5f3a<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u4e9b\u65b9\u6cd5\u5728\u5b9a\u4e49\u65f6\u4f7f\u7528\u7684\u662f\u62bd\u8c61\u7c7b\u578b&#xff0c;\u5728\u8fd0\u884c\u65f6\u518d\u4f20\u5165\u5177\u4f53\u7c7b\u578b\u7684\u5bf9\u8c61&#xff0c;\u7531\u5b50\u7c7b\u5bf9\u8c61\u6765\u8986\u76d6\u7236\u7c7b\u5bf9\u8c61\u3002<\/p>\n<p>&#x1f4ac; \u4eba\u8bdd\u7248 &#034;\u4f9d\u8d56\u5012\u7f6e&#034;\u662f\u8bbe\u8ba1\u601d\u60f3&#xff0c;&#034;\u4f9d\u8d56\u6ce8\u5165&#034;\u662f\u628a\u5b83\u843d\u5730\u7684\u5177\u4f53\u624b\u6cd5\u3002 \u601d\u8def\u5c31\u662f&#xff1a;\u6211\u4e0d\u4e3b\u52a8\u53bb new \u6211\u8981\u7528\u7684\u4e1c\u897f&#xff0c;\u800c\u662f\u8ba9\u522b\u4eba\u4ece\u5916\u9762&#034;\u9001&#034;\u8fdb\u6765\u3002<\/p>\n<p>\u751f\u6d3b\u7c7b\u6bd4&#xff1a;\u4f60\u8981\u559d\u5496\u5561\u3002<\/p>\n<ul>\n<li>\n<p>\u4e0d\u5012\u7f6e&#xff1a;\u4f60\u81ea\u5df1\u5728\u529e\u516c\u5ba4\u91cc\u88c5\u4e86\u53f0\u70d8\u7119\u673a\u3001\u78e8\u8c46\u673a\u3001\u610f\u5f0f\u673a&#xff08;\u4e1a\u52a1\u5c42\u76f4\u63a5\u4f9d\u8d56\u5168\u90e8\u7ec6\u8282&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u5012\u7f6e &#043; \u6ce8\u5165&#xff1a;\u4f60\u53ea\u8bf4&#034;\u6211\u8981\u4e00\u676f\u5496\u5561&#034;&#xff08;\u4f9d\u8d56\u62bd\u8c61 Coffee&#xff09;&#xff0c;\u697c\u4e0b\u5496\u5561\u5e97\u628a\u6210\u54c1\u9001\u4e0a\u6765&#xff08;\u5916\u90e8\u6ce8\u5165\u5177\u4f53\u5b9e\u73b0&#xff09;\u3002\u660e\u5929\u6362\u6210\u5976\u8336\u5e97&#xff0c;\u4f60\u6539\u7684\u53ea\u662f&#034;\u4e0b\u5355\u5bf9\u8c61&#034;&#xff0c;\u4f60\u7684\u529e\u516c\u5ba4\u5e03\u5c40\u4e00\u70b9\u6ca1\u53d8\u3002<\/p>\n<\/li>\n<\/ul>\n<h5 id=\"%E5%8F%8D%E4%BE%8B%EF%BC%9ADataBiz%20%E9%92%88%E5%AF%B9%E5%85%B7%E4%BD%93%E5%AE%9E%E7%8E%B0%E7%BC%96%E7%A8%8B\">\u53cd\u4f8b&#xff1a;DataBiz \u9488\u5bf9\u5177\u4f53\u5b9e\u73b0\u7f16\u7a0b<\/h5>\n<p style=\"text-align:center\"><span tabindex=\"-1\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260919124912-6aae84c87caef.png\" \/><span title=\"\u70b9\u51fb\u5e76\u62d6\u62fd\u4ee5\u6539\u53d8\u5c3a\u5bf8\">\u200b<\/span><\/span><\/p>\n<p> <span class=\"hljs-comment\">\/\/ \u274c \u8fdd\u53cd DIP&#xff1a;\u9ad8\u5c42\u4e1a\u52a1\u76f4\u63a5\u4f9d\u8d56\u4f4e\u5c42\u5b9e\u73b0<\/span><br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">DataBiz<\/span> {<br \/>\n<span class=\"hljs-keyword\">public<\/span>:<br \/>\n \u00a0 \u00a0<span class=\"hljs-function\"><span class=\"hljs-type\">void<\/span> <span class=\"hljs-title\">doWork<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">const<\/span> std::string&amp; data)<\/span> <\/span>{<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0XMLConvert convert; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<span class=\"hljs-comment\">\/\/ \u2190 \u786c\u7f16\u7801\u5177\u4f53\u5b9e\u73b0<\/span><br \/>\n \u00a0 \u00a0 \u00a0 \u00a0<span class=\"hljs-comment\">\/\/ JSONConvert convert; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0  \/\/ \u6539\u6210 JSON \u5c31\u5f97\u6539\u6e90\u7801\u3001\u91cd\u65b0\u7f16\u8bd1<\/span><br \/>\n \u00a0 \u00a0 \u00a0 \u00a0std::string result &#061; convert.<span class=\"hljs-built_in\">convert<\/span>(data);<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0<span class=\"hljs-comment\">\/\/ &#8230; \u4e1a\u52a1\u5904\u7406<\/span><br \/>\n \u00a0  }<br \/>\n}; <\/p>\n<p>\u4e0a\u9762\u793a\u4f8b\u4e2d DataBiz \u662f\u5178\u578b\u7684\u9488\u5bf9\u5177\u4f53\u5b9e\u73b0\u8fdb\u884c\u7f16\u7a0b&#xff0c;\u56e0\u6b64\u5728\u6570\u636e\u683c\u5f0f\u53d8\u66f4\u7684\u65f6\u5019\u5c31\u9700\u8981\u53cd\u590d\u5730\u4fee\u6539\u4ee3\u7801\u3002<\/p>\n<h5 id=\"%E9%87%8D%E6%9E%84%EF%BC%9A%E6%8A%BD%E8%B1%A1%20%2B%20%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%20%2B%20%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6\" style=\"background-color:transparent\">\u91cd\u6784&#xff1a;\u62bd\u8c61 &#043; \u4f9d\u8d56\u6ce8\u5165 &#043; \u914d\u7f6e\u6587\u4ef6<\/h5>\n<p style=\"text-align:center\"><span tabindex=\"-1\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260919124912-6aae84c8a175b.png\" \/><span title=\"\u70b9\u51fb\u5e76\u62d6\u62fd\u4ee5\u6539\u53d8\u5c3a\u5bf8\">\u200b<\/span><\/span><\/p>\n<p> <span class=\"hljs-comment\">\/\/ \u2500\u2500 \u62bd\u8c61\u5c42&#xff1a;\u9ad8\u5c42\u4e0e\u4f4e\u5c42\u5171\u540c\u4f9d\u8d56\u7684\u4e1c\u897f \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500<\/span><br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">DataConvert<\/span> {<br \/>\n<span class=\"hljs-keyword\">public<\/span>:<br \/>\n \u00a0 \u00a0<span class=\"hljs-keyword\">virtual<\/span> ~<span class=\"hljs-built_in\">DataConvert<\/span>() &#061; <span class=\"hljs-keyword\">default<\/span>;<br \/>\n \u00a0 \u00a0<span class=\"hljs-function\"><span class=\"hljs-keyword\">virtual<\/span> std::string <span class=\"hljs-title\">convert<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">const<\/span> std::string&amp; data)<\/span> <\/span>&#061; <span class=\"hljs-number\">0<\/span>;<br \/>\n};<br \/>\n\u200b<br \/>\n<span class=\"hljs-comment\">\/\/ \u2500\u2500 \u4f4e\u5c42\u5b9e\u73b0&#xff1a;\u4f9d\u8d56\u62bd\u8c61 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500<\/span><br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">XMLConvert<\/span> : <span class=\"hljs-keyword\">public<\/span> DataConvert {<br \/>\n<span class=\"hljs-keyword\">public<\/span>:<br \/>\n \u00a0 \u00a0<span class=\"hljs-function\">std::string <span class=\"hljs-title\">convert<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">const<\/span> std::string&amp; data)<\/span> <span class=\"hljs-keyword\">override<\/span> <\/span>{ <span class=\"hljs-comment\">\/* XML \u8f6c\u6362 *\/<\/span> <span class=\"hljs-keyword\">return<\/span> {}; }<br \/>\n};<br \/>\n\u200b<br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">JSONConvert<\/span> : <span class=\"hljs-keyword\">public<\/span> DataConvert {<br \/>\n<span class=\"hljs-keyword\">public<\/span>:<br \/>\n \u00a0 \u00a0<span class=\"hljs-function\">std::string <span class=\"hljs-title\">convert<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">const<\/span> std::string&amp; data)<\/span> <span class=\"hljs-keyword\">override<\/span> <\/span>{ <span class=\"hljs-comment\">\/* JSON \u8f6c\u6362 *\/<\/span> <span class=\"hljs-keyword\">return<\/span> {}; }<br \/>\n};<br \/>\n\u200b<br \/>\n<span class=\"hljs-comment\">\/\/ \u2500\u2500 \u9ad8\u5c42\u4e1a\u52a1&#xff1a;\u53ea\u8ba4\u8bc6\u62bd\u8c61 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500<\/span><br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">DataBiz<\/span> {<br \/>\n \u00a0 \u00a0DataConvert* m_convert;<br \/>\n<span class=\"hljs-keyword\">public<\/span>:<br \/>\n \u00a0 \u00a0<span class=\"hljs-function\"><span class=\"hljs-keyword\">explicit<\/span> <span class=\"hljs-title\">DataBiz<\/span><span class=\"hljs-params\">(DataConvert* convert)<\/span> : m_convert(convert) {<\/span>} \u00a0 <span class=\"hljs-comment\">\/\/ \u6784\u9020\u6ce8\u5165<\/span><br \/>\n\u200b<br \/>\n \u00a0 \u00a0<span class=\"hljs-function\"><span class=\"hljs-type\">void<\/span> <span class=\"hljs-title\">setConvert<\/span><span class=\"hljs-params\">(DataConvert* convert)<\/span> <\/span>{ m_convert &#061; convert; } \u00a0 <span class=\"hljs-comment\">\/\/ \u8bbe\u503c\u6ce8\u5165<\/span><br \/>\n\u200b<br \/>\n \u00a0 \u00a0<span class=\"hljs-function\"><span class=\"hljs-type\">void<\/span> <span class=\"hljs-title\">doWork<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">const<\/span> std::string&amp; data)<\/span> <\/span>{<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0std::string result &#061; m_convert-&gt;<span class=\"hljs-built_in\">convert<\/span>(data); \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\">\/\/ \u53ea\u8c03\u62bd\u8c61\u65b9\u6cd5<\/span><br \/>\n \u00a0 \u00a0 \u00a0 \u00a0<span class=\"hljs-comment\">\/\/ &#8230; \u4e1a\u52a1\u5904\u7406<\/span><br \/>\n \u00a0  }<br \/>\n}; <\/p>\n<p>\u914d\u7f6e\u9a71\u52a8&#xff1a;<\/p>\n<p> <span class=\"hljs-meta\"># conf.properties<\/span><br \/>\nconvert.<span class=\"hljs-keyword\">class<\/span> &#061; JSONConvert<br \/>\n<span class=\"hljs-comment\">\/\/ \u8fd0\u884c\u65f6 &#043; \u914d\u7f6e\u6587\u4ef6\u51b3\u5b9a\u7528\u54ea\u4e2a\u5177\u4f53\u5b9e\u73b0&#xff08;\u914d\u5408\u5de5\u5382\/\u53cd\u5c04&#xff09;<\/span><br \/>\nDataConvert* convert &#061; ClassHelper::<span class=\"hljs-built_in\">createFromConfig<\/span>(<span class=\"hljs-string\">&#034;convert.class&#034;<\/span>);<br \/>\n<span class=\"hljs-function\">DataBiz <span class=\"hljs-title\">biz<\/span><span class=\"hljs-params\">(convert)<\/span><\/span>;<br \/>\nbiz.<span class=\"hljs-built_in\">doWork<\/span>(payload); <\/p>\n<p>\u57fa\u4e8e\u4f9d\u8d56\u5012\u7f6e\u539f\u5219&#xff0c;\u65b0\u589e\u4e00\u4e2a\u62bd\u8c61\u7684\u8f6c\u6362\u5668 DataConvert&#xff0c;DataBiz \u9488\u5bf9 DataConvert \u8fdb\u884c\u7f16\u7a0b&#xff1b;\u7136\u540e\u6839\u636e\u91cc\u6c0f\u66ff\u6362\u539f\u5219&#xff0c;\u7a0b\u5e8f\u8fd0\u884c\u65f6\u5bf9\u7236\u7c7b\u8fdb\u884c\u66ff\u6362&#xff1b;\u6839\u636e\u5f00\u95ed\u539f\u5219&#xff0c;\u5c06\u8fd0\u884c\u5bf9\u8c61\u6307\u5b9a\u8bbe\u7f6e\u5230\u914d\u7f6e\u6587\u4ef6\u4e2d\u3002<\/p>\n<p>&#x1f511; \u4e09\u5927\u539f\u5219\u7684\u534f\u4f5c\u5173\u7cfb<\/p>\n<p>\u5728\u4e0a\u8ff0\u91cd\u6784\u8fc7\u7a0b\u4e2d&#xff0c;\u6211\u4eec\u4f7f\u7528\u4e86\u5f00\u95ed\u539f\u5219\u3001\u91cc\u6c0f\u4ee3\u6362\u539f\u5219\u548c\u4f9d\u8d56\u5012\u8f6c\u539f\u5219\u3002\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b&#xff0c;\u8fd9\u4e09\u4e2a\u8bbe\u8ba1\u539f\u5219\u4f1a\u540c\u65f6\u51fa\u73b0&#xff1a;<\/p>\n<table>\n<tr>\u539f\u5219\u89d2\u8272<\/tr>\n<tbody>\n<tr>\n<td>\u5f00\u95ed\u539f\u5219<\/td>\n<td>\u76ee\u6807 \u2014\u2014 \u6211\u4eec\u8981&#034;\u4e0d\u4fee\u6539\u5c31\u80fd\u6269\u5c55&#034;<\/td>\n<\/tr>\n<tr>\n<td>\u91cc\u6c0f\u4ee3\u6362\u539f\u5219<\/td>\n<td>\u57fa\u7840 \u2014\u2014 \u4fdd\u8bc1\u5b50\u7c7b\u80fd\u5b89\u5168\u66ff\u6362\u7236\u7c7b&#xff0c;\u591a\u6001\u624d\u6210\u7acb<\/td>\n<\/tr>\n<tr>\n<td>\u4f9d\u8d56\u5012\u8f6c\u539f\u5219<\/td>\n<td>\u624b\u6bb5 \u2014\u2014 \u901a\u8fc7\u4f9d\u8d56\u62bd\u8c61&#xff0c;\u8fbe\u6210\u4e0a\u9762\u7684\u76ee\u6807<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5b83\u4eec\u76f8\u8f85\u76f8\u6210&#xff0c;\u76f8\u4e92\u8865\u5145&#xff0c;\u76ee\u6807\u4e00\u81f4&#xff0c;\u53ea\u662f\u5206\u6790\u95ee\u9898\u65f6\u6240\u7ad9\u89d2\u5ea6\u4e0d\u540c\u800c\u5df2\u3002<\/p>\n<p>&#x1f4ac; \u4eba\u8bdd\u7248\u603b\u7ed3<\/p>\n<ul>\n<li>\n<p>\u5f00\u95ed\u56de\u7b54&#034;\u8981\u8fbe\u6210\u4ec0\u4e48&#034;<\/p>\n<\/li>\n<li>\n<p>\u91cc\u6c0f\u56de\u7b54&#034;\u51ed\u4ec0\u4e48\u80fd\u66ff\u6362&#034;<\/p>\n<\/li>\n<li>\n<p>\u4f9d\u8d56\u5012\u7f6e\u56de\u7b54&#034;\u5177\u4f53\u600e\u4e48\u505a&#034;<\/p>\n<\/li>\n<\/ul>\n<h5 id=\"C%2B%2B%20%E8%A7%86%E8%A7%92%E7%9A%84%E5%85%B3%E9%94%AE%E6%94%B6%E7%9B%8A\" style=\"background-color:transparent\">C&#043;&#043; \u89c6\u89d2\u7684\u5173\u952e\u6536\u76ca<\/h5>\n<p> <span class=\"hljs-comment\">\/\/ \u91cd\u6784\u524d&#xff1a;DataBiz.cpp<\/span><br \/>\n<span class=\"hljs-meta\">#<span class=\"hljs-keyword\">include<\/span> <span class=\"hljs-string\">&#034;XMLConvert.h&#034;<\/span> \u00a0 \u00a0 <span class=\"hljs-comment\">\/\/ \u2190 \u6362\u5b9e\u73b0 &#061; \u6539\u8fd9\u4e2a\u6587\u4ef6 &#061; \u91cd\u65b0\u7f16\u8bd1<\/span><\/span><br \/>\n\u200b<br \/>\n<span class=\"hljs-comment\">\/\/ \u91cd\u6784\u540e&#xff1a;DataBiz.cpp<\/span><br \/>\n<span class=\"hljs-meta\">#<span class=\"hljs-keyword\">include<\/span> <span class=\"hljs-string\">&#034;DataConvert.h&#034;<\/span> \u00a0 \u00a0<span class=\"hljs-comment\">\/\/ \u2190 \u53ea\u4f9d\u8d56\u62bd\u8c61\u5934\u6587\u4ef6&#xff0c;\u6362\u5b9e\u73b0\u4e0e\u672c\u6587\u4ef6\u65e0\u5173<\/span><\/span> <\/p>\n<p>\u7f16\u8bd1\u671f\u4f9d\u8d56\u65b9\u5411\u53d8\u4e86 \u2014\u2014 \u8fd9\u624d\u662f DIP \u5728 C&#043;&#043; \u8fd9\u7c7b\u7f16\u8bd1\u578b\u8bed\u8a00\u91cc\u7684\u6700\u5927\u4ef7\u503c&#xff1a;\u51cf\u5c11\u91cd\u7f16\u8bd1\u8303\u56f4&#xff0c;\u7f29\u77ed\u6784\u5efa\u65f6\u95f4&#xff0c;\u540c\u65f6\u8ba9\u6a21\u5757\u53ef\u4ee5\u88ab\u5355\u72ec\u6d4b\u8bd5&#xff08;\u6ce8\u5165 Mock \u5b9e\u73b0&#xff09;\u3002<\/p>\n<hr \/>\n<h4 id=\"1.3.5%20%E6%8E%A5%E5%8F%A3%E9%9A%94%E7%A6%BB%E5%8E%9F%E5%88%99%20ISP\" style=\"background-color:transparent\">1.3.5 \u63a5\u53e3\u9694\u79bb\u539f\u5219 ISP<\/h4>\n<p>\u63a5\u53e3\u9694\u79bb\u539f\u5219&#xff08;Interface Segregation Principle, ISP&#xff09;&#xff0c;\u5176\u5b9a\u4e49\u5982\u4e0b&#xff1a;<\/p>\n<p>Clients should not be forced to depend upon interfaces that they do not use.<\/p>\n<p>\u5ba2\u6237\u7aef\u4e0d\u5e94\u8be5\u4f9d\u8d56\u90a3\u4e9b\u5b83\u4e0d\u9700\u8981\u7684\u63a5\u53e3\u3002<\/p>\n<p>\u6839\u636e\u63a5\u53e3\u9694\u79bb\u539f\u5219&#xff0c;\u5f53\u4e00\u4e2a\u63a5\u53e3\u592a\u5927\u65f6&#xff0c;\u6211\u4eec\u9700\u8981\u5c06\u5b83\u5206\u5272\u6210\u4e00\u4e9b\u66f4\u7ec6\u5c0f\u7684\u63a5\u53e3&#xff0c;\u4f7f\u7528\u8be5\u63a5\u53e3\u7684\u5ba2\u6237\u7aef\u4ec5\u9700\u77e5\u9053\u4e0e\u4e4b\u76f8\u5173\u7684\u65b9\u6cd5\u5373\u53ef\u3002<\/p>\n<p>\u6bcf\u4e00\u4e2a\u63a5\u53e3\u5e94\u8be5\u627f\u62c5\u4e00\u79cd\u76f8\u5bf9\u72ec\u7acb\u7684\u89d2\u8272&#xff0c;\u4e0d\u5e72\u4e0d\u8be5\u5e72\u7684\u4e8b&#xff0c;\u8be5\u5e72\u7684\u4e8b\u90fd\u8981\u5e72\u3002<\/p>\n<p>&#x1f4ac; \u4eba\u8bdd\u7248 \u63a5\u53e3\u597d\u6bd4\u9910\u5385\u7684\u83dc\u5355\u3002\u5982\u679c\u4e00\u5f20\u83dc\u5355\u4e0a\u6709 200 \u9053\u83dc&#xff0c;\u800c\u4f60\u53ea\u60f3\u559d\u676f\u6c34&#xff0c;\u4f60\u5374\u5f97\u628a\u6574\u5f20\u83dc\u5355\u4ece\u5934\u770b\u5230\u5c3e \u2014\u2014 \u8fd9\u4e0d\u5408\u7406\u3002 \u66f4\u597d\u7684\u505a\u6cd5\u662f\u62c6\u6210&#034;\u65e9\u9910\u5355&#034;&#034;\u5348\u5e02\u5355&#034;&#034;\u996e\u54c1\u5355&#034;\u3002<\/p>\n<p>\u5224\u65ad\u4f9d\u636e\u4e0d\u662f&#034;\u63a5\u53e3\u5927\u4e0d\u5927&#034;&#xff0c;\u800c\u662f&#xff1a;\u6709\u6ca1\u6709\u54ea\u4e2a\u5b9e\u73b0\u7c7b\/\u8c03\u7528\u65b9&#xff0c;\u88ab\u8feb\u9762\u5bf9\u5b83\u6839\u672c\u4e0d\u7528\u7684\u65b9\u6cd5&#xff1f;<\/p>\n<h5 id=\"%22%E6%8E%A5%E5%8F%A3%22%E7%9A%84%E4%B8%A4%E7%A7%8D%E4%B8%8D%E5%90%8C%E5%90%AB%E4%B9%89\" style=\"background-color:transparent\">&#034;\u63a5\u53e3&#034;\u7684\u4e24\u79cd\u4e0d\u540c\u542b\u4e49<\/h5>\n<table>\n<tr>\u542b\u4e49\u8bf4\u660e\u5728 ISP \u4e2d\u7684\u7406\u89e3<\/tr>\n<tbody>\n<tr>\n<td>\u903b\u8f91\u62bd\u8c61<\/td>\n<td>\u4e00\u4e2a\u7c7b\u578b\u6240\u5177\u6709\u7684\u65b9\u6cd5\u7279\u5f81\u7684\u96c6\u5408&#xff0c;\u4ec5\u4ec5\u662f\u4e00\u79cd\u903b\u8f91\u4e0a\u7684\u62bd\u8c61<\/td>\n<td>\u53ef\u4ee5\u7406\u89e3\u6210\u4e00\u79cd\u89d2\u8272\u3002\u4e00\u4e2a\u63a5\u53e3\u53ea\u80fd\u4ee3\u8868\u4e00\u79cd\u89d2\u8272&#xff0c;\u6b64\u65f6\u4e5f\u53ef\u4ee5\u79f0\u4e4b\u4e3a&#034;\u89d2\u8272\u9694\u79bb\u539f\u5219&#034;<\/td>\n<\/tr>\n<tr>\n<td>\u8bed\u8a00\u7ea7\u63a5\u53e3<\/td>\n<td>\u67d0\u79cd\u8bed\u8a00\u5177\u4f53\u7684 &#034;interface&#034; \u5b9a\u4e49&#xff0c;\u6709\u4e25\u683c\u7684\u5b9a\u4e49\u548c\u7ed3\u6784&#xff08;\u5982 Java \u4e2d\u7684 interface&#xff09;<\/td>\n<td>\u63a5\u53e3\u4ec5\u4ec5\u63d0\u4f9b\u5ba2\u6237\u7aef\u9700\u8981\u7684\u884c\u4e3a&#xff0c;\u5ba2\u6237\u7aef\u4e0d\u9700\u8981\u7684\u884c\u4e3a\u5219\u9690\u85cf\u8d77\u6765\u3002\u5e94\u5f53\u4e3a\u5ba2\u6237\u7aef\u63d0\u4f9b\u5c3d\u53ef\u80fd\u5c0f\u7684\u5355\u72ec\u7684\u63a5\u53e3&#xff0c;\u800c\u4e0d\u8981\u63d0\u4f9b\u5927\u7684\u603b\u63a5\u53e3<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&#x1f4ac; \u4eba\u8bdd\u7248&#xff08;C&#043;&#043; \u89c6\u89d2&#xff09; C&#043;&#043; \u6ca1\u6709 interface \u5173\u952e\u5b57&#xff0c;\u6211\u4eec\u7528\u7684\u662f\u53ea\u6709\u7eaf\u865a\u51fd\u6570\u7684\u62bd\u8c61\u7c7b\u3002 \u6240\u4ee5&#034;\u63a5\u53e3\u9694\u79bb&#034;\u5728 C&#043;&#043; \u91cc\u66f4\u9700\u8981\u6ce8\u610f \u2014\u2014 \u56e0\u4e3a\u4e00\u4e2a\u62bd\u8c61\u7c7b\u5f88\u5bb9\u6613\u88ab\u987a\u624b\u52a0\u4e0a\u6570\u636e\u6210\u5458\u548c\u5b9e\u73b0&#xff0c;\u9010\u6e10&#034;\u53d8\u8d28&#034;\u6210\u5927\u6742\u70e9\u3002<\/p>\n<h5 id=\"%E5%8F%8D%E4%BE%8B%EF%BC%9A%E4%B8%80%E4%B8%AA%E5%8C%85%E5%8A%9E%E6%89%80%E6%9C%89%E7%9A%84%E8%83%96%E6%8E%A5%E5%8F%A3\">\u53cd\u4f8b&#xff1a;\u4e00\u4e2a\u5305\u529e\u6240\u6709\u7684\u80d6\u63a5\u53e3<\/h5>\n<p> <span class=\"hljs-comment\">\/\/ \u274c \u8fdd\u53cd ISP&#xff1a;\u80d6\u63a5\u53e3&#xff08;Fat Interface&#xff09;<\/span><br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">IUserService<\/span> {<br \/>\n<span class=\"hljs-keyword\">public<\/span>:<br \/>\n \u00a0 \u00a0<span class=\"hljs-keyword\">virtual<\/span> ~<span class=\"hljs-built_in\">IUserService<\/span>() &#061; <span class=\"hljs-keyword\">default<\/span>;<br \/>\n\u200b<br \/>\n \u00a0 \u00a0<span class=\"hljs-comment\">\/\/ \u8ba4\u8bc1\u76f8\u5173<\/span><br \/>\n \u00a0 \u00a0<span class=\"hljs-function\"><span class=\"hljs-keyword\">virtual<\/span> <span class=\"hljs-type\">bool<\/span> <span class=\"hljs-title\">login<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">const<\/span> std::string&amp; user, <span class=\"hljs-type\">const<\/span> std::string&amp; pwd)<\/span> <\/span>&#061; <span class=\"hljs-number\">0<\/span>;<br \/>\n \u00a0 \u00a0<span class=\"hljs-function\"><span class=\"hljs-keyword\">virtual<\/span> <span class=\"hljs-type\">void<\/span> <span class=\"hljs-title\">logout<\/span><span class=\"hljs-params\">()<\/span> <\/span>&#061; <span class=\"hljs-number\">0<\/span>;<br \/>\n \u00a0 \u00a0<span class=\"hljs-function\"><span class=\"hljs-keyword\">virtual<\/span> <span class=\"hljs-type\">void<\/span> <span class=\"hljs-title\">registerUser<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">const<\/span> User&amp; user)<\/span> <\/span>&#061; <span class=\"hljs-number\">0<\/span>;<br \/>\n\u200b<br \/>\n \u00a0 \u00a0<span class=\"hljs-comment\">\/\/ \u67e5\u8be2\u76f8\u5173<\/span><br \/>\n \u00a0 \u00a0<span class=\"hljs-function\"><span class=\"hljs-keyword\">virtual<\/span> User <span class=\"hljs-title\">queryById<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">int<\/span> id)<\/span> <\/span>&#061; <span class=\"hljs-number\">0<\/span>;<br \/>\n \u00a0 \u00a0<span class=\"hljs-function\"><span class=\"hljs-keyword\">virtual<\/span> std::vector&lt;User&gt; <span class=\"hljs-title\">queryAll<\/span><span class=\"hljs-params\">()<\/span> <\/span>&#061; <span class=\"hljs-number\">0<\/span>;<br \/>\n\u200b<br \/>\n \u00a0 \u00a0<span class=\"hljs-comment\">\/\/ \u6570\u636e\u5bfc\u5165\u5bfc\u51fa<\/span><br \/>\n \u00a0 \u00a0<span class=\"hljs-function\"><span class=\"hljs-keyword\">virtual<\/span> <span class=\"hljs-type\">void<\/span> <span class=\"hljs-title\">exportExcel<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">const<\/span> std::string&amp; file)<\/span> <\/span>&#061; <span class=\"hljs-number\">0<\/span>;<br \/>\n \u00a0 \u00a0<span class=\"hljs-function\"><span class=\"hljs-keyword\">virtual<\/span> <span class=\"hljs-type\">void<\/span> <span class=\"hljs-title\">importExcel<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">const<\/span> std::string&amp; file)<\/span> <\/span>&#061; <span class=\"hljs-number\">0<\/span>;<br \/>\n\u200b<br \/>\n \u00a0 \u00a0<span class=\"hljs-comment\">\/\/ \u901a\u77e5\u76f8\u5173<\/span><br \/>\n \u00a0 \u00a0<span class=\"hljs-function\"><span class=\"hljs-keyword\">virtual<\/span> <span class=\"hljs-type\">void<\/span> <span class=\"hljs-title\">sendSms<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">const<\/span> std::string&amp; phone)<\/span> <\/span>&#061; <span class=\"hljs-number\">0<\/span>;<br \/>\n \u00a0 \u00a0<span class=\"hljs-function\"><span class=\"hljs-keyword\">virtual<\/span> <span class=\"hljs-type\">void<\/span> <span class=\"hljs-title\">sendEmail<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">const<\/span> std::string&amp; addr)<\/span> <\/span>&#061; <span class=\"hljs-number\">0<\/span>;<br \/>\n}; <\/p>\n<p>\u540e\u679c&#xff1a;<\/p>\n<p> <span class=\"hljs-comment\">\/\/ \u6211\u53ea\u60f3\u505a\u4e00\u4e2a&#034;\u7528\u6237\u67e5\u8be2\u670d\u52a1&#034;&#xff0c;\u5374\u88ab\u8feb\u5b9e\u73b0 10 \u4e2a\u65b9\u6cd5<\/span><br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">UserQueryServiceImpl<\/span> : <span class=\"hljs-keyword\">public<\/span> IUserService {<br \/>\n<span class=\"hljs-keyword\">public<\/span>:<br \/>\n \u00a0 \u00a0<span class=\"hljs-function\">User <span class=\"hljs-title\">queryById<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">int<\/span> id)<\/span> <span class=\"hljs-keyword\">override<\/span> <\/span>{ <span class=\"hljs-comment\">\/* \u771f\u6b63\u60f3\u505a\u7684\u4e8b *\/<\/span> }<br \/>\n \u00a0 \u00a0<span class=\"hljs-function\">std::vector&lt;User&gt; <span class=\"hljs-title\">queryAll<\/span><span class=\"hljs-params\">()<\/span> <span class=\"hljs-keyword\">override<\/span> <\/span>{ <span class=\"hljs-comment\">\/* \u771f\u6b63\u60f3\u505a\u7684\u4e8b *\/<\/span> }<br \/>\n\u200b<br \/>\n \u00a0 \u00a0<span class=\"hljs-function\"><span class=\"hljs-type\">bool<\/span> <span class=\"hljs-title\">login<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">const<\/span> std::string&amp;, <span class=\"hljs-type\">const<\/span> std::string&amp;)<\/span> <span class=\"hljs-keyword\">override<\/span> <\/span>{<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0<span class=\"hljs-keyword\">throw<\/span> std::<span class=\"hljs-built_in\">logic_error<\/span>(<span class=\"hljs-string\">&#034;not supported&#034;<\/span>); \u00a0 \u00a0<span class=\"hljs-comment\">\/\/ \u274c \u88ab\u8feb\u5199\u7a7a\u5b9e\u73b0\/\u629b\u5f02\u5e38<\/span><br \/>\n \u00a0  }<br \/>\n \u00a0 \u00a0<span class=\"hljs-function\"><span class=\"hljs-type\">void<\/span> <span class=\"hljs-title\">exportExcel<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">const<\/span> std::string&amp;)<\/span> <span class=\"hljs-keyword\">override<\/span> <\/span>{<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0<span class=\"hljs-keyword\">throw<\/span> std::<span class=\"hljs-built_in\">logic_error<\/span>(<span class=\"hljs-string\">&#034;not supported&#034;<\/span>); \u00a0 \u00a0<span class=\"hljs-comment\">\/\/ \u274c \u987a\u5e26\u8fdd\u53cd\u4e86 LSP<\/span><br \/>\n \u00a0  }<br \/>\n \u00a0 \u00a0<span class=\"hljs-comment\">\/\/ &#8230; \u8fd8\u6709 6 \u4e2a<\/span><br \/>\n}; <\/p>\n<p>\u26a0\ufe0f \u6ce8\u610f\u8fd9\u91cc\u7684\u8fde\u9501\u53cd\u5e94 \u88ab\u8feb\u5b9e\u73b0\u7684\u7a7a\u65b9\u6cd5\/\u629b\u5f02\u5e38\u65b9\u6cd5&#xff0c;\u540c\u65f6\u8fdd\u53cd\u4e86\u91cc\u6c0f\u66ff\u6362\u539f\u5219&#xff08;1.3.3 \u91cc\u7684&#034;\u5b50\u7c7b\u4e2d\u7684\u9000\u5316\u65b9\u6cd5&#034;&#xff09;\u3002 \u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48 SOLID \u4e94\u6761\u539f\u5219\u7ecf\u5e38\u9700\u8981\u4e00\u8d77\u770b\u3002<\/p>\n<h5 id=\"%E9%87%8D%E6%9E%84%E5%90%8E%EF%BC%9A%E6%8C%89%E8%A7%92%E8%89%B2%E6%8B%86%E5%88%86%E6%88%90%E5%B0%8F%E6%8E%A5%E5%8F%A3\" style=\"background-color:transparent\">\u91cd\u6784\u540e&#xff1a;\u6309\u89d2\u8272\u62c6\u5206\u6210\u5c0f\u63a5\u53e3<\/h5>\n<p> <span class=\"hljs-comment\">\/\/ \u2705 \u6309&#034;\u89d2\u8272&#034;\u62c6\u5206&#xff0c;\u6bcf\u4e2a\u63a5\u53e3\u53ea\u670d\u52a1\u4e00\u7c7b\u5ba2\u6237\u7aef<\/span><br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">IAuthService<\/span> { \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\">\/\/ \u8ba4\u8bc1\u89d2\u8272<\/span><br \/>\n<span class=\"hljs-keyword\">public<\/span>:<br \/>\n \u00a0 \u00a0<span class=\"hljs-keyword\">virtual<\/span> ~<span class=\"hljs-built_in\">IAuthService<\/span>() &#061; <span class=\"hljs-keyword\">default<\/span>;<br \/>\n \u00a0 \u00a0<span class=\"hljs-function\"><span class=\"hljs-keyword\">virtual<\/span> <span class=\"hljs-type\">bool<\/span> <span class=\"hljs-title\">login<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">const<\/span> std::string&amp; user, <span class=\"hljs-type\">const<\/span> std::string&amp; pwd)<\/span> <\/span>&#061; <span class=\"hljs-number\">0<\/span>;<br \/>\n \u00a0 \u00a0<span class=\"hljs-function\"><span class=\"hljs-keyword\">virtual<\/span> <span class=\"hljs-type\">void<\/span> <span class=\"hljs-title\">logout<\/span><span class=\"hljs-params\">()<\/span> <\/span>&#061; <span class=\"hljs-number\">0<\/span>;<br \/>\n \u00a0 \u00a0<span class=\"hljs-function\"><span class=\"hljs-keyword\">virtual<\/span> <span class=\"hljs-type\">void<\/span> <span class=\"hljs-title\">registerUser<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">const<\/span> User&amp; user)<\/span> <\/span>&#061; <span class=\"hljs-number\">0<\/span>;<br \/>\n};<br \/>\n\u200b<br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">IUserQueryService<\/span> { \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<span class=\"hljs-comment\">\/\/ \u67e5\u8be2\u89d2\u8272<\/span><br \/>\n<span class=\"hljs-keyword\">public<\/span>:<br \/>\n \u00a0 \u00a0<span class=\"hljs-keyword\">virtual<\/span> ~<span class=\"hljs-built_in\">IUserQueryService<\/span>() &#061; <span class=\"hljs-keyword\">default<\/span>;<br \/>\n \u00a0 \u00a0<span class=\"hljs-function\"><span class=\"hljs-keyword\">virtual<\/span> User <span class=\"hljs-title\">queryById<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">int<\/span> id)<\/span> <\/span>&#061; <span class=\"hljs-number\">0<\/span>;<br \/>\n \u00a0 \u00a0<span class=\"hljs-function\"><span class=\"hljs-keyword\">virtual<\/span> std::vector&lt;User&gt; <span class=\"hljs-title\">queryAll<\/span><span class=\"hljs-params\">()<\/span> <\/span>&#061; <span class=\"hljs-number\">0<\/span>;<br \/>\n};<br \/>\n\u200b<br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">IUserDataService<\/span> { \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\">\/\/ \u6570\u636e\u5bfc\u5165\u5bfc\u51fa\u89d2\u8272<\/span><br \/>\n<span class=\"hljs-keyword\">public<\/span>:<br \/>\n \u00a0 \u00a0<span class=\"hljs-keyword\">virtual<\/span> ~<span class=\"hljs-built_in\">IUserDataService<\/span>() &#061; <span class=\"hljs-keyword\">default<\/span>;<br \/>\n \u00a0 \u00a0<span class=\"hljs-function\"><span class=\"hljs-keyword\">virtual<\/span> <span class=\"hljs-type\">void<\/span> <span class=\"hljs-title\">exportExcel<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">const<\/span> std::string&amp; file)<\/span> <\/span>&#061; <span class=\"hljs-number\">0<\/span>;<br \/>\n \u00a0 \u00a0<span class=\"hljs-function\"><span class=\"hljs-keyword\">virtual<\/span> <span class=\"hljs-type\">void<\/span> <span class=\"hljs-title\">importExcel<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">const<\/span> std::string&amp; file)<\/span> <\/span>&#061; <span class=\"hljs-number\">0<\/span>;<br \/>\n};<br \/>\n\u200b<br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">INotifyService<\/span> { \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <span class=\"hljs-comment\">\/\/ \u901a\u77e5\u89d2\u8272<\/span><br \/>\n<span class=\"hljs-keyword\">public<\/span>:<br \/>\n \u00a0 \u00a0<span class=\"hljs-keyword\">virtual<\/span> ~<span class=\"hljs-built_in\">INotifyService<\/span>() &#061; <span class=\"hljs-keyword\">default<\/span>;<br \/>\n \u00a0 \u00a0<span class=\"hljs-function\"><span class=\"hljs-keyword\">virtual<\/span> <span class=\"hljs-type\">void<\/span> <span class=\"hljs-title\">sendSms<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">const<\/span> std::string&amp; phone)<\/span> <\/span>&#061; <span class=\"hljs-number\">0<\/span>;<br \/>\n \u00a0 \u00a0<span class=\"hljs-function\"><span class=\"hljs-keyword\">virtual<\/span> <span class=\"hljs-type\">void<\/span> <span class=\"hljs-title\">sendEmail<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">const<\/span> std::string&amp; addr)<\/span> <\/span>&#061; <span class=\"hljs-number\">0<\/span>;<br \/>\n}; <\/p>\n<p>\u73b0\u5728&#xff1a;<\/p>\n<p> <span class=\"hljs-comment\">\/\/ \u53ea\u4f9d\u8d56\u9700\u8981\u7684\u63a5\u53e3&#xff0c;\u65b9\u6cd5\u4e00\u4e2a\u4e0d\u591a\u3001\u4e00\u4e2a\u4e0d\u5c11<\/span><br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">UserQueryServiceImpl<\/span> : <span class=\"hljs-keyword\">public<\/span> IUserQueryService {<br \/>\n<span class=\"hljs-keyword\">public<\/span>:<br \/>\n \u00a0 \u00a0<span class=\"hljs-function\">User <span class=\"hljs-title\">queryById<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">int<\/span> id)<\/span> <span class=\"hljs-keyword\">override<\/span> <\/span>{ <span class=\"hljs-comment\">\/* &#8230; *\/<\/span> }<br \/>\n \u00a0 \u00a0<span class=\"hljs-function\">std::vector&lt;User&gt; <span class=\"hljs-title\">queryAll<\/span><span class=\"hljs-params\">()<\/span> <span class=\"hljs-keyword\">override<\/span> <\/span>{ <span class=\"hljs-comment\">\/* &#8230; *\/<\/span> }<br \/>\n};<br \/>\n\u200b<br \/>\n<span class=\"hljs-comment\">\/\/ \u9700\u8981\u7ec4\u5408\u80fd\u529b\u65f6&#xff0c;\u7528\u591a\u7ee7\u627f&#034;\u6309\u9700\u62fc\u88c5&#034;&#xff0c;\u800c\u4e0d\u662f\u88ab\u4e00\u4e2a\u80d6\u63a5\u53e3\u7ed1\u6b7b<\/span><br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">FullUserService<\/span> : <span class=\"hljs-keyword\">public<\/span> IAuthService,<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<span class=\"hljs-keyword\">public<\/span> IUserQueryService,<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0<span class=\"hljs-keyword\">public<\/span> INotifyService {<br \/>\n \u00a0 \u00a0<span class=\"hljs-comment\">\/\/ &#8230;<\/span><br \/>\n}; <\/p>\n<h5 id=\"%E2%9A%A0%EF%B8%8F%20%E4%BD%BF%E7%94%A8%E6%8E%A5%E5%8F%A3%E9%9A%94%E7%A6%BB%E5%8E%9F%E5%88%99%E6%97%B6%E7%9A%84%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9\" style=\"background-color:transparent\">\u26a0\ufe0f \u4f7f\u7528\u63a5\u53e3\u9694\u79bb\u539f\u5219\u65f6\u7684\u6ce8\u610f\u4e8b\u9879<\/h5>\n<p>\u63a7\u5236\u63a5\u53e3\u7684\u7c92\u5ea6&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u63a5\u53e3\u4e0d\u80fd\u592a\u5c0f \u2014\u2014 \u5982\u679c\u592a\u5c0f\u4f1a\u5bfc\u81f4\u7cfb\u7edf\u4e2d\u63a5\u53e3\u6cdb\u6ee5&#xff0c;\u4e0d\u5229\u4e8e\u7ef4\u62a4&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u63a5\u53e3\u4e5f\u4e0d\u80fd\u592a\u5927 \u2014\u2014 \u592a\u5927\u7684\u63a5\u53e3\u5c06\u8fdd\u80cc\u63a5\u53e3\u9694\u79bb\u539f\u5219&#xff0c;\u7075\u6d3b\u6027\u8f83\u5dee&#xff0c;\u4f7f\u7528\u8d77\u6765\u5f88\u4e0d\u65b9\u4fbf\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u4e00\u822c\u800c\u8a00&#xff0c;\u63a5\u53e3\u4e2d\u4ec5\u5305\u542b\u4e3a\u67d0\u4e00\u7c7b\u7528\u6237\u5b9a\u5236\u7684\u65b9\u6cd5\u5373\u53ef&#xff0c;\u4e0d\u5e94\u8be5\u5f3a\u8feb\u5ba2\u6237\u4f9d\u8d56\u4e8e\u90a3\u4e9b\u5b83\u4eec\u4e0d\u7528\u7684\u65b9\u6cd5\u3002<\/p>\n<p>&#x1f4ac; \u4eba\u8bdd\u7248 \u62c6\u63a5\u53e3\u548c\u62c6\u7c7b\u4e00\u6837&#xff0c;\u6ca1\u6709\u7edd\u5bf9\u6807\u51c6\u3002\u5224\u65ad\u4f9d\u636e\u662f&#034;\u6709\u6ca1\u6709\u5ba2\u6237\u7aef\u88ab\u8feb\u9762\u5bf9\u5b83\u4e0d\u7528\u7684\u4e1c\u897f&#034;&#xff0c;\u800c\u4e0d\u662f&#034;\u63a5\u53e3\u91cc\u65b9\u6cd5\u8d85\u8fc7 N \u4e2a\u5c31\u8be5\u62c6&#034;\u3002 \u62c6\u8fc7\u5934&#xff0c;\u4f60\u4f1a\u5f97\u5230 50 \u4e2a\u53ea\u6709\u4e00\u4e2a\u65b9\u6cd5\u7684\u63a5\u53e3 \u2014\u2014 \u90a3\u662f\u53e6\u4e00\u79cd\u707e\u96be\u3002<\/p>\n<hr \/>\n<h2 id=\"1.4%20%E7%BB%84%E5%90%88%E5%A4%8D%E7%94%A8%E5%8E%9F%E5%88%99%20CRP\" style=\"background-color:transparent\">1.4 \u7ec4\u5408\u590d\u7528\u539f\u5219 CRP<\/h2>\n<p>\u7ec4\u5408\u590d\u7528\u539f\u5219&#xff08;Composite Reuse Principle, CRP&#xff09;\u53c8\u79f0\u4e3a\u7ec4\u5408\/\u805a\u5408\u590d\u7528\u539f\u5219&#xff08;Composition\/Aggregate Reuse Principle, CARP&#xff09;&#xff0c;\u5176\u5b9a\u4e49\u5982\u4e0b&#xff1a;<\/p>\n<p>Favor composition of objects over inheritance as reuse mechanism.<\/p>\n<p>\u4f18\u5148\u4f7f\u7528\u5bf9\u8c61\u7684\u7ec4\u5408&#xff0c;\u800c\u4e0d\u662f\u4f7f\u7528\u7ee7\u627f\u6765\u8fbe\u5230\u590d\u7528\u7684\u76ee\u7684\u3002<\/p>\n<p>\u5728\u9762\u5411\u5bf9\u8c61\u8bbe\u8ba1\u4e2d&#xff0c;\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u65b9\u6cd5\u5728\u4e0d\u540c\u7684\u73af\u5883\u4e2d\u590d\u7528\u5df2\u6709\u7684\u8bbe\u8ba1\u548c\u5b9e\u73b0&#xff0c;\u5373\u901a\u8fc7\u7ec4\u5408\/\u805a\u5408\u5173\u7cfb\u6216\u901a\u8fc7\u7ee7\u627f&#xff0c;\u4f46\u9996\u5148\u5e94\u8be5\u8003\u8651\u4f7f\u7528\u7ec4\u5408\/\u805a\u5408\u3002<\/p>\n<p>&#x1f4ac; \u4eba\u8bdd\u7248 \u8fd9\u53e5\u8bdd\u542c\u8d77\u6765\u50cf&#034;\u7ec4\u5408\u6bd4\u7ee7\u627f\u597d&#034;&#xff0c;\u4f46\u5b83\u771f\u6b63\u7684\u610f\u601d\u662f&#xff1a; \u7ee7\u627f\u662f&#034;\u6700\u7d27&#034;\u7684\u4e00\u79cd\u8026\u5408&#xff0c;\u6240\u4ee5\u8981\u7528\u5728\u6700\u786e\u5b9a\u7684\u5730\u65b9&#xff1b;\u7ec4\u5408\u662f&#034;\u677e&#034;\u7684&#xff0c;\u9002\u5408\u7528\u5728\u4f1a\u53d8\u7684\u5730\u65b9\u3002 \u4e0d\u662f\u7981\u6b62\u7ee7\u627f&#xff0c;\u800c\u662f\u628a\u7ee7\u627f\u964d\u7ea7\u4e3a&#034;\u5907\u9009\u9879&#034;&#xff0c;\u800c\u4e0d\u662f&#034;\u9ed8\u8ba4\u9879&#034;\u3002<\/p>\n<h4 id=\"%E4%B8%BA%E4%BB%80%E4%B9%88%E4%BC%98%E5%85%88%E7%BB%84%E5%90%88%EF%BC%9F\">\u4e3a\u4ec0\u4e48\u4f18\u5148\u7ec4\u5408&#xff1f;<\/h4>\n<p>\u2460 \u901a\u8fc7\u7ee7\u627f\u6765\u8fdb\u884c\u590d\u7528\u7684\u4e3b\u8981\u95ee\u9898&#xff1a;<\/p>\n<p>\u7ee7\u627f\u590d\u7528\u4f1a\u7834\u574f\u7cfb\u7edf\u7684\u5c01\u88c5\u6027&#xff0c;\u56e0\u4e3a\u7ee7\u627f\u4f1a\u5c06\u57fa\u7c7b\u7684\u5b9e\u73b0\u7ec6\u8282\u66b4\u9732\u7ed9\u5b50\u7c7b\u3002\u7531\u4e8e\u57fa\u7c7b\u7684\u5185\u90e8\u7ec6\u8282\u901a\u5e38\u5bf9\u5b50\u7c7b\u6765\u8bf4\u662f\u53ef\u89c1\u7684&#xff0c;\u6240\u4ee5\u8fd9\u79cd\u590d\u7528\u53c8\u79f0 &#034;\u767d\u7bb1&#034;\u590d\u7528\u3002<\/p>\n<ul>\n<li>\n<p>\u5982\u679c\u57fa\u7c7b\u53d1\u751f\u6539\u53d8&#xff0c;\u90a3\u4e48\u5b50\u7c7b\u7684\u5b9e\u73b0\u4e5f\u4e0d\u5f97\u4e0d\u53d1\u751f\u6539\u53d8&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u4ece\u57fa\u7c7b\u7ee7\u627f\u800c\u6765\u7684\u5b9e\u73b0\u662f\u9759\u6001\u7684&#xff0c;\u4e0d\u53ef\u80fd\u5728\u8fd0\u884c\u65f6\u53d1\u751f\u6539\u53d8&#xff0c;\u6ca1\u6709\u8db3\u591f\u7684\u7075\u6d3b\u6027&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u800c\u4e14\u7ee7\u627f\u53ea\u80fd\u5728\u6709\u9650\u7684\u73af\u5883\u4e2d\u4f7f\u7528&#xff08;\u5982\u7c7b\u6ca1\u6709\u58f0\u660e\u4e3a\u4e0d\u80fd\u88ab\u7ee7\u627f&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u2461 \u7ec4\u5408\/\u805a\u5408\u590d\u7528\u7684\u4f18\u52bf&#xff1a;<\/p>\n<p>\u7531\u4e8e\u7ec4\u5408\u6216\u805a\u5408\u5173\u7cfb\u53ef\u4ee5\u5c06\u5df2\u6709\u7684\u5bf9\u8c61&#xff08;\u4e5f\u53ef\u79f0\u4e3a\u6210\u5458\u5bf9\u8c61&#xff09;\u7eb3\u5165\u5230\u65b0\u5bf9\u8c61\u4e2d&#xff0c;\u4f7f\u4e4b\u6210\u4e3a\u65b0\u5bf9\u8c61\u7684\u4e00\u90e8\u5206&#xff0c;\u56e0\u6b64\u65b0\u5bf9\u8c61\u53ef\u4ee5\u8c03\u7528\u5df2\u6709\u5bf9\u8c61\u7684\u529f\u80fd\u3002<\/p>\n<ul>\n<li>\n<p>\u8fd9\u6837\u505a\u53ef\u4ee5\u4f7f\u5f97\u6210\u5458\u5bf9\u8c61\u7684\u5185\u90e8\u5b9e\u73b0\u7ec6\u8282\u5bf9\u4e8e\u65b0\u5bf9\u8c61\u4e0d\u53ef\u89c1&#xff0c;\u6240\u4ee5\u8fd9\u79cd\u590d\u7528\u53c8\u79f0\u4e3a &#034;\u9ed1\u7bb1&#034;\u590d\u7528&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u76f8\u5bf9\u7ee7\u627f\u5173\u7cfb\u800c\u8a00&#xff0c;\u5176\u8026\u5408\u5ea6\u76f8\u5bf9\u8f83\u4f4e&#xff0c;\u6210\u5458\u5bf9\u8c61\u7684\u53d8\u5316\u5bf9\u65b0\u5bf9\u8c61\u7684\u5f71\u54cd\u4e0d\u5927&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u53ef\u4ee5\u5728\u65b0\u5bf9\u8c61\u4e2d\u6839\u636e\u5b9e\u9645\u9700\u8981\u6709\u9009\u62e9\u6027\u5730\u8c03\u7528\u6210\u5458\u5bf9\u8c61\u7684\u64cd\u4f5c&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u7ec4\u5408\u590d\u7528\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u8fdb\u884c&#xff0c;\u65b0\u5bf9\u8c61\u53ef\u4ee5\u52a8\u6001\u5730\u5f15\u7528\u4e0e\u6210\u5458\u5bf9\u8c61\u7c7b\u578b\u76f8\u540c\u7684\u5176\u4ed6\u5bf9\u8c61\u3002<\/p>\n<\/li>\n<\/ul>\n<h4 id=\"%E7%99%BD%E7%AE%B1%E5%A4%8D%E7%94%A8%20vs%20%E9%BB%91%E7%AE%B1%E5%A4%8D%E7%94%A8\">\u767d\u7bb1\u590d\u7528 vs \u9ed1\u7bb1\u590d\u7528<\/h4>\n<table>\n<tr>\u5bf9\u6bd4\u7ef4\u5ea6\u7ee7\u627f&#xff08;\u767d\u7bb1\u590d\u7528&#xff09;\u7ec4\u5408\/\u805a\u5408&#xff08;\u9ed1\u7bb1\u590d\u7528&#xff09;<\/tr>\n<tbody>\n<tr>\n<td>\u5c01\u88c5\u6027<\/td>\n<td>\u274c \u7834\u574f \u2014\u2014 \u57fa\u7c7b\u5b9e\u73b0\u7ec6\u8282\u5bf9\u5b50\u7c7b\u53ef\u89c1<\/td>\n<td>\u2705 \u4fdd\u6301 \u2014\u2014 \u53ea\u901a\u8fc7\u516c\u5f00\u63a5\u53e3\u4ea4\u4e92<\/td>\n<\/tr>\n<tr>\n<td>\u8026\u5408\u5ea6<\/td>\n<td>\u9ad8&#xff08;\u7f16\u8bd1\u671f\u5f3a\u8026\u5408&#xff09;<\/td>\n<td>\u4f4e&#xff08;\u53ef\u9762\u5411\u62bd\u8c61&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u590d\u7528\u65f6\u673a<\/td>\n<td>\u9759\u6001&#xff0c;\u7f16\u8bd1\u671f\u786e\u5b9a<\/td>\n<td>\u52a8\u6001&#xff0c;\u8fd0\u884c\u65f6\u786e\u5b9a<\/td>\n<\/tr>\n<tr>\n<td>\u590d\u7528\u8303\u56f4<\/td>\n<td>\u5168\u90e8\u7ee7\u627f&#xff08;\u4e0d\u80fd\u53ea\u6311\u4e00\u90e8\u5206&#xff09;<\/td>\n<td>\u6709\u9009\u62e9\u6027\u5730\u53ea\u8c03\u7528\u9700\u8981\u7684\u64cd\u4f5c<\/td>\n<\/tr>\n<tr>\n<td>\u57fa\u7c7b\/\u6210\u5458\u53d8\u5316\u5f71\u54cd<\/td>\n<td>\u57fa\u7c7b\u6539\u52a8 \u2192 \u5b50\u7c7b\u53ef\u80fd\u88ab\u8feb\u6539\u52a8<\/td>\n<td>\u6210\u5458\u5bf9\u8c61\u53d8\u5316\u5f71\u54cd\u5c0f<\/td>\n<\/tr>\n<tr>\n<td>\u7075\u6d3b\u6027<\/td>\n<td>\u4f4e<\/td>\n<td>\u9ad8<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&#x1f4ac; \u4eba\u8bdd\u7248<\/p>\n<ul>\n<li>\n<p>\u7ee7\u627f\u662f&#034;\u751f\u4e0b\u6765\u5c31\u5b9a\u4e86&#034; \u2014\u2014 \u4f60\u662f\u8fd9\u79cd\u8239&#xff0c;\u4f60\u8fd9\u8f88\u5b50\u5c31\u662f\u8fd9\u79cd\u8239\u3002<\/p>\n<\/li>\n<li>\n<p>\u7ec4\u5408\u662f&#034;\u51fa\u95e8\u524d\u518d\u6311&#034; \u2014\u2014 \u6211\u4eca\u5929\u88c5\u84b8\u6c7d\u673a&#xff0c;\u660e\u5929\u88c5\u6838\u52a8\u529b\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u8fd8\u6709\u4e00\u53e5\u5173\u952e\u533a\u522b&#xff1a;\u7ee7\u627f\u662f&#034;\u5168\u90e8\u62ff\u8d70&#034;&#xff0c;\u7ec4\u5408\u662f&#034;\u6309\u9700\u53d6\u7528&#034;\u3002 \u4f60\u7ee7\u627f\u4e00\u4e2a\u57fa\u7c7b&#xff0c;\u5c31\u5f97\u8fde\u5b83\u90a3\u4e9b\u4f60\u6839\u672c\u7528\u4e0d\u4e0a\u7684\u65b9\u6cd5\u4e00\u8d77\u7ee7\u627f\u8fc7\u6765&#xff1b;\u800c\u7ec4\u5408\u4e00\u4e2a\u5bf9\u8c61&#xff0c;\u4f60\u53ef\u4ee5\u53ea\u7528\u5b83\u7684\u4e00\u4e2a\u65b9\u6cd5\u3002<\/p>\n<h4 id=\"%E4%B8%80%E8%88%AC%E8%80%8C%E8%A8%80%E7%9A%84%E5%88%A4%E6%96%AD%E6%A0%87%E5%87%86\">\u4e00\u822c\u800c\u8a00\u7684\u5224\u65ad\u6807\u51c6<\/h4>\n<p>\u5982\u679c\u4e24\u4e2a\u7c7b\u4e4b\u95f4\u662f &#034;Has-A&#034; \u7684\u5173\u7cfb\u5e94\u4f7f\u7528\u7ec4\u5408\u6216\u805a\u5408&#xff0c;\u5982\u679c\u662f &#034;Is-A&#034; \u5173\u7cfb\u53ef\u4f7f\u7528\u7ee7\u627f\u3002<\/p>\n<p>&#x1f4ac; \u4eba\u8bdd\u7248<\/p>\n<ul>\n<li>\n<p>Is-A&#xff08;\u662f\u4e00\u4e2a&#xff09;&#xff1a;\u72d7 \u662f \u52a8\u7269 \u2192 \u53ef\u4ee5\u7528\u7ee7\u627f<\/p>\n<\/li>\n<li>\n<p>Has-A&#xff08;\u6709\u4e00\u4e2a&#xff09;&#xff1a;\u6c7d\u8f66 \u6709 \u53d1\u52a8\u673a \u2192 \u5fc5\u987b\u7528\u7ec4\u5408<\/p>\n<\/li>\n<\/ul>\n<p>\u4f46\u6ce8\u610f&#xff1a;\u5373\u4f7f\u662f Is-A&#xff0c;\u4e5f\u8981\u6ee1\u8db3 1.3.3 \u7684\u91cc\u6c0f\u66ff\u6362\u539f\u5219&#xff0c;\u624d\u80fd\u7528\u7ee7\u627f\u3002 &#xff08;\u5706\u662f\u692d\u5706\u5417&#xff1f;\u6570\u5b66\u4e0a\u662f&#xff0c;\u4ee3\u7801\u5951\u7ea6\u4e0a\u4e0d\u662f \u2014\u2014 \u90a3\u5c31\u522b\u7ee7\u627f\u3002&#xff09;<\/p>\n<h4 id=\"%E6%A1%88%E4%BE%8B%EF%BC%9A%E5%A4%9A%E7%BB%B4%E5%BA%A6%E6%89%A9%E5%B1%95%E5%AF%BC%E8%87%B4%E7%9A%84%E5%AD%90%E7%B1%BB%E7%88%86%E7%82%B8\" style=\"background-color:transparent\">\u6848\u4f8b&#xff1a;\u591a\u7ef4\u5ea6\u6269\u5c55\u5bfc\u81f4\u7684\u5b50\u7c7b\u7206\u70b8<\/h4>\n<p>\u5728\u51e0\u4e2a\u7ef4\u5ea6\u4e0a\u6269\u5c55\u4e00\u4e2a\u7c7b&#xff08;\u8d27\u7269\u7c7b\u578b \u00d7 \u53d1\u52a8\u673a\u7c7b\u578b \u00d7 \u5bfc\u822a\u7c7b\u578b&#xff09;\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5b50\u7c7b\u7684\u7ec4\u5408\u7206\u70b8\u3002<\/p>\n<p> <span class=\"hljs-comment\">\/\/ \u274c \u7528\u7ee7\u627f\u53bb\u8986\u76d6\u591a\u4e2a\u72ec\u7acb\u7684&#034;\u53d8\u5316\u7ef4\u5ea6&#034;<\/span><br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">Ship<\/span> { <span class=\"hljs-comment\">\/* &#8230; *\/<\/span> };<br \/>\n\u200b<br \/>\n<span class=\"hljs-comment\">\/\/ \u7ef4\u5ea6\u4e00&#xff1a;\u8d27\u7269\u7c7b\u578b<\/span><br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">CargoShip<\/span>  : <span class=\"hljs-keyword\">public<\/span> Ship {};<br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">TankerShip<\/span> : <span class=\"hljs-keyword\">public<\/span> Ship {};<br \/>\n\u200b<br \/>\n<span class=\"hljs-comment\">\/\/ \u7ef4\u5ea6\u4e8c&#xff1a;\u53d1\u52a8\u673a\u7c7b\u578b \u2014\u2014 \u6bcf\u4e2a\u8d27\u7269\u7c7b\u578b\u90fd\u8981\u518d\u5206\u53c9<\/span><br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">CargoShipSteam<\/span> \u00a0  : <span class=\"hljs-keyword\">public<\/span> CargoShip {};<br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">CargoShipNuclear<\/span>  : <span class=\"hljs-keyword\">public<\/span> CargoShip {};<br \/>\n\u200b<br \/>\n<span class=\"hljs-comment\">\/\/ \u7ef4\u5ea6\u4e09&#xff1a;\u5bfc\u822a\u7c7b\u578b \u2014\u2014 \u53c8\u8981\u518d\u5206\u53c9<\/span><br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">CargoShipSteamGPS<\/span> \u00a0 \u00a0 \u00a0 : <span class=\"hljs-keyword\">public<\/span> CargoShipSteam {};<br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">CargoShipSteamStarChart<\/span> : <span class=\"hljs-keyword\">public<\/span> CargoShipSteam {};<br \/>\n<span class=\"hljs-comment\">\/\/ &#8230; \u7ec4\u5408\u6570\u7ee7\u7eed\u589e\u957f<\/span> <\/p>\n<p>\u5982\u60a8\u6240\u89c1&#xff0c;\u6bcf\u589e\u52a0\u4e00\u4e2a\u53c2\u6570\u90fd\u4f1a\u589e\u52a0\u5b50\u7c7b\u7684\u6570\u91cf\u3002\u5b50\u7c7b\u4e4b\u95f4\u6709\u5f88\u591a\u91cd\u590d\u7684\u4ee3\u7801&#xff0c;\u56e0\u4e3a\u4e00\u4e2a\u5b50\u7c7b\u4e0d\u80fd\u540c\u65f6\u7ee7\u627f\u4e24\u4e2a\u7c7b\u3002<\/p>\n<p>\u5b50\u7c7b\u6570\u91cf &#061; \u5404\u7ef4\u5ea6\u53d6\u503c\u6570\u4e4b\u79ef&#xff1a;<\/p>\n<p> 2&#xff08;\u8d27\u7269&#xff1a;\u666e\u901a \/ \u5371\u9669&#xff09; \u00d7 2&#xff08;\u53d1\u52a8\u673a&#xff1a;\u84b8\u6c7d \/ \u6838&#xff09; \u00d7 2&#xff08;\u5bfc\u822a&#xff1a;GPS \/ \u661f\u56fe&#xff09; &#061; 8 \u4e2a\u5b50\u7c7b<br \/>\n\u518d\u52a0\u4e00\u4e2a<span class=\"hljs-string\">&#034;\u8239\u4f53\u6750\u8d28&#034;<\/span>\u7ef4\u5ea6&#xff08;\u94a2 \/ \u6728&#xff09; \u2192 16 \u4e2a<br \/>\n\u518d\u52a0\u4e00\u4e2a<span class=\"hljs-string\">&#034;\u6d82\u88c5&#034;<\/span>\u7ef4\u5ea6&#xff08;3 \u79cd&#xff09; \u00a0 \u00a0 \u00a0 \u2192 48 \u4e2a <\/p>\n<p>\u8fd9\u4e0d\u662f\u7ebf\u6027\u589e\u957f&#xff0c;\u662f\u4e58\u6cd5\u589e\u957f\u3002<\/p>\n<p>&#x1f4ac; \u4eba\u8bdd\u7248 \u60f3\u8c61\u4f60\u8981\u4e70\u4e00\u4ef6\u8863\u670d&#xff1a;\u9886\u578b \u00d7 \u8896\u957f \u00d7 \u989c\u8272 \u00d7 \u6750\u8d28\u3002 \u5982\u679c\u6bcf\u4e2a\u7ec4\u5408\u90fd\u8981\u5355\u72ec\u751f\u4ea7\u4e00\u6b3e \u2014\u2014 \u90a3\u5de5\u5382\u5f97\u5f00\u591a\u5c11\u6761\u4ea7\u7ebf&#xff1f; \u73b0\u5b9e\u4e2d\u7684\u505a\u6cd5\u662f&#xff1a;\u4f60\u6311\u4e00\u4e2a\u9886\u5b50&#xff0c;\u6311\u4e00\u4e2a\u8896\u5b50&#xff0c;\u6311\u4e00\u79cd\u989c\u8272&#xff0c;\u73b0\u573a\u62fc\u8d77\u6765\u3002<\/p>\n<h4 id=\"%E9%87%8D%E6%9E%84%EF%BC%9A%E7%94%A8%E7%BB%84%E5%90%88%E6%9B%BF%E4%BB%A3%E5%A4%9A%E7%BB%B4%E7%BB%A7%E6%89%BF\" style=\"background-color:transparent\">\u91cd\u6784&#xff1a;\u7528\u7ec4\u5408\u66ff\u4ee3\u591a\u7ef4\u7ee7\u627f<\/h4>\n<p> <span class=\"hljs-comment\">\/\/ \u2500\u2500 \u7ef4\u5ea6\u4e00&#xff1a;\u8d27\u7269\u7c7b\u578b \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500<\/span><br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">Cargo<\/span> {<br \/>\n<span class=\"hljs-keyword\">public<\/span>:<br \/>\n \u00a0 \u00a0<span class=\"hljs-keyword\">virtual<\/span> ~<span class=\"hljs-built_in\">Cargo<\/span>() &#061; <span class=\"hljs-keyword\">default<\/span>;<br \/>\n \u00a0 \u00a0<span class=\"hljs-function\"><span class=\"hljs-keyword\">virtual<\/span> <span class=\"hljs-type\">int<\/span> \u00a0<span class=\"hljs-title\">riskLevel<\/span><span class=\"hljs-params\">()<\/span> <span class=\"hljs-type\">const<\/span> <\/span>&#061; <span class=\"hljs-number\">0<\/span>;<br \/>\n};<br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">NormalCargo<\/span> : <span class=\"hljs-keyword\">public<\/span> Cargo { <span class=\"hljs-keyword\">public<\/span>: <span class=\"hljs-function\"><span class=\"hljs-type\">int<\/span> <span class=\"hljs-title\">riskLevel<\/span><span class=\"hljs-params\">()<\/span> <span class=\"hljs-type\">const<\/span> <span class=\"hljs-keyword\">override<\/span> <\/span>{ <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">0<\/span>; } };<br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">DangerousCargo<\/span> : <span class=\"hljs-keyword\">public<\/span> Cargo { <span class=\"hljs-keyword\">public<\/span>: <span class=\"hljs-function\"><span class=\"hljs-type\">int<\/span> <span class=\"hljs-title\">riskLevel<\/span><span class=\"hljs-params\">()<\/span> <span class=\"hljs-type\">const<\/span> <span class=\"hljs-keyword\">override<\/span> <\/span>{ <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">5<\/span>; } };<br \/>\n\u200b<br \/>\n<span class=\"hljs-comment\">\/\/ \u2500\u2500 \u7ef4\u5ea6\u4e8c&#xff1a;\u53d1\u52a8\u673a\u7c7b\u578b \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500<\/span><br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">Engine<\/span> {<br \/>\n<span class=\"hljs-keyword\">public<\/span>:<br \/>\n \u00a0 \u00a0<span class=\"hljs-keyword\">virtual<\/span> ~<span class=\"hljs-built_in\">Engine<\/span>() &#061; <span class=\"hljs-keyword\">default<\/span>;<br \/>\n \u00a0 \u00a0<span class=\"hljs-function\"><span class=\"hljs-keyword\">virtual<\/span> <span class=\"hljs-type\">void<\/span> <span class=\"hljs-title\">start<\/span><span class=\"hljs-params\">()<\/span> <\/span>&#061; <span class=\"hljs-number\">0<\/span>;<br \/>\n};<br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">SteamEngine<\/span> \u00a0 : <span class=\"hljs-keyword\">public<\/span> Engine { <span class=\"hljs-keyword\">public<\/span>: <span class=\"hljs-function\"><span class=\"hljs-type\">void<\/span> <span class=\"hljs-title\">start<\/span><span class=\"hljs-params\">()<\/span> <span class=\"hljs-keyword\">override<\/span> <\/span>{ <span class=\"hljs-comment\">\/* &#8230; *\/<\/span> } };<br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">NuclearEngine<\/span> : <span class=\"hljs-keyword\">public<\/span> Engine { <span class=\"hljs-keyword\">public<\/span>: <span class=\"hljs-function\"><span class=\"hljs-type\">void<\/span> <span class=\"hljs-title\">start<\/span><span class=\"hljs-params\">()<\/span> <span class=\"hljs-keyword\">override<\/span> <\/span>{ <span class=\"hljs-comment\">\/* &#8230; *\/<\/span> } };<br \/>\n\u200b<br \/>\n<span class=\"hljs-comment\">\/\/ \u2500\u2500 \u7ef4\u5ea6\u4e09&#xff1a;\u5bfc\u822a\u7c7b\u578b \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500<\/span><br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">Navigation<\/span> {<br \/>\n<span class=\"hljs-keyword\">public<\/span>:<br \/>\n \u00a0 \u00a0<span class=\"hljs-keyword\">virtual<\/span> ~<span class=\"hljs-built_in\">Navigation<\/span>() &#061; <span class=\"hljs-keyword\">default<\/span>;<br \/>\n \u00a0 \u00a0<span class=\"hljs-function\"><span class=\"hljs-keyword\">virtual<\/span> <span class=\"hljs-type\">void<\/span> <span class=\"hljs-title\">navigate<\/span><span class=\"hljs-params\">()<\/span> <\/span>&#061; <span class=\"hljs-number\">0<\/span>;<br \/>\n};<br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">GPS<\/span> \u00a0 \u00a0 \u00a0 : <span class=\"hljs-keyword\">public<\/span> Navigation { <span class=\"hljs-keyword\">public<\/span>: <span class=\"hljs-function\"><span class=\"hljs-type\">void<\/span> <span class=\"hljs-title\">navigate<\/span><span class=\"hljs-params\">()<\/span> <span class=\"hljs-keyword\">override<\/span> <\/span>{ <span class=\"hljs-comment\">\/* &#8230; *\/<\/span> } };<br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">StarChart<\/span> : <span class=\"hljs-keyword\">public<\/span> Navigation { <span class=\"hljs-keyword\">public<\/span>: <span class=\"hljs-function\"><span class=\"hljs-type\">void<\/span> <span class=\"hljs-title\">navigate<\/span><span class=\"hljs-params\">()<\/span> <span class=\"hljs-keyword\">override<\/span> <\/span>{ <span class=\"hljs-comment\">\/* &#8230; *\/<\/span> } };<br \/>\n\u200b<br \/>\n<span class=\"hljs-comment\">\/\/ \u2500\u2500 \u7ec4\u5408&#xff1a;\u4e00\u4e2a\u7c7b\u5199\u4e00\u6b21&#xff0c;\u7ef4\u5ea6\u968f\u4fbf\u62fc \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500<\/span><br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">Ship<\/span> {<br \/>\n \u00a0 \u00a0Cargo* \u00a0 \u00a0 \u00a0m_cargo; \u00a0 \u00a0 \u00a0<span class=\"hljs-comment\">\/\/ Has-A<\/span><br \/>\n \u00a0 \u00a0Engine* \u00a0 \u00a0 m_engine; \u00a0 \u00a0 <span class=\"hljs-comment\">\/\/ Has-A<\/span><br \/>\n \u00a0 \u00a0Navigation* m_nav; \u00a0 \u00a0 \u00a0 \u00a0<span class=\"hljs-comment\">\/\/ Has-A<\/span><br \/>\n<span class=\"hljs-keyword\">public<\/span>:<br \/>\n \u00a0 \u00a0<span class=\"hljs-built_in\">Ship<\/span>(Cargo* cargo, Engine* engine, Navigation* nav)<br \/>\n \u00a0 \u00a0 \u00a0  : <span class=\"hljs-built_in\">m_cargo<\/span>(cargo), <span class=\"hljs-built_in\">m_engine<\/span>(engine), <span class=\"hljs-built_in\">m_nav<\/span>(nav) {}<br \/>\n\u200b<br \/>\n \u00a0 \u00a0<span class=\"hljs-function\"><span class=\"hljs-type\">void<\/span> <span class=\"hljs-title\">sail<\/span><span class=\"hljs-params\">()<\/span> <\/span>{<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0m_engine-&gt;<span class=\"hljs-built_in\">start<\/span>();<br \/>\n \u00a0 \u00a0 \u00a0 \u00a0m_nav-&gt;<span class=\"hljs-built_in\">navigate<\/span>();<br \/>\n \u00a0  }<br \/>\n\u200b<br \/>\n \u00a0 \u00a0<span class=\"hljs-comment\">\/\/ \u2605 \u8fd0\u884c\u65f6\u52a8\u6001\u66ff\u6362 \u2014\u2014 \u7ee7\u627f\u505a\u4e0d\u5230\u8fd9\u4e00\u70b9<\/span><br \/>\n \u00a0 \u00a0<span class=\"hljs-function\"><span class=\"hljs-type\">void<\/span> <span class=\"hljs-title\">replaceEngine<\/span><span class=\"hljs-params\">(Engine* engine)<\/span> <\/span>{ m_engine &#061; engine; }<br \/>\n}; <\/p>\n<p>\u6548\u679c\u5bf9\u6bd4&#xff1a;<\/p>\n<table>\n<tr>\u7ee7\u627f\u65b9\u6848\u7ec4\u5408\u65b9\u6848<\/tr>\n<tbody>\n<tr>\n<td>\u7c7b\u6570\u91cf<\/td>\n<td>2 \u00d7 2 \u00d7 2 &#061; 8 \u4e2a&#xff08;\u8fd8\u4f1a\u7ee7\u7eed\u7ffb\u500d&#xff09;<\/td>\n<td>2 &#043; 2 &#043; 2 &#043; 1 &#061; 7 \u4e2a&#xff08;\u7ebf\u6027\u589e\u957f&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u52a0\u4e00\u4e2a\u7ef4\u5ea6&#xff08;\u8239\u4f53\u6750\u8d28&#xff0c;2 \u79cd&#xff09;<\/td>\n<td>8 \u2192 16 \u4e2a\u7c7b<\/td>\n<td>&#043;2 \u4e2a\u7c7b<\/td>\n<\/tr>\n<tr>\n<td>\u8fd0\u884c\u65f6\u6362\u53d1\u52a8\u673a<\/td>\n<td>\u274c \u505a\u4e0d\u5230<\/td>\n<td>\u2705 replaceEngine()<\/td>\n<\/tr>\n<tr>\n<td>\u91cd\u590d\u4ee3\u7801<\/td>\n<td>\u5927\u91cf&#xff08;\u4e00\u4e2a\u5b50\u7c7b\u4e0d\u80fd\u540c\u65f6\u7ee7\u627f\u4e24\u4e2a\u7c7b&#xff09;<\/td>\n<td>\u65e0<\/td>\n<\/tr>\n<tr>\n<td>\u590d\u7528\u7c92\u5ea6<\/td>\n<td>\u5168\u90e8\u7ee7\u627f<\/td>\n<td>\u6309\u9700\u8c03\u7528<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&#x1f4ac; \u4eba\u8bdd\u7248\u603b\u7ed3 \u7ee7\u627f\u590d\u7528\u662f&#034;\u590d\u5236\u4e00\u4efd\u5e76\u7279\u5316&#034;&#xff0c;\u7ec4\u5408\u590d\u7528\u662f&#034;\u6301\u6709\u5e76\u59d4\u6258&#034;\u3002 \u5f53\u53d8\u5316\u7684\u7ef4\u5ea6\u8d85\u8fc7\u4e00\u4e2a\u65f6&#xff0c;\u7ee7\u627f\u4f1a\u8ba9\u7c7b\u6570\u91cf\u6307\u6570\u7206\u70b8&#xff1b;\u7ec4\u5408\u8ba9\u7c7b\u6570\u91cf\u7ebf\u6027\u589e\u957f\u3002<\/p>\n<h5 id=\"C%2B%2B%20%E8%A7%86%E8%A7%92%E7%9A%84%E8%A1%A5%E5%85%85%E6%94%B6%E7%9B%8A\">C&#043;&#043; \u89c6\u89d2\u7684\u8865\u5145\u6536\u76ca<\/h5>\n<p> <span class=\"hljs-comment\">\/\/ Ship.h \u2014\u2014 \u53ea\u524d\u5411\u58f0\u660e&#xff0c;\u4e0d include \u5177\u4f53\u5b9e\u73b0<\/span><br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">Engine<\/span>; \u00a0 \u00a0 \u00a0 \u00a0<span class=\"hljs-comment\">\/\/ \u524d\u5411\u58f0\u660e<\/span><br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">Navigation<\/span>;<br \/>\n\u200b<br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">Ship<\/span> {<br \/>\n \u00a0 \u00a0Engine* \u00a0 \u00a0 m_engine;<br \/>\n \u00a0 \u00a0Navigation* m_nav;<br \/>\n<span class=\"hljs-keyword\">public<\/span>:<br \/>\n \u00a0 \u00a0<span class=\"hljs-function\"><span class=\"hljs-type\">void<\/span> <span class=\"hljs-title\">sail<\/span><span class=\"hljs-params\">()<\/span><\/span>;<br \/>\n};<br \/>\n\u200b<br \/>\n<span class=\"hljs-comment\">\/\/ Ship.cpp \u2014\u2014 \u53ea\u6709\u8fd9\u91cc\u624d include \u5177\u4f53\u5934\u6587\u4ef6<\/span><br \/>\n<span class=\"hljs-meta\">#<span class=\"hljs-keyword\">include<\/span> <span class=\"hljs-string\">&#034;SteamEngine.h&#034;<\/span><\/span><br \/>\n<span class=\"hljs-meta\">#<span class=\"hljs-keyword\">include<\/span> <span class=\"hljs-string\">&#034;GPS.h&#034;<\/span><\/span> <\/p>\n<p>\u597d\u5904&#xff1a; \u6362\u4e00\u4e2a\u53d1\u52a8\u673a\u5b9e\u73b0&#xff0c;Ship.h \u4e0d\u53d8&#xff0c;\u6240\u6709 #include &#034;Ship.h&#034; \u7684\u6587\u4ef6\u90fd\u4e0d\u9700\u8981\u91cd\u65b0\u7f16\u8bd1\u3002<\/p>\n<p>\u8fd9\u5728 C&#043;&#043; \u91cc\u662f\u5b9e\u6253\u5b9e\u7684\u6784\u5efa\u65f6\u95f4\u6536\u76ca \u2014\u2014 \u800c\u7ee7\u627f\u65b9\u6848\u91cc&#xff0c;Ship.h \u4e00\u6539&#xff0c;\u5168\u9879\u76ee\u91cd\u7f16\u8bd1\u3002<\/p>\n<p>\u26a0\ufe0f \u4f46\u4e5f\u522b\u8d70\u6781\u7aef \u4e0d\u662f\u6240\u6709\u7ee7\u627f\u90fd\u8be5\u6362\u6210\u7ec4\u5408&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u771f\u6b63\u6ee1\u8db3 IS-A \u4e14\u6ee1\u8db3 LSP \u7684\u573a\u666f&#xff0c;\u7ee7\u627f\u662f\u66f4\u7b80\u6d01\u3001\u66f4\u7b26\u5408\u76f4\u89c9\u7684\u8868\u8fbe&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u9700\u8981\u591a\u6001\u66ff\u6362&#xff08;\u9762\u5411\u62bd\u8c61\u7f16\u7a0b&#xff09;\u65f6&#xff0c;\u7ee7\u627f&#xff08;\u6216\u63a5\u53e3\u5b9e\u73b0&#xff09;\u4ecd\u7136\u662f\u5fc5\u9700\u7684\u3002<\/p>\n<\/li>\n<li>\n<p>\u5224\u65ad\u53e3\u8bc0&#xff1a;\u5148\u7528\u7ec4\u5408&#xff0c;\u5b9e\u5728\u4e0d\u5408\u9002\u518d\u7ee7\u627f&#xff1b;\u7528\u7ee7\u627f&#xff0c;\u5c31\u5fc5\u987b\u5b88 LSP\u3002<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h2 id=\"1.5%20%E8%BF%AA%E7%B1%B3%E7%89%B9%E6%B3%95%E5%88%99%20LOD\" style=\"background-color:transparent\">1.5 \u8fea\u7c73\u7279\u6cd5\u5219 LOD<\/h2>\n<p>\u8fea\u7c73\u7279\u6cd5\u5219&#xff08;Law Of Demeter, LOD&#xff09;\u6765\u81ea\u4e8e 1987 \u5e74\u7f8e\u56fd\u4e1c\u5317\u5927\u5b66&#xff08;Northeastern University&#xff09;\u4e00\u4e2a\u540d\u4e3a &#034;Demeter&#034; \u7684\u7814\u7a76\u9879\u76ee\u3002<\/p>\n<p>\u8fea\u7c73\u7279\u6cd5\u5219\u53c8\u79f0\u4e3a\u6700\u5c11\u77e5\u8bc6\u539f\u5219&#xff08;Least Knowledge Principle, LKP&#xff09;&#xff0c;\u4e5f\u5c31\u662f\u8bf4&#xff0c;\u4e00\u4e2a\u5bf9\u8c61\u5e94\u5f53\u5bf9\u5176\u4ed6\u5bf9\u8c61\u6709\u5c3d\u53ef\u80fd\u5c11\u7684\u4e86\u89e3\u3002<\/p>\n<p>\u5176\u5b9a\u4e49\u6709\u5982\u4e0b\u51e0\u79cd\u5f62\u5f0f&#xff1a;<\/p>\n<p>Each unit should have only limited knowledge about other units: only units &#034;closely&#034; related to the current unit.<\/p>\n<p>\u6bcf\u4e00\u4e2a\u8f6f\u4ef6\u5355\u4f4d\u5bf9\u5176\u4ed6\u5355\u4f4d\u5c3d\u53ef\u80fd\u5c11\u7684\u4e86\u89e3&#xff0c;\u800c\u4e14\u5c40\u9650\u4e8e\u54ea\u4e9b\u4e0e\u672c\u5355\u4f4d\u5bc6\u5207\u76f8\u5173\u7684\u8f6f\u4ef6\u3002<\/p>\n<p>Each unit should only talk to its friends; don&#039;t talk to strangers.<\/p>\n<p>\u6bcf\u4e2a\u5355\u4f4d\u5e94\u8be5\u53ea\u548c\u5b83\u7684\u670b\u53cb\u4eec\u901a\u4fe1&#xff0c;\u4e0d\u4e0e\u964c\u751f\u4eba\u901a\u4fe1\u3002<\/p>\n<p>Talk only to your immediate friends.<\/p>\n<p>\u53ea\u548c\u76f4\u63a5\u7684\u670b\u53cb\u901a\u4fe1\u3002<\/p>\n<p>&#x1f4ac; \u4eba\u8bdd\u7248 \u4e00\u53e5\u8bdd&#xff1a;&#034;\u522b\u8d8a\u7ea7\u627e\u4eba\u529e\u4e8b\u3002&#034;<\/p>\n<p>\u4f60\u8981\u529e\u4ec0\u4e48\u4e8b&#xff0c;\u5c31\u8ddf\u4f60\u7684\u76f4\u63a5\u5bf9\u63a5\u4eba\u8bf4&#xff0c;\u8ba9\u4ed6\u53bb\u5b89\u6392\u3002 \u4e0d\u8981&#034;\u901a\u8fc7\u670b\u53cb\u8ba4\u8bc6\u4e86\u4ed6\u670b\u53cb&#xff0c;\u7136\u540e\u7ed5\u8fc7\u670b\u53cb\u76f4\u63a5\u627e\u4eba\u5bb6&#034;\u3002<\/p>\n<p>\u56e0\u4e3a\u4e00\u65e6\u4f60\u8d8a\u7ea7&#xff0c;\u4e2d\u95f4\u90a3\u4e2a\u4eba\u6362\u4e86\u3001\u6539\u4e86\u3001\u751a\u81f3\u4e0d\u5b58\u5728\u4e86&#xff0c;\u4f60\u90fd\u8981\u8ddf\u7740\u6539 \u2014\u2014 \u4f60\u88ab\u8feb\u77e5\u9053\u4e86\u592a\u591a\u4e0d\u8be5\u77e5\u9053\u7684\u4e1c\u897f\u3002<\/p>\n<h4 id=\"1.5.1%20%E6%B3%95%E5%88%99%E7%9B%AE%E6%A0%87\">1.5.1 \u6cd5\u5219\u76ee\u6807<\/h4>\n<p>\u5982\u679c\u4e00\u4e2a\u7cfb\u7edf\u7b26\u5408\u8fea\u7c73\u7279\u6cd5\u5219&#xff0c;\u90a3\u4e48\u5f53\u5176\u4e2d\u67d0\u4e00\u4e2a\u6a21\u5757\u53d1\u751f\u4fee\u6539\u65f6&#xff0c;\u5c31\u4f1a\u5c3d\u91cf\u5c11\u5730\u5f71\u54cd\u5176\u4ed6\u6a21\u5757&#xff0c;\u6269\u5c55\u4f1a\u76f8\u5bf9\u5bb9\u6613\u3002<\/p>\n<p>\u8fd9\u662f\u5bf9\u8f6f\u4ef6\u5b9e\u4f53\u4e4b\u95f4\u901a\u4fe1\u7684\u9650\u5236&#xff1a;\u8fea\u7c73\u7279\u6cd5\u5219\u8981\u6c42\u9650\u5236\u8f6f\u4ef6\u5b9e\u4f53\u4e4b\u95f4\u901a\u4fe1\u7684\u5bbd\u5ea6\u548c\u6df1\u5ea6\u3002<\/p>\n<p>\u8fea\u7c73\u7279\u6cd5\u5219\u53ef\u964d\u4f4e\u7cfb\u7edf\u7684\u8026\u5408\u5ea6&#xff0c;\u4f7f\u7c7b\u4e0e\u7c7b\u4e4b\u95f4\u4fdd\u6301\u677e\u6563\u7684\u8026\u5408\u5173\u7cfb\u3002<\/p>\n<p>&#x1f4ac; \u4eba\u8bdd\u7248<\/p>\n<ul>\n<li>\n<p>\u9650\u5236\u5bbd\u5ea6&#xff1a;\u4e0d\u8ddf\u65e0\u5173\u7684\u4eba\u8bf4\u8bdd&#xff08;\u53ea\u8ddf\u8be5\u8bf4\u7684\u5bf9\u8c61\u8bf4&#xff09;<\/p>\n<\/li>\n<li>\n<p>\u9650\u5236\u6df1\u5ea6&#xff1a;\u4e0d\u8ddf&#034;\u670b\u53cb\u7684\u670b\u53cb&#034;\u8bf4\u8bdd&#xff08;\u4e0d\u94fe\u5f0f\u8c03\u7528 a-&gt;getB()-&gt;getC()-&gt;doThing()&#xff09;<\/p>\n<\/li>\n<\/ul>\n<h4 id=\"1.5.2%20%E7%8B%AD%E4%B9%89%E7%9A%84%E8%BF%AA%E7%B1%B3%E7%89%B9%E6%B3%95%E5%88%99\">1.5.2 \u72ed\u4e49\u7684\u8fea\u7c73\u7279\u6cd5\u5219<\/h4>\n<p>\u8fea\u7c73\u7279\u6cd5\u5219\u8981\u6c42\u6211\u4eec\u5728\u8bbe\u8ba1\u7cfb\u7edf\u65f6&#xff0c;\u5e94\u8be5\u5c3d\u91cf\u51cf\u5c11\u5bf9\u8c61\u4e4b\u95f4\u7684\u4ea4\u4e92\u3002\u5982\u679c\u4e24\u4e2a\u5bf9\u8c61\u4e4b\u95f4\u4e0d\u5fc5\u5f7c\u6b64\u76f4\u63a5\u901a\u4fe1&#xff0c;\u90a3\u4e48\u8fd9\u4e24\u4e2a\u5bf9\u8c61\u5c31\u4e0d\u5e94\u5f53\u53d1\u751f\u4efb\u4f55\u76f4\u63a5\u7684\u76f8\u4e92\u4f5c\u7528\u3002<\/p>\n<p>\u5982\u679c\u5176\u4e2d\u7684\u4e00\u4e2a\u5bf9\u8c61\u9700\u8981\u8c03\u7528\u53e6\u4e00\u4e2a\u5bf9\u8c61\u7684\u67d0\u4e00\u4e2a\u65b9\u6cd5\u7684\u8bdd&#xff0c;\u53ef\u4ee5\u901a\u8fc7\u7b2c\u4e09\u8005\u8f6c\u53d1\u8fd9\u4e2a\u8c03\u7528\u3002<\/p>\n<p>\u7b80\u8a00\u4e4b&#xff0c;\u5c31\u662f\u901a\u8fc7\u5f15\u5165\u4e00\u4e2a\u5408\u7406\u7684\u7b2c\u4e09\u8005\u6765\u964d\u4f4e\u73b0\u6709\u5bf9\u8c61\u4e4b\u95f4\u7684\u8026\u5408\u5ea6\u3002<\/p>\n<p>\u5bf9\u4e8e\u4e00\u4e2a\u5bf9\u8c61&#xff0c;\u5176\u76f4\u63a5\u670b\u53cb\u5305\u62ec\u4ee5\u4e0b\u51e0\u7c7b&#xff1a;<\/p>\n<li>\n<p>\u5f53\u524d\u5bf9\u8c61\u672c\u8eab&#xff08;this&#xff09;&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u4ee5\u53c2\u6570\u5f62\u5f0f\u4f20\u5165\u5230\u5f53\u524d\u5bf9\u8c61\u65b9\u6cd5\u4e2d\u7684\u5bf9\u8c61&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u5f53\u524d\u5bf9\u8c61\u7684\u6210\u5458\u5bf9\u8c61&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u5f53\u524d\u5bf9\u8c61\u7684\u6210\u5458\u5bf9\u8c61\u662f\u4e00\u4e2a\u96c6\u5408&#xff0c;\u90a3\u4e48\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u4e5f\u90fd\u662f\u670b\u53cb&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u5f53\u524d\u5bf9\u8c61\u6240\u521b\u5efa\u7684\u5bf9\u8c61\u3002<\/p>\n<\/li>\n<p>\u4e0d\u662f\u76f4\u63a5\u670b\u53cb\u7684\u5178\u578b\u60c5\u51b5&#xff1a;\u53ea\u51fa\u73b0\u5728\u65b9\u6cd5\u4f53\u5185\u90e8\u7684\u7c7b\u5bf9\u8c61 \u2014\u2014 \u6bd4\u5982\u901a\u8fc7 a-&gt;getB()-&gt;getC() \u62ff\u5230\u7684 c&#xff0c;\u5b83\u4e0d\u662f\u4f60\u7684\u670b\u53cb&#xff0c;\u5b83\u662f&#034;\u4f60\u670b\u53cb\u7684\u670b\u53cb&#034;\u3002<\/p>\n<p>&#x1f4ac; \u4eba\u8bdd\u7248&#xff08;\u670b\u53cb\u6e05\u5355&#xff09; \u4f60\u7684&#034;\u76f4\u63a5\u670b\u53cb&#034;\u5c31\u662f&#xff1a;\u4f60\u81ea\u5df1\u3001\u4f60\u7684\u6210\u5458\u3001\u522b\u4eba\u9012\u7ed9\u4f60\u7684\u3001\u4f60\u4eb2\u624b\u9020\u7684\u3001\u4ee5\u53ca\u4f60\u6210\u5458\u96c6\u5408\u91cc\u7684\u5143\u7d20\u3002 \u9664\u6b64\u4e4b\u5916\u90fd\u662f\u964c\u751f\u4eba \u2014\u2014 \u5c24\u5176\u662f\u90a3\u79cd&#034;\u4e00\u5c42\u5957\u4e00\u5c42\u53d6\u51fa\u6765\u7684\u5bf9\u8c61&#034;\u3002<\/p>\n<p>\u4ee3\u7801\u5473\u9053&#xff1a;\u5982\u679c\u4f60\u5199\u51fa order-&gt;getCustomer()-&gt;getAddress()-&gt;getCity()&#xff0c;\u8fd9\u5c31\u662f\u5178\u578b\u7684&#034;\u548c\u964c\u751f\u4eba\u8bf4\u8bdd&#034;&#xff08;\u4e5f\u53eb\u706b\u8f66\u6b8b\u9ab8 Train Wreck&#xff09;\u3002<\/p>\n<p>\u26a0\ufe0f \u7f3a\u70b9&#xff1a;<\/p>\n<p>\u9075\u5faa\u7c7b\u4e4b\u95f4\u7684\u8fea\u7c73\u7279\u6cd5\u5219\u4f1a\u662f\u4e00\u4e2a\u7cfb\u7edf\u7684\u5c40\u90e8\u8bbe\u8ba1\u7b80\u5316&#xff0c;\u56e0\u4e3a\u6bcf\u4e00\u4e2a\u5c40\u90e8\u90fd\u4e0d\u4f1a\u548c\u8fdc\u8ddd\u79bb\u7684\u5bf9\u8c61\u6709\u76f4\u63a5\u7684\u5173\u8054\u3002 \u4f46\u662f&#xff0c;\u8fd9\u4e5f\u4f1a\u9020\u6210\u7cfb\u7edf\u7684\u4e0d\u540c\u6a21\u5757\u4e4b\u95f4\u7684\u901a\u4fe1\u6548\u7387\u964d\u4f4e&#xff0c;\u4e5f\u4f1a\u4f7f\u7cfb\u7edf\u7684\u4e0d\u540c\u6a21\u5757\u4e4b\u95f4\u4e0d\u5bb9\u6613\u534f\u8c03\u3002<\/p>\n<p>&#x1f4ac; \u4eba\u8bdd\u7248&#xff08;\u4ee3\u4ef7&#xff09; \u4e3a\u4e86&#034;\u4e0d\u8d8a\u7ea7&#034;&#xff0c;\u7cfb\u7edf\u91cc\u4f1a\u591a\u51fa\u4e00\u5806\u53ea\u4e3a\u8f6c\u53d1\u800c\u5b58\u5728\u7684\u65b9\u6cd5&#xff08;\u7eaf\u4e2d\u8f6c\u4ee3\u7801&#xff09;\u3002 \u8fd9\u662f\u7528\u4e00\u70b9&#034;\u5570\u55e6&#034;\u6362&#034;\u4f4e\u8026\u5408&#034; \u2014\u2014 \u5927\u591a\u6570\u65f6\u5019\u8fd9\u7b14\u4e70\u5356\u5212\u7b97&#xff0c;\u4f46\u4e0d\u8981\u4e3a\u4e86\u5b83\u628a\u7cfb\u7edf\u5207\u5f97\u4e03\u96f6\u516b\u843d\u3002<\/p>\n<h4 id=\"1.5.3%20%E5%B9%BF%E4%B9%89%E7%9A%84%E8%BF%AA%E7%B1%B3%E7%89%B9%E6%B3%95%E5%88%99\" style=\"background-color:transparent\">1.5.3 \u5e7f\u4e49\u7684\u8fea\u7c73\u7279\u6cd5\u5219<\/h4>\n<ul>\n<li>\n<p>\u5728\u7c7b\u7684\u7ed3\u6784\u8bbe\u8ba1\u4e0a&#xff0c;\u6bcf\u4e00\u4e2a\u7c7b\u90fd\u5e94\u5f53\u5c3d\u91cf\u964d\u4f4e\u5176\u6210\u5458\u53d8\u91cf\u548c\u6210\u5458\u51fd\u6570\u7684\u8bbf\u95ee\u6743\u9650&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u5728\u7c7b\u7684\u8bbe\u8ba1\u4e0a&#xff0c;\u53ea\u8981\u6709\u53ef\u80fd&#xff0c;\u4e00\u4e2a\u7c7b\u578b\u5e94\u5f53\u8bbe\u8ba1\u6210\u4e0d\u53d8\u7c7b&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u5728\u5bf9\u5176\u4ed6\u7c7b\u7684\u5f15\u7528\u4e0a&#xff0c;\u4e00\u4e2a\u5bf9\u8c61\u5bf9\u5176\u4ed6\u5bf9\u8c61\u7684\u5f15\u7528\u5e94\u5f53\u964d\u5230\u6700\u4f4e\u3002<\/p>\n<\/li>\n<\/ul>\n<p>&#x1f4ac; \u4eba\u8bdd\u7248 \u5e7f\u4e49\u7684 LOD \u5176\u5b9e\u5c31\u662f\u4e00\u53e5\u8bdd&#xff1a;&#034;\u80fd\u4e0d\u7ed9\u522b\u4eba\u770b\u7684&#xff0c;\u5c31\u522b\u7ed9\u522b\u4eba\u770b\u3002&#034;<\/p>\n<ul>\n<li>\n<p>private \u4f18\u5148&#xff0c;protected \u6b21\u4e4b&#xff0c;public \u662f\u6700\u540e\u9009\u62e9<\/p>\n<\/li>\n<li>\n<p>\u80fd\u505a\u6210\u4e0d\u53ef\u53d8\u7684&#xff08;\u6240\u6709\u6210\u5458 const \/ \u6784\u9020\u540e\u4e0d\u53ef\u6539&#xff09;&#xff0c;\u5c31\u505a\u6210\u4e0d\u53ef\u53d8 \u2014\u2014 \u522b\u4eba\u6539\u4e0d\u4e86&#xff0c;\u5c31\u4e0d\u53ef\u80fd\u56e0\u4e3a\u522b\u4eba\u6539\u800c\u51fa bug<\/p>\n<\/li>\n<li>\n<p>\u6301\u6709\u522b\u4eba\u7684\u5f15\u7528\u8d8a\u5c11&#xff0c;\u88ab\u7275\u8fde\u7684\u53ef\u80fd\u8d8a\u5c0f<\/p>\n<\/li>\n<\/ul>\n<h4 id=\"1.5.4%20%E6%A1%88%E4%BE%8B\">1.5.4 \u6848\u4f8b<\/h4>\n<h5 id=\"1.5.4.1%20%E4%B8%8E%E7%9B%B4%E6%8E%A5%E6%9C%8B%E5%8F%8B%E9%80%9A%E4%BF%A1\" style=\"background-color:transparent\">1.5.4.1 \u4e0e\u76f4\u63a5\u670b\u53cb\u901a\u4fe1<\/h5>\n<p>\u95ee\u9898\u573a\u666f&#xff08;\u539f\u8bb2\u4e49\u914d\u56fe&#xff0c;\u6b64\u5904\u7528\u6587\u5b57\u8fd8\u539f&#xff09;&#xff1a;<\/p>\n<p style=\"text-align:center\"><span tabindex=\"-1\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260919124912-6aae84c8cdfc6.png\" \/><span title=\"\u70b9\u51fb\u5e76\u62d6\u62fd\u4ee5\u6539\u53d8\u5c3a\u5bf8\">\u200b<\/span><\/span><\/p>\n<p>\u5728 Teacher \u4e2d\u5bf9\u975e\u76f4\u63a5\u7684\u670b\u53cb Student \u8fdb\u884c\u4e86\u901a\u4fe1&#xff0c;\u8fdd\u80cc\u4e86\u8fea\u7c73\u7279\u6cd5\u5219\u3002<\/p>\n<p> <span class=\"hljs-comment\">\/\/ \u274c \u8fdd\u53cd LOD&#xff1a;Teacher \u76f4\u63a5\u548c&#034;\u964c\u751f\u4eba&#034; Student \u901a\u4fe1<\/span><br \/>\n<span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">Student<\/span> {<br \/>\n<span class=\"hljs-keyword\">public<\/span>:<br \/>\n    <span class=\"hljs-function\"><span class=\"hljs-type\">void<\/span> <span class=\"hljs-title\">study<\/span><span class=\"hljs-params\">()<\/span> <\/span>{ <span class=\"hljs-comment\">\/* \u5b66\u4e60 *\/<\/span> }<br \/>\n};<\/p>\n<p><span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">Moniter<\/span> {                                  <span class=\"hljs-comment\">\/\/ Teacher \u7684\u6210\u5458\u5bf9\u8c61&#xff08;\u670b\u53cb&#xff09;<\/span><br \/>\n    std::vector&lt;Student*&gt; m_students;<br \/>\n<span class=\"hljs-keyword\">public<\/span>:<br \/>\n    <span class=\"hljs-function\">std::vector&lt;Student*&gt;&amp; <span class=\"hljs-title\">getStudents<\/span><span class=\"hljs-params\">()<\/span> <\/span>{ <span class=\"hljs-keyword\">return<\/span> m_students; }<br \/>\n};<\/p>\n<p><span class=\"hljs-keyword\">class<\/span> <span class=\"class_ hljs-title\">Teacher<\/span> {<br \/>\n    Moniter* m_moniter;                             <span class=\"hljs-comment\">\/\/ \u2705 \u662f\u670b\u53cb<\/span><br \/>\n<span class=\"hljs-keyword\">public<\/span>:<br \/>\n    <span class=\"hljs-function\"><span class=\"hljs-type\">void<\/span> <span class=\"hljs-title\">teach<\/span><span class=\"hljs-params\">()<\/span> <\/span>{<br \/>\n        <span class=\"hljs-comment\">\/\/ \u26a0\ufe0f Student \u662f&#034;\u670b\u53cb\u7684\u670b\u53cb&#034; \u2014\u2014 \u5bf9 Teacher \u6765\u8bf4\u662f\u964c\u751f\u4eba<\/span><br \/>\n        <span class=\"hljs-keyword\">for<\/span> (Student* s : m_moniter-&gt;<span class=\"hljs-built_in\">getStudents<\/span>()) {<br \/>\n            s-&gt;<span class=\"hljs-built_in\">study<\/span>();                            <span class=\"hljs-comment\">\/\/ &#x1f6a8; \u8d8a\u7ea7\u8c03\u7528<\/span><br \/>\n        }<br \/>\n    }<br \/>\n}; <\/p>\n<p>\u4e3a\u4ec0\u4e48\u8981\u91cd\u6784&#xff1f;<\/p>\n<ul>\n<li>\n<p>Moniter \u7684\u5185\u90e8\u7ed3\u6784&#xff08;\u7528 vector \u8fd8\u662f list\u3001\u8981\u4e0d\u8981\u5206\u9875\u3001\u6709\u6ca1\u6709\u865a\u62df\u5b66\u751f&#xff09;\u4e00\u65e6\u53d8\u5316&#xff0c;Teacher \u5c31\u5f97\u8ddf\u7740\u6539&#xff1b;<\/p>\n<\/li>\n<li>\n<p>Teacher \u88ab\u8feb\u77e5\u9053\u4e86 Moniter \u7684\u5b9e\u73b0\u7ec6\u8282\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u91cd\u6784\u540e&#xff1a;\u8ba9 Moniter \u8f6c\u53d1<\/p>\n<p>\/\/ \u2705 \u7b26\u5408 LOD<br \/>\nclass Moniter {<br \/>\n    std::vector&lt;Student*&gt; m_students;<br \/>\npublic:<br \/>\n    \/\/ \u628a&#034;\u8ba9\u5b66\u751f\u5b66\u4e60&#034;\u8fd9\u4ef6\u4e8b\u4ea4\u7ed9 Moniter \u81ea\u5df1\u5b8c\u6210 \u2014\u2014 \u5b83\u624d\u662f Student \u7684\u76f4\u63a5\u670b\u53cb<br \/>\n    void studyAll() {<br \/>\n        for (Student* s : m_students) {<br \/>\n            s-&gt;study();<br \/>\n        }<br \/>\n    }<br \/>\n};<\/p>\n<p>class Teacher {<br \/>\n    Moniter* m_moniter;<br \/>\npublic:<br \/>\n    void teach() {<br \/>\n        m_moniter-&gt;studyAll();        \/\/ \u2705 \u53ea\u548c\u76f4\u63a5\u670b\u53cb\u8bf4\u4e00\u53e5\u8bdd<br \/>\n    }<br \/>\n}; <\/p>\n<p>&#x1f4ac; \u4eba\u8bdd\u7248 \u8001\u5e08\u8981\u8ba9\u5b66\u751f\u5b66\u4e60&#xff0c;\u4e0d\u9700\u8981\u81ea\u5df1\u4e00\u4e2a\u4e2a\u70b9\u540d\u53bb\u53eb \u2014\u2014 \u8ddf\u73ed\u957f\u8bf4&#034;\u8ba9\u5927\u5bb6\u81ea\u4e60&#034;&#xff0c;\u73ed\u957f\u53bb\u5b89\u6392\u3002 \u8001\u5e08\u4ece\u6b64\u4e0d\u9700\u8981\u77e5\u9053\u73ed\u4e0a\u6709\u51e0\u4e2a\u5b66\u751f\u3001\u7528\u4ec0\u4e48\u6570\u636e\u7ed3\u6784\u5b58\u7684\u3002<\/p>\n<h5 id=\"1.5.4.2%20%E5%87%8F%E5%B0%91%E5%AF%B9%E6%9C%8B%E5%8F%8B%E7%9A%84%E4%BA%86%E8%A7%A3\">1.5.4.2 \u51cf\u5c11\u5bf9\u670b\u53cb\u7684\u4e86\u89e3<\/h5>\n<p>\u5728\u4e00\u4e2a\u7c7b\u4e2d&#xff0c;\u5c31\u662f\u5c3d\u91cf\u51cf\u5c11\u4e00\u4e2a\u7c7b\u5bf9\u5916\u66b4\u9732\u7684\u65b9\u6cd5\u3002<\/p>\n<p>\u95ee\u9898\u573a\u666f&#xff1a;<\/p>\n<p>Human \u9700\u8981\u8c03\u7528\u8bb8\u591a WashingMachine \u63d0\u4f9b\u7684\u65b9\u6cd5&#xff0c;<\/p>\n<p>\u540c\u65f6\u8fd8\u6709\u53ef\u80fd\u5bfc\u81f4\u8c03\u7528\u987a\u5e8f\u9519\u8bef&#xff0c;\u8fdd\u80cc\u4e86\u8fea\u7c73\u7279\u6cd5\u5219\u3002<\/p>\n<p>\/\/ \u274c \u8fdd\u53cd LOD&#xff1a;Human \u9700\u8981\u77e5\u9053\u6d17\u8863\u673a\u7684\u5168\u90e8\u64cd\u4f5c\u6b65\u9aa4<br \/>\nclass WashingMachine {<br \/>\npublic:<br \/>\n    void openLid();<br \/>\n    void addWater();<br \/>\n    void addClothes(const Clothes&amp; c);<br \/>\n    void wash();<br \/>\n    void drain();<br \/>\n    void spin();<br \/>\n    void closeLid();<br \/>\n};<\/p>\n<p>class Human {<br \/>\n    WashingMachine* m_machine;<br \/>\npublic:<br \/>\n    void doLaundry(const Clothes&amp; c) {<br \/>\n        m_machine-&gt;openLid();<br \/>\n        m_machine-&gt;addWater();<br \/>\n        m_machine-&gt;addClothes(c);      \/\/ \u26a0\ufe0f \u5fd8\u4e86\u8fd9\u6b65&#xff1f;\u987a\u5e8f\u641e\u9519\u4e86&#xff1f;\u8863\u670d\u767d\u6d17<br \/>\n        m_machine-&gt;wash();<br \/>\n        m_machine-&gt;drain();<br \/>\n        m_machine-&gt;spin();<br \/>\n        m_machine-&gt;closeLid();<br \/>\n    }<br \/>\n}; <\/p>\n<p>\u95ee\u9898&#xff1a;<\/p>\n<li>\n<p>Human \u88ab\u8feb\u77e5\u9053\u6d17\u8863\u673a\u7684\u5168\u90e8 7 \u4e2a\u5185\u90e8\u6b65\u9aa4&#xff08;\u77e5\u9053\u7684\u592a\u591a&#xff09;&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u8c03\u7528\u987a\u5e8f\u9519\u4e86\u4f1a\u51fa\u95ee\u9898&#xff08;&#034;\u5148\u653e\u8863\u670d\u518d\u52a0\u6c34&#034; vs &#034;\u5148\u52a0\u6c34\u518d\u653e\u8863\u670d&#034;&#xff09;&#xff1b;<\/p>\n<\/li>\n<li>\n<p>\u6d17\u8863\u673a\u5185\u90e8\u6d41\u7a0b\u4e00\u53d8&#xff08;\u6bd4\u5982\u65b0\u589e&#034;\u6f02\u6d17&#034;&#xff09;&#xff0c;\u6240\u6709\u8c03\u7528\u65b9\u90fd\u8981\u6539\u3002<\/p>\n<\/li>\n<p>\u91cd\u6784\u540e&#xff1a;\u53ea\u66b4\u9732\u4e00\u4e2a&#034;\u4e00\u952e\u6d17\u8863&#034;<\/p>\n<p>\/\/ \u2705 \u7b26\u5408 LOD&#xff1a;\u53ea\u66b4\u9732\u5ba2\u6237\u7aef\u771f\u6b63\u9700\u8981\u7684\u884c\u4e3a<br \/>\nclass WashingMachine {<br \/>\nprivate:<br \/>\n    void openLid();<br \/>\n    void addWater();<br \/>\n    void addClothes(const Clothes&amp; c);<br \/>\n    void wash();<br \/>\n    void drain();<br \/>\n    void spin();<br \/>\n    void closeLid();<\/p>\n<p>public:<br \/>\n    \/\/ \u2605 \u552f\u4e00\u5bf9\u5916\u7684\u65b9\u6cd5&#xff1a;\u628a\u590d\u6742\u6d41\u7a0b\u5c01\u88c5\u8d77\u6765&#xff0c;\u987a\u5e8f\u7531\u673a\u5668\u81ea\u5df1\u4fdd\u8bc1<br \/>\n    void washAll(const std::vector&lt;Clothes&gt;&amp; clothes) {<br \/>\n        openLid();<br \/>\n        addWater();<br \/>\n        for (const auto&amp; c : clothes) addClothes(c);<br \/>\n        wash();<br \/>\n        drain();<br \/>\n        spin();<br \/>\n        closeLid();<br \/>\n    }<br \/>\n};<\/p>\n<p>class Human {<br \/>\n    WashingMachine* m_machine;<br \/>\npublic:<br \/>\n    void doLaundry(const std::vector&lt;Clothes&gt;&amp; clothes) {<br \/>\n        m_machine-&gt;washAll(clothes);   \/\/ \u2705 \u4e00\u53e5\u8bdd\u641e\u5b9a&#xff0c;\u4e0e\u76f4\u63a5\u670b\u53cb\u901a\u4fe1<br \/>\n    }<br \/>\n}; <\/p>\n<p>&#x1f4ac; \u4eba\u8bdd\u7248 \u4f60\u7528\u5fae\u6ce2\u7089&#xff0c;\u53ea\u9700\u8981\u6309&#034;\u5f00\u59cb&#034;&#xff0c;\u4e0d\u9700\u8981\u77e5\u9053\u9ad8\u538b\u53d8\u538b\u5668\u600e\u4e48\u5de5\u4f5c\u3002 \u6d17\u8863\u673a\u4e5f\u4e00\u6837 \u2014\u2014 \u4eba\u53ea\u9700\u8981\u4f1a\u8bf4&#034;\u6d17\u8fd9\u4e00\u7b50\u8863\u670d&#034;&#xff0c;\u4e0d\u9700\u8981\u80cc\u4e0b\u6765\u6d17\u8863\u673a\u7684 7 \u6b65\u64cd\u4f5c\u624b\u518c\u3002<\/p>\n<p>\u8fd9\u4e5f\u662f\u95e8\u9762\u6a21\u5f0f&#xff08;Facade&#xff09;\u3001\u5916\u89c2\u6a21\u5f0f\u7684\u601d\u60f3\u6765\u6e90\u3002<\/p>\n<h4 id=\"1.5.5%20%E4%BD%BF%E7%94%A8%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9\">1.5.5 \u4f7f\u7528\u6ce8\u610f\u4e8b\u9879<\/h4>\n<ul>\n<li>\n<p>\u5728\u7c7b\u7684\u5212\u5206\u4e0a&#xff0c;\u5e94\u5f53\u521b\u5efa\u5f31\u8026\u5408\u7684\u7c7b&#xff0c;\u7c7b\u4e0e\u7c7b\u4e4b\u95f4\u7684\u8026\u5408\u8d8a\u5f31&#xff0c;\u5c31\u8d8a\u6709\u5229\u4e8e\u5b9e\u73b0\u53ef\u590d\u7528\u7684\u76ee\u6807\u3002<\/p>\n<\/li>\n<li>\n<p>\u5728\u7c7b\u7684\u7ed3\u6784\u8bbe\u8ba1\u4e0a&#xff0c;\u6bcf\u4e2a\u7c7b\u90fd\u5e94\u8be5\u964d\u4f4e\u6210\u5458\u7684\u8bbf\u95ee\u6743\u9650\u3002<\/p>\n<\/li>\n<li>\n<p>\u5728\u7c7b\u7684\u8bbe\u8ba1\u4e0a&#xff0c;\u53ea\u8981\u6709\u53ef\u80fd&#xff0c;\u4e00\u4e2a\u7c7b\u5e94\u5f53\u8bbe\u8ba1\u6210\u4e0d\u53d8\u7684\u7c7b\u3002<\/p>\n<\/li>\n<li>\n<p>\u5728\u5bf9\u5176\u4ed6\u7c7b\u7684\u5f15\u7528\u4e0a&#xff0c;\u4e00\u4e2a\u5bf9\u8c61\u5bf9\u5176\u4ed6\u7c7b\u7684\u5bf9\u8c61\u7684\u5f15\u7528\u5e94\u8be5\u964d\u5230\u6700\u4f4e\u3002<\/p>\n<\/li>\n<li>\n<p>\u5c3d\u91cf\u9650\u5236\u5c40\u90e8\u53d8\u91cf\u7684\u6709\u6548\u8303\u56f4&#xff0c;\u964d\u4f4e\u7c7b\u7684\u8bbf\u95ee\u6743\u9650\u3002<\/p>\n<\/li>\n<\/ul>\n<p>&#x1f4ac; \u4eba\u8bdd\u7248&#xff08;\u4e00\u6761\u5b9e\u7528\u68c0\u67e5\u6e05\u5355&#xff09;<\/p>\n<table>\n<tr>\u68c0\u67e5\u9879\u8be5\u95ee\u81ea\u5df1\u7684\u95ee\u9898<\/tr>\n<tbody>\n<tr>\n<td>\u6210\u5458\u8bbf\u95ee\u6743\u9650<\/td>\n<td>\u8fd9\u4e2a public \u771f\u7684\u662f\u5916\u9762\u9700\u8981\u7684\u5417&#xff1f;private \u884c\u4e0d\u884c&#xff1f;<\/td>\n<\/tr>\n<tr>\n<td>\u94fe\u5f0f\u8c03\u7528<\/td>\n<td>\u6709\u6ca1\u6709 a-&gt;getB()-&gt;getC()-&gt;doX()&#xff1f;<\/td>\n<\/tr>\n<tr>\n<td>\u6210\u5458\u5f15\u7528\u6570\u91cf<\/td>\n<td>\u8fd9\u4e2a\u7c7b\u6301\u6709\u4e86\u51e0\u4e2a\u522b\u7684\u4e1c\u897f&#xff1f;\u80fd\u51cf\u5c11\u5417&#xff1f;<\/td>\n<\/tr>\n<tr>\n<td>\u5c40\u90e8\u53d8\u91cf\u4f5c\u7528\u57df<\/td>\n<td>\u8fd9\u4e2a\u53d8\u91cf\u771f\u7684\u9700\u8981\u6d3b\u8fd9\u4e48\u4e45\u5417&#xff1f;<\/td>\n<\/tr>\n<tr>\n<td>\u4e0d\u53ef\u53d8\u6027<\/td>\n<td>\u8fd9\u4e2a\u7c7b\u7684\u5b57\u6bb5\u80fd\u6539\u6210 const \u5417&#xff1f;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h2 id=\"1.6%20%E5%B0%8F%E7%BB%93\" style=\"background-color:transparent\">1.6 \u5c0f\u7ed3<\/h2>\n<h4 id=\"%E9%9D%A2%E8%AF%95%2F%E5%A4%8D%E4%B9%A0%E5%85%AB%E9%97%AE\" style=\"background-color:transparent\">\u9762\u8bd5\/\u590d\u4e60\u516b\u95ee<\/h4>\n<p>1. \u9762\u5411\u5bf9\u8c61\u8bbe\u8ba1\u539f\u5219\u7684\u76ee\u6807\u662f\u4ec0\u4e48&#xff1f;<\/p>\n<p>\u63d0\u9ad8\u8f6f\u4ef6\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u590d\u7528\u6027&#xff0c;\u5b9e\u73b0\u53ef\u7ef4\u62a4\u6027\u7684\u590d\u7528\u3002<\/p>\n<p>2. \u4ec0\u4e48\u662f\u5355\u4e00\u804c\u8d23\u539f\u5219&#xff1f;<\/p>\n<p>\u4e00\u4e2a\u5bf9\u8c61\u5e94\u8be5\u53ea\u5305\u542b\u5355\u4e00\u7684\u804c\u8d23&#xff0c;\u5e76\u4e14\u8be5\u804c\u8d23\u88ab\u5b8c\u7f8e\u5730\u5c01\u88c5\u5728\u4e00\u4e2a\u7c7b\u4e2d\u3002 &#xff08;\u6362\u4e2a\u8bf4\u6cd5&#xff1a;\u4e00\u4e2a\u7c7b\u5e94\u8be5\u53ea\u6709\u4e00\u4e2a\u5f15\u8d77\u5b83\u53d8\u5316\u7684\u539f\u56e0\u3002&#xff09;<\/p>\n<p>3. \u4ec0\u4e48\u662f\u5f00\u95ed\u539f\u5219&#xff1f;<\/p>\n<p>\u8f6f\u4ef6\u5b9e\u4f53\u5e94\u5bf9\u6269\u5c55\u5f00\u653e&#xff0c;\u800c\u5bf9\u4fee\u6539\u5173\u95ed\u3002<\/p>\n<p>4. \u4ec0\u4e48\u662f\u91cc\u6c0f\u66ff\u6362\u539f\u5219&#xff1f;<\/p>\n<p>\u8f6f\u4ef6\u4e2d\u6240\u6709\u5f15\u7528\u57fa\u7c7b&#xff08;\u7236\u7c7b&#xff09;\u7684\u5730\u65b9&#xff0c;\u5fc5\u987b\u80fd\u900f\u660e\u5730\u4f7f\u7528\u5176\u5b50\u7c7b\u7684\u5bf9\u8c61\u3002<\/p>\n<p>5. \u4ec0\u4e48\u662f\u4f9d\u8d56\u5012\u7f6e\u539f\u5219&#xff1f;<\/p>\n<p>\u9ad8\u5c42\u6a21\u5757\u4e0d\u5e94\u8be5\u4ece\u4f4e\u5c42\u6a21\u5757\u5bfc\u5165\u4efb\u4f55\u4e1c\u897f&#xff0c;\u4e24\u8005\u90fd\u5e94\u8be5\u4f9d\u8d56\u4e8e\u62bd\u8c61\u3002\u62bd\u8c61\u4e0d\u5e94\u8be5\u4f9d\u8d56\u4e8e\u7ec6\u8282&#xff0c;\u7ec6\u8282\u5e94\u5f53\u4f9d\u8d56\u4e8e\u62bd\u8c61\u3002 \u5373&#xff1a;\u9488\u5bf9\u63a5\u53e3\u7f16\u7a0b&#xff0c;\u800c\u4e0d\u662f\u9488\u5bf9\u5b9e\u73b0\u7f16\u7a0b\u3002<\/p>\n<p>6. \u4ec0\u4e48\u662f\u63a5\u53e3\u9694\u79bb\u539f\u5219&#xff1f;<\/p>\n<p>\u5ba2\u6237\u7aef\u4e0d\u5e94\u8be5\u4f9d\u8d56\u54ea\u4e9b\u5b83\u4e0d\u9700\u8981\u7684\u63a5\u53e3\u3002<\/p>\n<p>7. \u4ec0\u4e48\u662f\u7ec4\u5408\u590d\u7528\u539f\u5219&#xff1f;<\/p>\n<p>\u4f18\u5148\u4f7f\u7528\u5bf9\u8c61\u7684\u7ec4\u5408&#xff0c;\u800c\u4e0d\u662f\u4f7f\u7528\u7ee7\u627f\u6765\u8fbe\u5230\u590d\u7528\u7684\u76ee\u7684\u3002<\/p>\n<p>8. \u4ec0\u4e48\u662f\u8fea\u7c73\u7279\u6cd5\u5219&#xff1f;<\/p>\n<p>\u6bcf\u4e00\u4e2a\u8f6f\u4ef6\u5355\u4f4d\u5bf9\u5176\u4ed6\u5355\u4f4d\u5c3d\u53ef\u80fd\u5c11\u7684\u4e86\u89e3&#xff0c;\u800c\u4e14\u5c40\u9650\u4e8e\u54ea\u4e9b\u4e0e\u672c\u5355\u4f4d\u5bc6\u5207\u76f8\u5173\u7684\u8f6f\u4ef6\u3002<\/p>\n<h4 id=\"%E4%B8%80%E5%8F%A5%E8%AF%9D%E8%AE%B0%E5%BF%86%E7%89%88\" style=\"background-color:transparent\">\u4e00\u53e5\u8bdd\u8bb0\u5fc6\u7248<\/h4>\n<table>\n<tr>\u539f\u5219\u4e00\u53e5\u8bdd\u89e3\u51b3\u4ec0\u4e48\u95ee\u9898<\/tr>\n<tbody>\n<tr>\n<td>SRP \u5355\u4e00\u804c\u8d23<\/td>\n<td>\u4e00\u4e2a\u7c7b\u53ea\u5e72\u4e00\u4ef6\u4e8b<\/td>\n<td>\u7c7b\u592a\u80d6&#xff0c;\u6539\u4e00\u5904\u7275\u52a8\u5168\u8eab<\/td>\n<\/tr>\n<tr>\n<td>OCP \u5f00\u95ed<\/td>\n<td>\u52a0\u529f\u80fd\u4e0d\u6539\u8001\u4ee3\u7801<\/td>\n<td>\u6bcf\u6b21\u9700\u6c42\u53d8\u66f4\u90fd\u8981\u52a8\u8001\u4ee3\u7801<\/td>\n<\/tr>\n<tr>\n<td>LSP \u91cc\u6c0f\u66ff\u6362<\/td>\n<td>\u5b50\u7c7b\u5fc5\u987b\u80fd\u66ff\u6362\u7236\u7c7b<\/td>\n<td>\u591a\u6001\u7528\u8d77\u6765\u5fc3\u91cc\u6ca1\u5e95\u3001\u8fd0\u884c\u65f6\u70b8<\/td>\n<\/tr>\n<tr>\n<td>DIP \u4f9d\u8d56\u5012\u7f6e<\/td>\n<td>\u4f9d\u8d56\u62bd\u8c61&#xff0c;\u4e0d\u4f9d\u8d56\u5177\u4f53<\/td>\n<td>\u6362\u4e2a\u5b9e\u73b0\u8981\u91cd\u7f16\u8bd1\u534a\u4e2a\u9879\u76ee<\/td>\n<\/tr>\n<tr>\n<td>ISP \u63a5\u53e3\u9694\u79bb<\/td>\n<td>\u63a5\u53e3\u8981\u5c0f\u800c\u4e13<\/td>\n<td>\u5b9e\u73b0\u63a5\u53e3\u88ab\u8feb\u5199\u4e00\u5806\u7a7a\u65b9\u6cd5<\/td>\n<\/tr>\n<tr>\n<td>CRP \u7ec4\u5408\u590d\u7528<\/td>\n<td>\u4f18\u5148\u7ec4\u5408&#xff0c;\u614e\u7528\u7ee7\u627f<\/td>\n<td>\u7ee7\u627f\u5c42\u6b21\u7206\u70b8&#xff0c;\u7c7b\u6570\u91cf\u6307\u6570\u589e\u957f<\/td>\n<\/tr>\n<tr>\n<td>LOD \u8fea\u7c73\u7279<\/td>\n<td>\u53ea\u548c\u76f4\u63a5\u670b\u53cb\u8bf4\u8bdd<\/td>\n<td>\u8d8a\u7ea7\u8c03\u7528&#xff0c;\u94fe\u5f0f\u8c03\u7528&#xff0c;\u8026\u5408\u592a\u6df1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4 id=\"%E5%8E%9F%E5%88%99%E4%B9%8B%E9%97%B4%E7%9A%84%E5%85%B3%E7%B3%BB\" style=\"background-color:transparent\">\u539f\u5219\u4e4b\u95f4\u7684\u5173\u7cfb<\/h4>\n<p style=\"text-align:center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"413\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260919124913-6aae84c920c5f.png\" width=\"355\" \/><\/p>\n<p>\u6838\u5fc3\u7ed3\u8bba&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5f00\u95ed\u539f\u5219\u662f\u76ee\u6807 \u2014\u2014 \u6240\u6709\u7684\u539f\u5219\u6700\u7ec8\u90fd\u670d\u52a1\u4e8e&#034;\u4e0d\u4fee\u6539\u5c31\u80fd\u6269\u5c55&#034;<\/p>\n<\/li>\n<li>\n<p>\u91cc\u6c0f\u66ff\u6362\u662f\u57fa\u7840 \u2014\u2014 \u6ca1\u6709\u5b83&#xff0c;\u591a\u6001\u5c31\u662f&#034;\u78b0\u8fd0\u6c14&#034;<\/p>\n<\/li>\n<li>\n<p>\u4f9d\u8d56\u5012\u7f6e\u662f\u624b\u6bb5 \u2014\u2014 \u901a\u8fc7\u4f9d\u8d56\u62bd\u8c61\u6765\u8fbe\u6210\u5f00\u95ed<\/p>\n<\/li>\n<li>\n<p>SRP \/ ISP \u662f\u7c92\u5ea6\u63a7\u5236 \u2014\u2014 \u51b3\u5b9a&#034;\u7c7b\u8be5\u591a\u5927&#034;&#034;\u63a5\u53e3\u8be5\u591a\u5927&#034;<\/p>\n<\/li>\n<li>\n<p>LOD \u662f\u8026\u5408\u63a7\u5236 \u2014\u2014 \u51b3\u5b9a&#034;\u5bf9\u8c61\u4e4b\u95f4\u600e\u4e48\u8bf4\u8bdd&#034;<\/p>\n<\/li>\n<li>\n<p>CRP \u662f\u590d\u7528\u7b56\u7565 \u2014\u2014 \u51b3\u5b9a&#034;\u7528\u7ee7\u627f\u8fd8\u662f\u7ec4\u5408\u6765\u590d\u7528&#034;<\/p>\n<\/li>\n<\/ul>\n<p>&#x1f4ac; \u6700\u540e\u7684\u63d0\u9192 \u8fd9\u4e9b\u539f\u5219\u662f&#034;\u6307\u5bfc&#034;&#xff0c;\u4e0d\u662f&#034;\u6cd5\u5f8b&#034;\u3002 \u5b83\u4eec\u4f1a\u4e92\u76f8\u51b2\u7a81&#xff08;\u6bd4\u5982 SRP \u62c6\u5f97\u8d8a\u7ec6&#xff0c;\u7c7b\u6570\u91cf\u8d8a\u591a&#xff1b;LOD \u8d8a\u4e25\u683c&#xff0c;\u8f6c\u53d1\u4ee3\u7801\u8d8a\u591a&#xff09;\u3002 \u771f\u6b63\u7684\u529f\u592b\u5728\u4e8e\u6743\u8861&#xff1a;\u5224\u65ad\u8fd9\u4e2a\u7cfb\u7edf\u91cc&#034;\u4ec0\u4e48\u4f1a\u53d8\u3001\u4ec0\u4e48\u4e0d\u4f1a\u53d8&#034;&#xff0c;\u7136\u540e\u5728\u5bf9\u7684\u5730\u65b9\u7559\u51fa\u6269\u5c55\u70b9&#xff0c;\u5728\u4e0d\u5bf9\u7684\u5730\u65b9\u4fdd\u6301\u7b80\u5355\u3002<\/p>\n<p>\u8fc7\u5ea6\u8bbe\u8ba1\u548c\u8bbe\u8ba1\u4e0d\u8db3&#xff0c;\u90fd\u662f\u5931\u8d25\u3002<\/p>\n<hr \/>\n<h3 id=\"%E9%99%84%E5%BD%95%EF%BC%9A%E4%B8%83%E5%A4%A7%E5%8E%9F%E5%88%99%E9%80%9F%E6%9F%A5%E8%A1%A8%EF%BC%88%E5%A4%8D%E4%B9%A0%E7%94%A8%EF%BC%89\" style=\"background-color:transparent\">\u9644\u5f55&#xff1a;\u4e03\u5927\u539f\u5219\u901f\u67e5\u8868&#xff08;\u590d\u4e60\u7528&#xff09;<\/h3>\n<table>\n<tr>#\u539f\u5219\u82f1\u6587\u7f29\u5199\u5b9a\u4e49&#xff08;\u539f\u6587&#xff09;\u4eba\u8bdd<\/tr>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>\u5355\u4e00\u804c\u8d23\u539f\u5219<\/td>\n<td>Single Responsibility Principle<\/td>\n<td>SRP<\/td>\n<td>\u4e00\u4e2a\u5bf9\u8c61\u5e94\u8be5\u53ea\u5305\u542b\u5355\u4e00\u7684\u804c\u8d23&#xff0c;\u5e76\u4e14\u8be5\u804c\u8d23\u88ab\u5b8c\u7f8e\u5730\u5c01\u88c5\u5728\u4e00\u4e2a\u7c7b\u4e2d<\/td>\n<td>\u4e00\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a&#034;\u6539\u5b83\u7684\u7406\u7531&#034;<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>\u5f00\u95ed\u539f\u5219<\/td>\n<td>Open-Closed Principle<\/td>\n<td>OCP<\/td>\n<td>\u8f6f\u4ef6\u5b9e\u4f53\u5e94\u5bf9\u6269\u5c55\u5f00\u653e&#xff0c;\u800c\u5bf9\u4fee\u6539\u5173\u95ed<\/td>\n<td>\u52a0\u529f\u80fd\u9760&#034;\u65b0\u589e&#034;&#xff0c;\u4e0d\u9760&#034;\u6539\u8001\u7684&#034;<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>\u91cc\u6c0f\u66ff\u6362\u539f\u5219<\/td>\n<td>Liskov Substitution Principle<\/td>\n<td>LSP<\/td>\n<td>\u6240\u6709\u5f15\u7528\u57fa\u7c7b\u7684\u5730\u65b9\u5fc5\u987b\u80fd\u900f\u660e\u5730\u4f7f\u7528\u5176\u5b50\u7c7b\u7684\u5bf9\u8c61<\/td>\n<td>\u5b50\u7c7b\u80fd\u9876\u66ff\u7236\u7c7b&#xff0c;\u4e14\u4e0d\u51fa\u5c94\u5b50<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>\u4f9d\u8d56\u5012\u7f6e\u539f\u5219<\/td>\n<td>Dependence Inversion Principle<\/td>\n<td>DIP<\/td>\n<td>\u9ad8\u5c42\u6a21\u5757\u4e0d\u5e94\u4f9d\u8d56\u4f4e\u5c42\u6a21\u5757&#xff0c;\u4e24\u8005\u90fd\u5e94\u4f9d\u8d56\u62bd\u8c61&#xff1b;\u62bd\u8c61\u4e0d\u5e94\u4f9d\u8d56\u7ec6\u8282&#xff0c;\u7ec6\u8282\u5e94\u4f9d\u8d56\u62bd\u8c61<\/td>\n<td>\u522b new \u5177\u4f53\u7684&#xff0c;\u8ba9\u5916\u9762&#034;\u9001&#034;\u8fdb\u6765<\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>\u63a5\u53e3\u9694\u79bb\u539f\u5219<\/td>\n<td>Interface Segregation Principle<\/td>\n<td>ISP<\/td>\n<td>\u5ba2\u6237\u7aef\u4e0d\u5e94\u8be5\u4f9d\u8d56\u54ea\u4e9b\u5b83\u4e0d\u9700\u8981\u7684\u63a5\u53e3<\/td>\n<td>\u63a5\u53e3\u5c0f\u800c\u4e13&#xff0c;\u522b\u903c\u4eba\u5b9e\u73b0\u7a7a\u65b9\u6cd5<\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>\u7ec4\u5408\u590d\u7528\u539f\u5219<\/td>\n<td>Composite\/Aggregate Reuse Principle<\/td>\n<td>CRP \/ CARP<\/td>\n<td>\u4f18\u5148\u4f7f\u7528\u5bf9\u8c61\u7684\u7ec4\u5408&#xff0c;\u800c\u4e0d\u662f\u4f7f\u7528\u7ee7\u627f\u6765\u8fbe\u5230\u590d\u7528\u7684\u76ee\u7684<\/td>\n<td>\u80fd&#034;\u88c5\u4e00\u4e2a&#034;\u5c31\u522b&#034;\u662f\u4e00\u79cd&#034;<\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>\u8fea\u7c73\u7279\u6cd5\u5219<\/td>\n<td>Law Of Demeter<\/td>\n<td>LOD<\/td>\n<td>\u6bcf\u4e00\u4e2a\u8f6f\u4ef6\u5355\u4f4d\u5bf9\u5176\u4ed6\u5355\u4f4d\u5c3d\u53ef\u80fd\u5c11\u7684\u4e86\u89e3&#xff0c;\u800c\u4e14\u5c40\u9650\u4e8e\u54ea\u4e9b\u4e0e\u672c\u5355\u4f4d\u5bc6\u5207\u76f8\u5173\u7684\u8f6f\u4ef6<\/td>\n<td>\u53ea\u548c\u76f4\u63a5\u670b\u53cb\u8bf4\u8bdd&#xff0c;\u522b\u8d8a\u7ea7<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h2>\u7ed3\u8bed<\/h2>\n<p>\u00a0 \u00a0 \u00a0 \u00a0\u00a0\u6211\u76f8\u4fe1\u4e2d\u95f4\u8fd8\u662f\u6709\u4e00\u4e9b\u96be\u7406\u89e3\u7684\u6982\u5ff5&#xff0c;\u591a\u770b\u51e0\u904d\u5c31\u597d\u4e86&#xff0c;\u8fd9\u4e00\u7bc7\u53ea\u662f\u6211\u4eec\u8bbe\u8ba1\u6a21\u5f0f\u7684\u57fa\u7840&#xff0c;\u63a5\u4e0b\u6765\u6211\u5c06\u5e26\u7740\u5927\u5bb6\u628a\u5e38\u89c1\u768423\u79cd\u8bbe\u8ba1\u6a21\u5f0f\u4e00\u4e2a\u4e00\u4e2a\u8d70\u4e00\u904d\u3002<\/p>\n<p><span style=\"color:#38d8f0\">\u6211\u662fYYYing&#xff0c;\u540e\u9762\u8fd8\u6709\u66f4\u7cbe\u5f69\u7684\u5185\u5bb9&#xff0c;\u5e0c\u671b\u5404\u4f4d\u80fd\u591a\u591a\u5173\u6ce8\u652f\u6301\u4e00\u4e0b\u4e3b\u5305\u3002<\/span><\/p>\n<p><span style=\"color:#98c091\">\u65e0\u9650\u8fdb\u6b65&#xff0c;\u6211\u4eec\u4e0b\u6b21\u518d\u89c1&#xff01;<\/span><\/p>\n<hr \/>\n<h2 id=\"---%E2%AD%90%EF%B8%8F%E5%B0%81%E9%9D%A2%E8%87%AA%E5%8F%96%E2%AD%90%EF%B8%8F---\" style=\"text-align:center\">&#8212;\u2b50\ufe0f\u5c01\u9762\u81ea\u53d6\u2b50\ufe0f&#8212;<\/h2>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260919124913-6aae84c960821.jpg\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u2b50\ufe0f\u5728\u8fd9\u4e2a\u6000\u7591\u7684\u5e74\u4ee3&#xff0c;\u6211\u4eec\u4f9d\u7136\u9700\u8981\u4fe1\u4ef0\u3002<br \/>\n\u4e2a\u4eba\u4e3b\u9875\u00a0&#xff1a;YYYing.<br \/>\n\u2b50\ufe0f\u8bbe\u8ba1\u6a21\u5f0f\u7cfb\u5217\u4e13\u680f&#xff1a;\u8bbe\u8ba1\u6a21\u5f0f\u7cfb\u5217<br \/>\n\u7cfb\u5217\u4e0b\u671f\u5185\u5bb9&#xff1a;\u6682\u65e0 \u76ee\u5f55<br \/>\n\u524d\u8a00&#xff1a;<br \/>\n1.1 \u4f18\u79c0\u8bbe\u8ba1\u7684\u7279\u5f81<br \/>\n1.1.1 \u4ee3\u7801\u590d\u7528&#xff08;Code Reuse&#xff09;<br \/>\n1.1.2 \u6269\u5c55\u6027&#xff08;Extensibility&#xff09;<br \/>\n1.1.3 \u53ef\u7ef4\u62a4\u6027\u4e0e\u53ef\u590d\u7528\u6027&#xff1a;\u4e00\u5bf9\u5b6a\u751f\u76ee\u6807<br \/>\n1.2 \u9762\u5411\u5bf9\u8c61<\/p>\n","protected":false},"author":2,"featured_media":107558,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[11815,12232,1054,55,292,2874],"topic":[],"class_list":["post-107567","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-11815","tag-12232","tag-c-c","tag-c","tag-292","tag-2874"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010\u8bbe\u8ba1\u6a21\u5f0f\u7cfb\u5217 (\u4e00) \u3011\u4e07\u5b57\u89e3\u6790\uff1a\u4e00\u6587\u5e26\u4f60\u641e\u5b9a\u9762\u5411\u5bf9\u8c61\u8bbe\u8ba1\u539f\u5219 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/107567.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010\u8bbe\u8ba1\u6a21\u5f0f\u7cfb\u5217 (\u4e00) \u3011\u4e07\u5b57\u89e3\u6790\uff1a\u4e00\u6587\u5e26\u4f60\u641e\u5b9a\u9762\u5411\u5bf9\u8c61\u8bbe\u8ba1\u539f\u5219 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u2b50\ufe0f\u5728\u8fd9\u4e2a\u6000\u7591\u7684\u5e74\u4ee3&#xff0c;\u6211\u4eec\u4f9d\u7136\u9700\u8981\u4fe1\u4ef0\u3002 \u4e2a\u4eba\u4e3b\u9875\u00a0&#xff1a;YYYing. \u2b50\ufe0f\u8bbe\u8ba1\u6a21\u5f0f\u7cfb\u5217\u4e13\u680f&#xff1a;\u8bbe\u8ba1\u6a21\u5f0f\u7cfb\u5217 \u7cfb\u5217\u4e0b\u671f\u5185\u5bb9&#xff1a;\u6682\u65e0 \u76ee\u5f55 \u524d\u8a00&#xff1a; 1.1 \u4f18\u79c0\u8bbe\u8ba1\u7684\u7279\u5f81 1.1.1 \u4ee3\u7801\u590d\u7528&#xff08;Code Reuse&#xff09; 1.1.2 \u6269\u5c55\u6027&#xff08;Extensibility&#xff09; 1.1.3 \u53ef\u7ef4\u62a4\u6027\u4e0e\u53ef\u590d\u7528\u6027&#xff1a;\u4e00\u5bf9\u5b6a\u751f\u76ee\u6807 1.2 \u9762\u5411\u5bf9\u8c61\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/107567.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-19T12:49:16+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260919124911-6aae84c79f3ad.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"17 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/107567.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/107567.html\",\"name\":\"\u3010\u8bbe\u8ba1\u6a21\u5f0f\u7cfb\u5217 (\u4e00) \u3011\u4e07\u5b57\u89e3\u6790\uff1a\u4e00\u6587\u5e26\u4f60\u641e\u5b9a\u9762\u5411\u5bf9\u8c61\u8bbe\u8ba1\u539f\u5219 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-09-19T12:49:16+00:00\",\"dateModified\":\"2026-09-19T12:49:16+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/107567.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/107567.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/107567.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010\u8bbe\u8ba1\u6a21\u5f0f\u7cfb\u5217 (\u4e00) \u3011\u4e07\u5b57\u89e3\u6790\uff1a\u4e00\u6587\u5e26\u4f60\u641e\u5b9a\u9762\u5411\u5bf9\u8c61\u8bbe\u8ba1\u539f\u5219\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u3010\u8bbe\u8ba1\u6a21\u5f0f\u7cfb\u5217 (\u4e00) \u3011\u4e07\u5b57\u89e3\u6790\uff1a\u4e00\u6587\u5e26\u4f60\u641e\u5b9a\u9762\u5411\u5bf9\u8c61\u8bbe\u8ba1\u539f\u5219 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/107567.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010\u8bbe\u8ba1\u6a21\u5f0f\u7cfb\u5217 (\u4e00) \u3011\u4e07\u5b57\u89e3\u6790\uff1a\u4e00\u6587\u5e26\u4f60\u641e\u5b9a\u9762\u5411\u5bf9\u8c61\u8bbe\u8ba1\u539f\u5219 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u2b50\ufe0f\u5728\u8fd9\u4e2a\u6000\u7591\u7684\u5e74\u4ee3&#xff0c;\u6211\u4eec\u4f9d\u7136\u9700\u8981\u4fe1\u4ef0\u3002 \u4e2a\u4eba\u4e3b\u9875\u00a0&#xff1a;YYYing. \u2b50\ufe0f\u8bbe\u8ba1\u6a21\u5f0f\u7cfb\u5217\u4e13\u680f&#xff1a;\u8bbe\u8ba1\u6a21\u5f0f\u7cfb\u5217 \u7cfb\u5217\u4e0b\u671f\u5185\u5bb9&#xff1a;\u6682\u65e0 \u76ee\u5f55 \u524d\u8a00&#xff1a; 1.1 \u4f18\u79c0\u8bbe\u8ba1\u7684\u7279\u5f81 1.1.1 \u4ee3\u7801\u590d\u7528&#xff08;Code Reuse&#xff09; 1.1.2 \u6269\u5c55\u6027&#xff08;Extensibility&#xff09; 1.1.3 \u53ef\u7ef4\u62a4\u6027\u4e0e\u53ef\u590d\u7528\u6027&#xff1a;\u4e00\u5bf9\u5b6a\u751f\u76ee\u6807 1.2 \u9762\u5411\u5bf9\u8c61","og_url":"https:\/\/www.wsisp.com\/helps\/107567.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-09-19T12:49:16+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260919124911-6aae84c79f3ad.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"17 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/107567.html","url":"https:\/\/www.wsisp.com\/helps\/107567.html","name":"\u3010\u8bbe\u8ba1\u6a21\u5f0f\u7cfb\u5217 (\u4e00) \u3011\u4e07\u5b57\u89e3\u6790\uff1a\u4e00\u6587\u5e26\u4f60\u641e\u5b9a\u9762\u5411\u5bf9\u8c61\u8bbe\u8ba1\u539f\u5219 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-09-19T12:49:16+00:00","dateModified":"2026-09-19T12:49:16+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/107567.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/107567.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/107567.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010\u8bbe\u8ba1\u6a21\u5f0f\u7cfb\u5217 (\u4e00) \u3011\u4e07\u5b57\u89e3\u6790\uff1a\u4e00\u6587\u5e26\u4f60\u641e\u5b9a\u9762\u5411\u5bf9\u8c61\u8bbe\u8ba1\u539f\u5219"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/107567","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=107567"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/107567\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/107558"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=107567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=107567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=107567"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=107567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}