{"id":203825,"date":"2025-05-22T09:38:58","date_gmt":"2025-05-22T01:38:58","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203825\/"},"modified":"2025-05-22T09:38:58","modified_gmt":"2025-05-22T01:38:58","slug":"%e7%90%86%e8%a7%a3%e8%bd%af%e4%bb%b6%e8%ae%be%e8%ae%a1%e4%b8%ad%e7%9a%84-solid-%e5%8e%9f%e5%88%99","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203825\/","title":{"rendered":"\u7406\u89e3\u8f6f\u4ef6\u8bbe\u8ba1\u4e2d\u7684 SOLID \u539f\u5219"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u7406\u89e3\u8f6f\u4ef6\u8bbe\u8ba1\u4e2d\u7684 SOLID \u539f\u5219<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u5728\u6587\u7ae0\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929\u5c31\u6574\u7406\u5206\u4eab\u300a\u7406\u89e3\u8f6f\u4ef6\u8bbe\u8ba1\u4e2d\u7684 SOLID \u539f\u5219\u300b\uff0c\u804a\u804a\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241126\/17326192056745abc5422d6.jpg\" class=\"aligncenter\"><\/p>\n<p>solid \u539f\u5219\u662f\u4e00\u7ec4\u6307\u5bfc\u539f\u5219\uff0c\u53ef\u5e2e\u52a9\u8f6f\u4ef6\u5f00\u53d1\u4eba\u5458\u8bbe\u8ba1\u5065\u58ee\u3001\u53ef\u6269\u5c55\u4e14\u53ef\u7ef4\u62a4\u7684\u7cfb\u7edf\u3002\u8fd9\u4e9b\u539f\u5219\u7531 robert c. martin\uff08bob \u53d4\u53d4\uff09\u63d0\u51fa\uff0c\u5bf9\u4e8e\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u521b\u5efa\u7075\u6d3b\u4e14\u53ef\u91cd\u7528\u7684\u4ee3\u7801\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u7814\u7a76\u6bcf\u4e2a solid \u539f\u5219\uff0c\u89e3\u91ca\u5176\u76ee\u7684\uff0c\u5e76\u63d0\u4f9b java \u793a\u4f8b\u6765\u6f14\u793a\u5176\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<h2> 1.\u5355\u4e00\u804c\u8d23\u539f\u5219\uff08srp\uff09 <\/h2>\n<p>\u5b9a\u4e49\uff1a\u4e00\u4e2a\u7c7b\u5e94\u8be5\u53ea\u6709\u4e00\u4e2a\u6539\u53d8\u7684\u7406\u7531\u3002\u8fd9\u610f\u5473\u7740\u4e00\u4e2a\u7c7b\u5e94\u8be5\u53ea\u6709\u4e00\u9879\u5de5\u4f5c\u6216\u804c\u8d23\u3002<\/p>\n<h3> \u4e3a\u4ec0\u4e48\u5efa\u8bae\u96f6\u552e\u4ef7\u5f88\u91cd\u8981 <\/h3>\n<p>\u5f53\u4e00\u4e2a\u7c7b\u5177\u6709\u591a\u4e2a\u804c\u8d23\u65f6\uff0c\u5bf9\u4e00\u9879\u804c\u8d23\u7684\u66f4\u6539\u53ef\u80fd\u4f1a\u5f71\u54cd\u6216\u7834\u574f\u4ee3\u7801\u7684\u5176\u4ed6\u90e8\u5206\u3002\u901a\u8fc7\u9075\u5b88 srp\uff0c\u6211\u4eec\u786e\u4fdd\u4e86\u66f4\u597d\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u6d4b\u8bd5\u6027\u3002<\/p>\n<h3> \u4f8b\u5b50 <\/h3>\n<pre>\/\/ violating srp: a class that handles both user authentication and database operations.\nclass usermanager {\n    public void authenticateuser(string username, string password) {\n        \/\/ authentication logic\n    }\n\n    public void saveusertodatabase(user user) {\n        \/\/ database logic\n    }\n}\n\n\/\/ following srp: separate responsibilities into distinct classes.\nclass authservice {\n    public void authenticateuser(string username, string password) {\n        \/\/ authentication logic\n    }\n}\n\nclass userrepository {\n    public void saveusertodatabase(user user) {\n        \/\/ database logic\n    }\n}\n<\/pre>\n<p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0cauthservice \u5904\u7406\u8eab\u4efd\u9a8c\u8bc1\uff0cuserrepository \u7ba1\u7406\u6570\u636e\u5e93\u64cd\u4f5c\u3002\u6bcf\u4e2a\u7c7b\u90fd\u6709\u4e00\u4e2a\u804c\u8d23\uff0c\u4f7f\u4ee3\u7801\u66f4\u5e72\u51c0\u3001\u66f4\u6a21\u5757\u5316\u3002<\/p>\n<h2> 2. \u5f00\u95ed\u539f\u7406\uff08ocp\uff09 <\/h2>\n<p>\u5b9a\u4e49\uff1a\u7c7b\u5e94\u8be5\u5bf9\u6269\u5c55\u5f00\u653e\uff0c\u4f46\u5bf9\u4fee\u6539\u5173\u95ed\u3002\u8fd9\u610f\u5473\u7740\u60a8\u5e94\u8be5\u80fd\u591f\u5728\u4e0d\u66f4\u6539\u73b0\u6709\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\u6dfb\u52a0\u65b0\u529f\u80fd\u3002<\/p>\n<h3> \u4e3a\u4ec0\u4e48 ocp \u5f88\u91cd\u8981 <\/h3>\n<p>\u5f53\u60a8\u4fee\u6539\u73b0\u6709\u4ee3\u7801\u65f6\uff0c\u60a8\u6709\u5f15\u5165\u9519\u8bef\u7684\u98ce\u9669\u3002 ocp \u901a\u8fc7\u7ee7\u627f\u6216\u7ec4\u5408\u6765\u4fc3\u8fdb\u529f\u80fd\u6269\u5c55\uff0c\u800c\u4e0d\u662f\u6539\u53d8\u539f\u59cb\u5b9e\u73b0\u3002<\/p>\n<h3> \u4f8b\u5b50 <\/h3>\n<pre>\/\/ violating ocp: adding a new discount type requires modifying the existing code.\nclass discountcalculator {\n    public double calculatediscount(string discounttype, double amount) {\n        if (\"newyear\".equals(discounttype)) {\n            return amount * 0.10;\n        } else if (\"blackfriday\".equals(discounttype)) {\n            return amount * 0.20;\n        }\n        return 0;\n    }\n}\n\n\/\/ following ocp: use polymorphism to add new discount types without changing existing code.\ninterface discount {\n    double apply(double amount);\n}\n\nclass newyeardiscount implements discount {\n    public double apply(double amount) {\n        return amount * 0.10;\n    }\n}\n\nclass blackfridaydiscount implements discount {\n    public double apply(double amount) {\n        return amount * 0.20;\n    }\n}\n\nclass discountcalculator {\n    public double calculatediscount(discount discount, double amount) {\n        return discount.apply(amount);\n    }\n}\n<\/pre>\n<p>\u73b0\u5728\uff0c\u6dfb\u52a0\u65b0\u7684\u6298\u6263\u7c7b\u578b\u53ea\u9700\u521b\u5efa\u4e00\u4e2a\u5b9e\u73b0 discount \u63a5\u53e3\u7684\u65b0\u7c7b\u3002<\/p>\n<h2> 3.\u91cc\u6c0f\u66ff\u6362\u539f\u7406\uff08lsp\uff09 <\/h2>\n<p>\u5b9a\u4e49\uff1a\u5b50\u7c7b\u578b\u5fc5\u987b\u53ef\u4ee5\u66ff\u6362\u5176\u57fa\u672c\u7c7b\u578b\uff0c\u800c\u4e0d\u6539\u53d8\u7a0b\u5e8f\u7684\u6b63\u786e\u6027\u3002<\/p>\n<h3> \u4e3a\u4ec0\u4e48 lsp \u5f88\u91cd\u8981 <\/h3>\n<p>\u4f7f\u7528\u591a\u6001\u6027\u65f6\uff0c\u8fdd\u53cd lsp \u53ef\u80fd\u4f1a\u5bfc\u81f4\u610f\u5916\u7684\u884c\u4e3a\u548c\u9519\u8bef\u3002\u6d3e\u751f\u7c7b\u5fc5\u987b\u9075\u5b88\u5176\u57fa\u7c7b\u5b9a\u4e49\u7684\u5951\u7ea6\u3002<\/p>\n<h3> \u4f8b\u5b50 <\/h3>\n<pre>\/\/ violating lsp: a subclass changes the behavior of the parent class in an unexpected way.\nclass bird {\n    public void fly() {\n        system.out.println(\"flying...\");\n    }\n}\n\nclass penguin extends bird {\n    @override\n    public void fly() {\n        throw new unsupportedoperationexception(\"penguins can't fly!\");\n    }\n}\n\n\/\/ following lsp: refactor the hierarchy to honor substitutability.\nabstract class bird {\n    public abstract void move();\n}\n\nclass flyingbird extends bird {\n    public void move() {\n        system.out.println(\"flying...\");\n    }\n}\n\nclass penguin extends bird {\n    public void move() {\n        system.out.println(\"swimming...\");\n    }\n}\n<\/pre>\n<p>\u901a\u8fc7\u91cd\u65b0\u8bbe\u8ba1\u5c42\u6b21\u7ed3\u6784\uff0cflyingbird \u548c penguin \u5728\u66ff\u6362 bird \u65f6\u90fd\u80fd\u6b63\u786e\u8fd0\u884c\u3002<\/p>\n<h2> 4. \u63a5\u53e3\u9694\u79bb\u539f\u5219\uff08isp\uff09 <\/h2>\n<p>\u5b9a\u4e49\uff1a\u4e0d\u5e94\u5f3a\u8feb\u5ba2\u6237\u7aef\u5b9e\u73b0\u4ed6\u4eec\u4e0d\u4f7f\u7528\u7684\u63a5\u53e3\u3002\u76f8\u53cd\uff0c\u521b\u5efa\u66f4\u5c0f\u3001\u66f4\u5177\u4f53\u7684\u754c\u9762\u3002<\/p>\n<h3> \u4e3a\u4ec0\u4e48 isp \u5f88\u91cd\u8981 <\/h3>\n<p>\u5927\u578b\u63a5\u53e3\u5f3a\u5236\u5b9e\u73b0\u7c7b\u5305\u542b\u5b83\u4eec\u4e0d\u9700\u8981\u7684\u65b9\u6cd5\u3002\u8fd9\u4f1a\u5bfc\u81f4\u4ee3\u7801\u81c3\u80bf\u548c\u4e0d\u5fc5\u8981\u7684\u4f9d\u8d56\u5173\u7cfb\u3002<\/p>\n<h3> \u4f8b\u5b50 <\/h3>\n<pre>\/\/ violating isp: a large interface with unrelated methods.\ninterface worker {\n    void work();\n    void eat();\n}\n\nclass robot implements worker {\n    public void work() {\n        system.out.println(\"working...\");\n    }\n\n    public void eat() {\n        \/\/ robots don't eat, but they're forced to implement this method.\n        throw new unsupportedoperationexception(\"robots don't eat!\");\n    }\n}\n\n\/\/ following isp: split the interface into smaller, focused interfaces.\ninterface workable {\n    void work();\n}\n\ninterface eatable {\n    void eat();\n}\n\nclass robot implements workable {\n    public void work() {\n        system.out.println(\"working...\");\n    }\n}\n\nclass human implements workable, eatable {\n    public void work() {\n        system.out.println(\"working...\");\n    }\n\n    public void eat() {\n        system.out.println(\"eating...\");\n    }\n}\n<\/pre>\n<p>\u73b0\u5728\uff0crobot \u53ea\u5b9e\u73b0\u4e86 workable \u63a5\u53e3\uff0c\u907f\u514d\u4e86\u4e0d\u5fc5\u8981\u7684\u65b9\u6cd5\u3002<\/p>\n<h2> 5. \u4f9d\u8d56\u5012\u7f6e\u539f\u5219\uff08dip\uff09 <\/h2>\n<p>\u5b9a\u4e49\uff1a\u9ad8\u5c42\u6a21\u5757\u4e0d\u5e94\u8be5\u4f9d\u8d56\u4e8e\u4f4e\u5c42\u6a21\u5757\u3002\u4e24\u8005\u90fd\u5e94\u8be5\u4f9d\u8d56\u4e8e\u62bd\u8c61\u3002<\/p>\n<h3> \u4e3a\u4ec0\u4e48 dip \u5f88\u91cd\u8981 <\/h3>\n<p>\u5bf9\u5177\u4f53\u5b9e\u73b0\u7684\u76f4\u63a5\u4f9d\u8d56\u4f7f\u4ee3\u7801\u53d8\u5f97\u50f5\u5316\u4e14\u96be\u4ee5\u6d4b\u8bd5\u3002 dip \u63d0\u5021\u4f7f\u7528\u62bd\u8c61\uff08\u63a5\u53e3\uff09\u6765\u89e3\u8026\u7ec4\u4ef6\u3002<\/p>\n<h3> \u4f8b\u5b50 <\/h3>\n<pre>\/\/ Violating DIP: High-level class depends on a low-level implementation.\nclass MySQLDatabase {\n    public void connect() {\n        System.out.println(\"Connecting to MySQL...\");\n    }\n}\n\nclass UserService {\n    private MySQLDatabase database;\n\n    public UserService() {\n        this.database = new MySQLDatabase(); \/\/ Tight coupling\n    }\n\n    public void performDatabaseOperation() {\n        database.connect();\n    }\n}\n\n\/\/ Following DIP: High-level class depends on an abstraction.\ninterface Database {\n    void connect();\n}\n\nclass MySQLDatabase implements Database {\n    public void connect() {\n        System.out.println(\"Connecting to MySQL...\");\n    }\n}\n\nclass UserService {\n    private Database database;\n\n    public UserService(Database database) {\n        this.database = database; \/\/ Depend on abstraction\n    }\n\n    public void performDatabaseOperation() {\n        database.connect();\n    }\n}\n\n\/\/ Usage\nDatabase db = new MySQLDatabase();\nUserService userService = new UserService(db);\nuserService.performDatabaseOperation();\n<\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u8bbe\u8ba1\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u4ea4\u6362\u6570\u636e\u5e93\u5b9e\u73b0\uff08\u4f8b\u5982 postgresql\u3001mongodb\uff09\uff0c\u800c\u65e0\u9700\u4fee\u6539 userservice \u7c7b\u3002<\/p>\n<h2> \u7ed3\u8bba <\/h2>\n<p>solid \u539f\u5219\u662f\u521b\u5efa\u53ef\u7ef4\u62a4\u3001\u53ef\u6269\u5c55\u4e14\u5f3a\u5927\u7684\u8f6f\u4ef6\u7684\u5f3a\u5927\u5de5\u5177\u3002\u5feb\u901f\u56de\u987e\u4e00\u4e0b\uff1a<\/p>\n<ol>\n<li> srp\uff1a\u4e00\u4e2a\u73ed\u7ea7\uff0c\u4e00\u79cd\u8d23\u4efb\u3002<\/li>\n<li> ocp\uff1a\u5728\u4e0d\u4fee\u6539\u73b0\u6709\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\u6269\u5c55\u529f\u80fd\u3002<\/li>\n<li> lsp\uff1a\u5b50\u7c7b\u578b\u5fc5\u987b\u53ef\u66ff\u6362\u5176\u57fa\u672c\u7c7b\u578b\u3002<\/li>\n<li> isp\uff1a\u66f4\u559c\u6b22\u8f83\u5c0f\u7684\u3001\u96c6\u4e2d\u7684\u754c\u9762\u3002<\/li>\n<li> dip\uff1a\u4f9d\u8d56\u4e8e\u62bd\u8c61\uff0c\u800c\u4e0d\u662f\u5177\u4f53\u5b9e\u73b0\u3002<\/li>\n<\/ol>\n<p>\u901a\u8fc7\u5e94\u7528\u8fd9\u4e9b\u539f\u5219\uff0c\u60a8\u7684\u4ee3\u7801\u5c06\u66f4\u5bb9\u6613\u7406\u89e3\u3001\u6d4b\u8bd5\u5e76\u9002\u5e94\u4e0d\u65ad\u53d8\u5316\u7684\u9700\u6c42\u3002\u4ece\u5c0f\u5904\u5f00\u59cb\uff0c\u6839\u636e\u9700\u8981\u8fdb\u884c\u91cd\u6784\uff0c\u5e76\u9010\u6e10\u5c06\u8fd9\u4e9b\u539f\u5219\u878d\u5165\u5230\u60a8\u7684\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff01<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u7406\u89e3\u8f6f\u4ef6\u8bbe\u8ba1\u4e2d\u7684 SOLID \u539f\u5219\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7406\u89e3\u8f6f\u4ef6\u8bbe\u8ba1\u4e2d\u7684 SOLID \u539f&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-203825","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203825","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=203825"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203825\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}