{"id":5207,"date":"2021-05-26T09:59:36","date_gmt":"2021-05-26T01:59:36","guid":{"rendered":"https:\/\/www.xttblog.com\/?p=5207"},"modified":"2021-05-26T09:59:36","modified_gmt":"2021-05-26T01:59:36","slug":"springboot-%e5%be%ae%e6%9c%8d%e5%8a%a1%e5%9f%ba%e4%ba%8e-ddd-%e7%9a%84%e4%b8%80%e4%ba%9b%e4%bb%a3%e7%a0%81%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=5207","title":{"rendered":"SpringBoot \u5fae\u670d\u52a1\u57fa\u4e8e DDD \u7684\u4e00\u4e9b\u4ee3\u7801\u5b9e\u8df5"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u7b80\u4ecb<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5173\u4e8eDDD\u7684\u4e00\u7cfb\u5217\u6587\u7ae0\uff0c\u5e0c\u671b\u80fd\u7ee7\u7eed\u5728\u603b\u7ed3\u524d\u4eba\u7684\u57fa\u7840\u4e0a\u53d1\u626c\u5149\u5927DDD\u7684\u601d\u60f3\uff0c\u4f46\u662f\u901a\u8fc7\u4e00\u5957\u6211\u8ba4\u4e3a\u5408\u7406\u7684\u4ee3\u7801\u7ed3\u6784\u3001\u6846\u67b6\u548c\u7ea6\u675f\uff0c\u6765\u964d\u4f4eDDD\u7684\u5b9e\u8df5\u95e8\u69db\uff0c\u63d0\u5347\u4ee3\u7801\u8d28\u91cf\u3001\u53ef\u6d4b\u8bd5\u6027\u3001\u5b89\u5168\u6027\u3001\u5065\u58ee\u6027\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5bfc\u8bfb<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5bf9\u4e8e\u4e00\u4e2a\u67b6\u6784\u5e08\u6765\u8bf4\uff0c\u5728\u8f6f\u4ef6\u5f00\u53d1\u4e2d\u5982\u4f55\u964d\u4f4e\u7cfb\u7edf\u590d\u6742\u5ea6\u662f\u4e00\u4e2a\u6c38\u6052\u7684\u6311\u6218\uff0c\u65e0\u8bba\u662f 94 \u5e74 GoF \u7684 Design Patterns \uff0c 99 \u5e74\u7684 Martin Fowler \u7684 Refactoring \uff0c 02 \u5e74\u7684 P of EAA \uff0c\u8fd8\u662f 03 \u5e74\u7684 Enterprise Integration Patterns \uff0c\u90fd\u662f\u901a\u8fc7\u4e00\u7cfb\u5217\u7684\u8bbe\u8ba1\u6a21\u5f0f\u6216\u8303\u4f8b\u6765\u964d\u4f4e\u4e00\u4e9b\u5e38\u89c1\u7684\u590d\u6742\u5ea6\u3002\u4f46\u662f\u95ee\u9898\u5728\u4e8e\uff0c\u8fd9\u4e9b\u4e66\u7684\u7406\u5ff5\u662f\u901a\u8fc7\u6280\u672f\u624b\u6bb5\u89e3\u51b3\u6280\u672f\u95ee\u9898\uff0c\u4f46\u5e76\u6ca1\u6709\u4ece\u6839\u672c\u4e0a\u89e3\u51b3\u4e1a\u52a1\u7684\u95ee\u9898\u3002\u6240\u4ee5 03 \u5e74 Eric Evans \u7684 Domain Driven Design \u4e00\u4e66\uff0c\u4ee5\u53ca\u540e\u7eed Vaughn Vernon \u7684 Implementing DDD \uff0c Uncle Bob \u7684 Clean Architecture \u7b49\u4e66\uff0c\u771f\u6b63\u7684\u4ece\u4e1a\u52a1\u7684\u89d2\u5ea6\u51fa\u53d1\uff0c\u4e3a\u5168\u4e16\u754c\u7edd\u5927\u90e8\u5206\u505a\u7eaf\u4e1a\u52a1\u7684\u5f00\u53d1\u63d0\u4f9b\u4e86\u4e00\u6574\u5957\u7684\u67b6\u6784\u601d\u8def\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u524d\u8a00<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u7531\u4e8e DDD \u4e0d\u662f\u4e00\u5957\u6846\u67b6\uff0c\u800c\u662f\u4e00\u79cd\u67b6\u6784\u601d\u60f3\uff0c\u6240\u4ee5\u5728\u4ee3\u7801\u5c42\u9762\u7f3a\u4e4f\u4e86\u8db3\u591f\u7684\u7ea6\u675f\uff0c\u5bfc\u81f4 DDD \u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u4e0a\u624b\u95e8\u69db\u5f88\u9ad8\uff0c\u751a\u81f3\u53ef\u4ee5\u8bf4\u7edd\u5927\u90e8\u5206\u4eba\u90fd\u5bf9 DDD \u7684\u7406\u89e3\u6709\u6240\u504f\u5dee\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c Martin Fowler \u5728\u4ed6\u4e2a\u4eba\u535a\u5ba2\u91cc\u63cf\u8ff0\u7684\u4e00\u4e2a Anti-pattern\uff0cAnemic Domain Model \uff08\u8d2b\u8840\u57df\u6a21\u578b\uff09\u5728\u5b9e\u9645\u5e94\u7528\u5f53\u4e2d\u5c42\u51fa\u4e0d\u7a77\uff0c\u800c\u4e00\u4e9b\u4ecd\u7136\u706b\u70ed\u7684 ORM \u5de5\u5177\u6bd4\u5982 Hibernate\uff0cEntity Framework \u5b9e\u9645\u4e0a\u52a9\u957f\u4e86\u8d2b\u8840\u6a21\u578b\u7684\u6269\u6563\u3002\u540c\u6837\u7684\uff0c\u4f20\u7edf\u7684\u57fa\u4e8e\u6570\u636e\u5e93\u6280\u672f\u4ee5\u53ca MVC \u7684\u56db\u5c42\u5e94\u7528\u67b6\u6784\uff08UI\u3001Business\u3001Data Access\u3001Database\uff09\uff0c\u5728\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u548c DDD \u7684\u4e00\u4e9b\u6982\u5ff5\u6df7\u6dc6\uff0c\u5bfc\u81f4\u7edd\u5927\u90e8\u5206\u4eba\u5728\u5b9e\u9645\u5e94\u7528\u5f53\u4e2d\u4ec5\u4ec5\u7528\u5230\u4e86 DDD \u7684\u5efa\u6a21\u7684\u601d\u60f3\uff0c\u800c\u5176\u5bf9\u4e8e\u6574\u4e2a\u67b6\u6784\u4f53\u7cfb\u7684\u601d\u60f3\u65e0\u6cd5\u843d\u5730\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u7b2c\u4e00\u6b21\u63a5\u89e6 DDD \u5e94\u8be5\u662f 2012 \u5e74\uff0c\u5f53\u65f6\u9664\u4e86\u5927\u578b\u4e92\u8054\u7f51\u516c\u53f8\uff0c\u57fa\u672c\u4e0a\u5546\u4e1a\u5e94\u7528\u90fd\u8fd8\u5904\u4e8e\u5355\u673a\u7684\u65f6\u4ee3\uff0c\u670d\u52a1\u5316\u7684\u67b6\u6784\u8fd8\u5c40\u9650\u4e8e\u5355\u673a +LB \u7528 MVC \u63d0\u4f9b Rest \u63a5\u53e3\u4f9b\u5916\u90e8\u8c03\u7528\uff0c\u6216\u8005\u7528 SOAP \u6216 WebServices \u505a RPC \u8c03\u7528\uff0c\u4f46\u5176\u5b9e\u66f4\u591a\u5c40\u9650\u4e8e\u5bf9\u5916\u90e8\u4f9d\u8d56\u7684\u534f\u8bae\u3002\u8ba9\u6211\u5173\u6ce8\u5230 DDD \u601d\u60f3\u7684\u662f\u4e00\u4e2a\u53eb Anti-Corruption Layer\uff08\u9632\u8150\u5c42\uff09\u7684\u6982\u5ff5\uff0c\u7279\u522b\u662f\u5176\u5728\u89e3\u51b3\u5916\u90e8\u4f9d\u8d56\u9891\u7e41\u53d8\u66f4\u7684\u60c5\u51b5\u4e0b\uff0c\u5982\u4f55\u5c06\u6838\u5fc3\u4e1a\u52a1\u903b\u8f91\u548c\u5916\u90e8\u4f9d\u8d56\u9694\u79bb\u7684\u673a\u5236\u3002\u5230\u4e86 2014 \u5e74\uff0c SOA \u5f00\u59cb\u5927\u884c\u5176\u9053\uff0c\u5fae\u670d\u52a1\u7684\u6982\u5ff5\u5f00\u59cb\u5192\u5934\uff0c\u800c\u5982\u4f55\u5c06\u4e00\u4e2a Monolith \u5e94\u7528\u5408\u7406\u7684\u62c6\u5206\u4e3a\u591a\u4e2a\u5fae\u670d\u52a1\u6210\u4e3a\u4e86\u5404\u5927\u8bba\u575b\u7684\u70ed\u95e8\u8bdd\u9898\uff0c\u800c DDD \u91cc\u9762\u7684 Bounded Context\uff08\u9650\u754c\u4e0a\u4e0b\u6587\uff09\u7684\u601d\u60f3\u4e3a\u5fae\u670d\u52a1\u62c6\u5206\u63d0\u4f9b\u4e86\u4e00\u5957\u5408\u7406\u7684\u6846\u67b6\u3002\u800c\u5728\u4eca\u5929\uff0c\u5728\u4e00\u4e2a\u6240\u6709\u7684\u4e1c\u897f\u90fd\u80fd\u88ab\u79f0\u4e4b\u4e3a\u201c\u670d\u52a1\u201d\u7684\u65f6\u4ee3\uff08XAAS\uff09\uff0c DDD \u7684\u601d\u60f3\u8ba9\u6211\u4eec\u80fd\u51b7\u9759\u4e0b\u6765\uff0c\u53bb\u601d\u8003\u5230\u5e95\u54ea\u4e9b\u4e1c\u897f\u53ef\u4ee5\u88ab\u670d\u52a1\u5316\u62c6\u5206\uff0c\u54ea\u4e9b\u903b\u8f91\u9700\u8981\u805a\u5408\uff0c\u624d\u80fd\u5e26\u6765\u6700\u5c0f\u7684\u7ef4\u62a4\u6210\u672c\uff0c\u800c\u4e0d\u662f\u7b80\u5355\u7684\u53bb\u8ffd\u6c42\u5f00\u53d1\u6548\u7387\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\u4eca\u5929\uff0c\u6211\u5f00\u59cb\u8fd9\u4e2a\u5173\u4e8e DDD \u7684\u4e00\u7cfb\u5217\u6587\u7ae0\uff0c\u5e0c\u671b\u80fd\u7ee7\u7eed\u5728\u603b\u7ed3\u524d\u4eba\u7684\u57fa\u7840\u4e0a\u53d1\u626c\u5149\u5927 DDD \u7684\u601d\u60f3\uff0c\u4f46\u662f\u901a\u8fc7\u4e00\u5957\u6211\u8ba4\u4e3a\u5408\u7406\u7684\u4ee3\u7801\u7ed3\u6784\u3001\u6846\u67b6\u548c\u7ea6\u675f\uff0c\u6765\u964d\u4f4e DDD \u7684\u5b9e\u8df5\u95e8\u69db\uff0c\u63d0\u5347\u4ee3\u7801\u8d28\u91cf\u3001\u53ef\u6d4b\u8bd5\u6027\u3001\u5b89\u5168\u6027\u3001\u5065\u58ee\u6027\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u672a\u6765\u4f1a\u8986\u76d6\u7684\u5185\u5bb9\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u6700\u4f73\u67b6\u6784\u5b9e\u8df5\uff1a\u516d\u8fb9\u5f62\u5e94\u7528\u67b6\u6784 \/ Clean \u67b6\u6784\u7684\u6838\u5fc3\u601d\u60f3\u548c\u843d\u5730\u65b9\u6848<\/li><li>\u6301\u7eed\u53d1\u73b0\u548c\u4ea4\u4ed8\uff1aEvent Storming &gt; Context Map &gt; Design Heuristics &gt; Modelling<\/li><li>\u964d\u4f4e\u67b6\u6784\u8150\u8d25\u901f\u5ea6\uff1a\u901a\u8fc7 Anti-Corruption Layer \u96c6\u6210\u7b2c\u4e09\u65b9\u5e93\u7684\u6a21\u5757\u5316\u65b9\u6848<\/li><li>\u6807\u51c6\u7ec4\u4ef6\u7684\u89c4\u8303\u548c\u8fb9\u754c\uff1aEntity, Aggregate, Repository, Domain Service, Application Service, Event, DTO Assembler \u7b49<\/li><li>\u57fa\u4e8e Use Case \u91cd\u5b9a\u4e49\u5e94\u7528\u670d\u52a1\u7684\u8fb9\u754c<\/li><li>\u57fa\u4e8e DDD \u7684\u5fae\u670d\u52a1\u5316\u6539\u9020\u53ca\u9897\u7c92\u5ea6\u63a7\u5236<\/li><li>CQRS \u67b6\u6784\u7684\u6539\u9020\u548c\u6311\u6218<\/li><li>\u57fa\u4e8e\u4e8b\u4ef6\u9a71\u52a8\u7684\u67b6\u6784\u7684\u6311\u6218<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4eca\u5929\u5148\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7\u6700\u57fa\u7840\uff0c\u4f46\u6781\u5176\u6709\u4ef7\u503c\u7684Domain Primitive\u7684\u6982\u5ff5\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Domain Primitive<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c31\u597d\u50cf\u5728\u5b66\u4efb\u4f55\u8bed\u8a00\u65f6\u9996\u5148\u9700\u8981\u4e86\u89e3\u7684\u662f\u57fa\u7840\u6570\u636e\u7c7b\u578b\u4e00\u6837\uff0c\u5728\u5168\u9762\u4e86\u89e3 DDD \u4e4b\u524d\uff0c\u9996\u5148\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u4e00\u4e2a\u6700\u57fa\u7840\u7684\u6982\u5ff5: Domain Primitive\uff08DP\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Primitive \u7684\u5b9a\u4e49\u662f\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u4e0d\u4ece\u4efb\u4f55\u5176\u4ed6\u4e8b\u7269\u53d1\u5c55\u800c\u6765<br>\n\u521d\u7ea7\u7684\u5f62\u6210\u6216\u751f\u957f\u7684\u65e9\u671f\u9636\u6bb5<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c31\u597d\u50cf Integer\u3001String \u662f\u6240\u6709\u7f16\u7a0b\u8bed\u8a00\u7684Primitive\u4e00\u6837\uff0c\u5728 DDD \u91cc\uff0c DP \u53ef\u4ee5\u8bf4\u662f\u4e00\u5207\u6a21\u578b\u3001\u65b9\u6cd5\u3001\u67b6\u6784\u7684\u57fa\u7840\uff0c\u800c\u5c31\u50cf Integer\u3001String \u4e00\u6837\uff0c DP \u53c8\u662f\u65e0\u6240\u4e0d\u5728\u7684\u3002\u6240\u4ee5\uff0c\u7b2c\u4e00\u8bb2\u4f1a\u5bf9 DP \u505a\u4e00\u4e2a\u5168\u9762\u7684\u4ecb\u7ecd\u548c\u5206\u6790\uff0c\u4f46\u6211\u4eec\u5148\u4e0d\u53bb\u8bb2\u6982\u5ff5\uff0c\u800c\u662f\u4ece\u6848\u4f8b\u5165\u624b\uff0c\u770b\u770b\u4e3a\u4ec0\u4e48 DP \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6982\u5ff5\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1\u3001\u6848\u4f8b\u5206\u6790<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u5148\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u8fd9\u4e2a case \u7684\u4e1a\u52a1\u903b\u8f91\u5982\u4e0b\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u4e00\u4e2a\u65b0\u5e94\u7528\u5728\u5168\u56fd\u901a\u8fc7 \u5730\u63a8\u4e1a\u52a1\u5458 \u505a\u63a8\u5e7f\uff0c\u9700\u8981\u505a\u4e00\u4e2a\u7528\u6237\u6ce8\u518c\u7cfb\u7edf\uff0c\u540c\u65f6\u5e0c\u671b\u5728\u7528\u6237\u6ce8\u518c\u540e\u80fd\u591f\u901a\u8fc7\u7528\u6237\u7535\u8bdd\uff08\u5148\u5047\u8bbe\u4ec5\u9650\u5ea7\u673a\uff09\u7684\u5730\u57df\uff08\u533a\u53f7\uff09\u5bf9\u4e1a\u52a1\u5458\u53d1\u5956\u91d1\u3002<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u5148\u4e0d\u8981\u53bb\u7ea0\u7ed3\u8fd9\u4e2a\u6839\u636e\u7528\u6237\u7535\u8bdd\u53bb\u53d1\u5956\u91d1\u7684\u4e1a\u52a1\u903b\u8f91\u662f\u5426\u5408\u7406\uff0c\u4e5f\u5148\u4e0d\u8981\u53bb\u7ba1\u7528\u6237\u662f\u5426\u5e94\u8be5\u5728\u6ce8\u518c\u65f6\u548c\u4e1a\u52a1\u5458\u505a\u7ed1\u5b9a\uff0c\u8fd9\u91cc\u6211\u4eec\u770b\u7684\u4e3b\u8981\u8fd8\u662f\u5982\u4f55\u66f4\u52a0\u5408\u7406\u7684\u53bb\u5b9e\u73b0\u8fd9\u4e2a\u903b\u8f91\u3002\u4e00\u4e2a\u7b80\u5355\u7684\u7528\u6237\u548c\u7528\u6237\u6ce8\u518c\u7684\u4ee3\u7801\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class User {\n    Long userId;\n    String name;\n    String phone;\n    String address;\n    Long repId;\n}\n\npublic class RegistrationServiceImpl implements RegistrationService {\n\n    private SalesRepRepository salesRepRepo;\n    private UserRepository userRepo;\n\n    public User register(String name, String phone, String address) \n      throws ValidationException {\n        \/\/ \u6821\u9a8c\u903b\u8f91\n        if (name == null || name.length() == 0) {\n            throw new ValidationException(\"name\");\n        }\n        if (phone == null || !isValidPhoneNumber(phone)) {\n            throw new ValidationException(\"phone\");\n        }\n        \/\/ \u6b64\u5904\u7701\u7565address\u7684\u6821\u9a8c\u903b\u8f91\n\n        \/\/ \u53d6\u7535\u8bdd\u53f7\u91cc\u7684\u533a\u53f7\uff0c\u7136\u540e\u901a\u8fc7\u533a\u53f7\u627e\u5230\u533a\u57df\u5185\u7684SalesRep\n        String areaCode = null;\n        String[] areas = new String[]{\"0571\", \"021\", \"010\"};\n        for (int i = 0; i &lt; phone.length(); i++) {\n            String prefix = phone.substring(0, i);\n            if (Arrays.asList(areas).contains(prefix)) {\n                areaCode = prefix;\n                break;\n            }\n        }\n        SalesRep rep = salesRepRepo.findRep(areaCode);\n\n        \/\/ \u6700\u540e\u521b\u5efa\u7528\u6237\uff0c\u843d\u76d8\uff0c\u7136\u540e\u8fd4\u56de\n        User user = new User();\n        user.name = name;\n        user.phone = phone;\n        user.address = address;\n        if (rep != null) {\n            user.repId = rep.repId;\n        }\n\n        return userRepo.save(user);\n    }\n\n    private boolean isValidPhoneNumber(String phone) {\n        String pattern = \"^0[1-9]{2,3}-?\\\\d{8}$\";\n        return phone.matches(pattern);\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u65e5\u5e38\u7edd\u5927\u90e8\u5206\u4ee3\u7801\u548c\u6a21\u578b\u5176\u5b9e\u90fd\u8ddf\u8fd9\u4e2a\u662f\u7c7b\u4f3c\u7684\uff0c\u4e4d\u4e00\u770b\u8c8c\u4f3c\u6ca1\u5565\u95ee\u9898\uff0c\u4f46\u6211\u4eec\u518d\u6df1\u5165\u4e00\u6b65\uff0c\u4ece\u4ee5\u4e0b\u56db\u4e2a\u7ef4\u5ea6\u53bb\u5206\u6790\u4e00\u4e0b\uff1a\u63a5\u53e3\u7684\u6e05\u6670\u5ea6\uff08\u53ef\u9605\u8bfb\u6027\uff09\u3001\u6570\u636e\u9a8c\u8bc1\u548c\u9519\u8bef\u5904\u7406\u3001\u4e1a\u52a1\u903b\u8f91\u4ee3\u7801\u7684\u6e05\u6670\u5ea6\u3001\u548c\u53ef\u6d4b\u8bd5\u6027\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u258d\u95ee\u98981 &#8211; \u63a5\u53e3\u7684\u6e05\u6670\u5ea6\u300d<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728Java\u4ee3\u7801\u4e2d\uff0c\u5bf9\u4e8e\u4e00\u4e2a\u65b9\u6cd5\u6765\u8bf4\u6240\u6709\u7684\u53c2\u6570\u540d\u5728\u7f16\u8bd1\u65f6\u4e22\u5931\uff0c\u7559\u4e0b\u7684\u4ec5\u4ec5\u662f\u4e00\u4e2a\u53c2\u6570\u7c7b\u578b\u7684\u5217\u8868\uff0c\u6240\u4ee5\u6211\u4eec\u91cd\u65b0\u770b\u4e00\u4e0b\u4ee5\u4e0a\u7684\u63a5\u53e3\u5b9a\u4e49\uff0c\u5176\u5b9e\u5728\u8fd0\u884c\u65f6\u4ec5\u4ec5\u662f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>User register(String, String, String);\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\u4ee5\u4e0b\u7684\u4ee3\u7801\u662f\u4e00\u6bb5\u7f16\u8bd1\u5668\u5b8c\u5168\u4e0d\u4f1a\u62a5\u9519\u7684\uff0c\u5f88\u96be\u901a\u8fc7\u770b\u4ee3\u7801\u5c31\u80fd\u53d1\u73b0\u7684 bug \uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>service.register(\"\u4e1a\u4f59\u8349\", \"\u6d59\u6c5f\u7701\u676d\u5dde\u5e02\u4f59\u676d\u533a\u6587\u4e09\u897f\u8def969\u53f7\", \"0571-12345678\");<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u7136\uff0c\u5728\u771f\u5b9e\u4ee3\u7801\u4e2d\u8fd0\u884c\u65f6\u4f1a\u62a5\u9519\uff0c\u4f46\u8fd9\u79cd bug \u662f\u5728\u8fd0\u884c\u65f6\u88ab\u53d1\u73b0\u7684\uff0c\u800c\u4e0d\u662f\u5728\u7f16\u8bd1\u65f6\u3002\u666e\u901a\u7684 Code Review \u4e5f\u5f88\u96be\u53d1\u73b0\u8fd9\u79cd\u95ee\u9898\uff0c\u5f88\u6709\u53ef\u80fd\u662f\u4ee3\u7801\u4e0a\u7ebf\u540e\u624d\u4f1a\u88ab\u66b4\u9732\u51fa\u6765\u3002\u8fd9\u91cc\u7684\u601d\u8003\u662f\uff0c\u6709\u6ca1\u6709\u529e\u6cd5\u5728\u7f16\u7801\u65f6\u5c31\u907f\u514d\u8fd9\u79cd\u53ef\u80fd\u4f1a\u51fa\u73b0\u7684\u95ee\u9898\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u53e6\u5916\u4e00\u79cd\u5e38\u89c1\u7684\uff0c\u7279\u522b\u662f\u5728\u67e5\u8be2\u670d\u52a1\u4e2d\u5bb9\u6613\u51fa\u73b0\u7684\u4f8b\u5b50\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>User findByName(String name);\nUser findByPhone(String phone);\nUser findByNameAndPhone(String name, String phone);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u573a\u666f\u4e0b\uff0c\u7531\u4e8e\u5165\u53c2\u90fd\u662f String \u7c7b\u578b\uff0c\u4e0d\u5f97\u4e0d\u5728\u65b9\u6cd5\u540d\u4e0a\u9762\u52a0\u4e0a ByXXX \u6765\u533a\u5206\uff0c\u800c findByNameAndPhone \u540c\u6837\u4e5f\u4f1a\u9677\u5165\u524d\u9762\u7684\u5165\u53c2\u987a\u5e8f\u9519\u8bef\u7684\u95ee\u9898\uff0c\u800c\u4e14\u548c\u524d\u9762\u7684\u5165\u53c2\u4e0d\u540c\uff0c\u8fd9\u91cc\u53c2\u6570\u987a\u5e8f\u5982\u679c\u8f93\u9519\u4e86\uff0c\u65b9\u6cd5\u4e0d\u4f1a\u62a5\u9519\u53ea\u4f1a\u8fd4\u56de null\uff0c\u800c\u8fd9\u79cd bug \u66f4\u52a0\u96be\u88ab\u53d1\u73b0\u3002\u8fd9\u91cc\u7684\u601d\u8003\u662f\uff0c<strong>\u300c\u6709\u6ca1\u6709\u529e\u6cd5\u8ba9\u65b9\u6cd5\u5165\u53c2\u4e00\u76ee\u4e86\u7136\uff0c\u907f\u514d\u5165\u53c2\u9519\u8bef\u5bfc\u81f4\u7684 bug \uff1f\u300d<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u258d\u95ee\u98982 &#8211; \u6570\u636e\u9a8c\u8bc1\u548c\u9519\u8bef\u5904\u7406\u300d<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u524d\u9762\u8fd9\u6bb5\u6570\u636e\u6821\u9a8c\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (phone == null || !isValidPhoneNumber(phone)) {\n    throw new ValidationException(\"phone\");\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u65e5\u5e38\u7f16\u7801\u4e2d\u7ecf\u5e38\u4f1a\u51fa\u73b0\uff0c\u4e00\u822c\u6765\u8bf4\u8fd9\u79cd\u4ee3\u7801\u9700\u8981\u51fa\u73b0\u5728\u65b9\u6cd5\u7684\u6700\u524d\u7aef\uff0c\u786e\u4fdd\u80fd\u591f fail-fast \u3002\u4f46\u662f\u5047\u8bbe\u4f60\u6709\u591a\u4e2a\u7c7b\u4f3c\u7684\u63a5\u53e3\u548c\u7c7b\u4f3c\u7684\u5165\u53c2\uff0c\u5728\u6bcf\u4e2a\u65b9\u6cd5\u91cc\u8fd9\u6bb5\u903b\u8f91\u4f1a\u88ab\u91cd\u590d\u3002\u800c\u66f4\u4e25\u91cd\u7684\u662f\u5982\u679c\u672a\u6765\u6211\u4eec\u8981\u62d3\u5c55\u7535\u8bdd\u53f7\u53bb\u5305\u542b\u624b\u673a\u65f6\uff0c\u5f88\u53ef\u80fd\u9700\u8981\u52a0\u5165\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (phone == null || !isValidPhoneNumber(phone) || !isValidCellNumber(phone)) {\n    throw new ValidationException(\"phone\");\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u6709\u5f88\u591a\u4e2a\u5730\u65b9\u7528\u5230\u4e86 phone \u8fd9\u4e2a\u5165\u53c2\uff0c\u4f46\u662f\u6709\u4e2a\u5730\u65b9\u5fd8\u8bb0\u4fee\u6539\u4e86\uff0c\u4f1a\u9020\u6210 bug \u3002\u8fd9\u662f\u4e00\u4e2a DRY \u539f\u5219\u88ab\u8fdd\u80cc\u65f6\u7ecf\u5e38\u4f1a\u53d1\u751f\u7684\u95ee\u9898\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u6709\u4e2a\u65b0\u7684\u9700\u6c42\uff0c\u9700\u8981\u628a\u5165\u53c2\u9519\u8bef\u7684\u539f\u56e0\u8fd4\u56de\uff0c\u90a3\u4e48\u8fd9\u6bb5\u4ee3\u7801\u5c31\u53d8\u5f97\u66f4\u52a0\u590d\u6742\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (phone == null) {\n    throw new ValidationException(\"phone\u4e0d\u80fd\u4e3a\u7a7a\");\n} else if (!isValidPhoneNumber(phone)) {\n    throw new ValidationException(\"phone\u683c\u5f0f\u9519\u8bef\");\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u4ee5\u60f3\u50cf\u5f97\u5230\uff0c\u4ee3\u7801\u91cc\u5145\u65a5\u7740\u5927\u91cf\u7684\u7c7b\u4f3c\u4ee3\u7801\u5757\u65f6\uff0c\u7ef4\u62a4\u6210\u672c\u8981\u6709\u591a\u9ad8\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u540e\uff0c\u5728\u8fd9\u4e2a\u4e1a\u52a1\u65b9\u6cd5\u91cc\uff0c\u4f1a\uff08\u9690\u6027\u6216\u663e\u6027\u7684\uff09\u629b ValidationException\uff0c\u6240\u4ee5\u9700\u8981\u5916\u90e8\u8c03\u7528\u65b9\u53bbtry\/catch\uff0c\u800c\u4e1a\u52a1\u903b\u8f91\u5f02\u5e38\u548c\u6570\u636e\u6821\u9a8c\u5f02\u5e38\u88ab\u6df7\u5728\u4e86\u4e00\u8d77\uff0c\u662f\u5426\u662f\u5408\u7406\u7684\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4f20\u7edfJava\u67b6\u6784\u91cc\u6709\u51e0\u4e2a\u529e\u6cd5\u80fd\u591f\u53bb\u89e3\u51b3\u4e00\u90e8\u5206\u95ee\u9898\uff0c\u5e38\u89c1\u7684\u5982BeanValidation\u6ce8\u89e3\u6216ValidationUtils\u7c7b\uff0c\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Use Bean Validation\nUser registerWithBeanValidation(\n  @NotNull @NotBlank String name,\n  @NotNull @Pattern(regexp = \"^0?[1-9]{2,3}-?\\\\d{8}$\") String phone,\n  @NotNull String address\n);\n\n\/\/ Use ValidationUtils:\npublic User registerWithUtils(String name, String phone, String address) {\n    ValidationUtils.validateName(name); \/\/ throws ValidationException\n    ValidationUtils.validatePhone(phone);\n    ValidationUtils.validateAddress(address);\n    ...\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f46\u8fd9\u51e0\u4e2a\u4f20\u7edf\u7684\u65b9\u6cd5\u540c\u6837\u6709\u95ee\u9898\uff0c<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300cBeanValidation\uff1a\u300d<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u901a\u5e38\u53ea\u80fd\u89e3\u51b3\u7b80\u5355\u7684\u6821\u9a8c\u903b\u8f91\uff0c\u590d\u6742\u7684\u6821\u9a8c\u903b\u8f91\u4e00\u6837\u8981\u5199\u4ee3\u7801\u5b9e\u73b0\u5b9a\u5236\u6821\u9a8c\u5668<\/li><li>\u5728\u6dfb\u52a0\u4e86\u65b0\u6821\u9a8c\u903b\u8f91\u65f6\uff0c\u540c\u6837\u4f1a\u51fa\u73b0\u5728\u67d0\u4e9b\u5730\u65b9\u5fd8\u8bb0\u6dfb\u52a0\u4e00\u4e2a\u6ce8\u89e3\u7684\u60c5\u51b5\uff0cDRY\u539f\u5219\u8fd8\u662f\u4f1a\u88ab\u8fdd\u80cc<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300cValidationUtils\u7c7b\uff1a\u300d<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5f53\u5927\u91cf\u7684\u6821\u9a8c\u903b\u8f91\u96c6\u4e2d\u5728\u4e00\u4e2a\u7c7b\u91cc\u4e4b\u540e\uff0c\u8fdd\u80cc\u4e86Single Responsibility\u5355\u4e00\u6027\u539f\u5219\uff0c\u5bfc\u81f4\u4ee3\u7801\u6df7\u4e71\u548c\u4e0d\u53ef\u7ef4\u62a4<\/li><li>\u4e1a\u52a1\u5f02\u5e38\u548c\u6821\u9a8c\u5f02\u5e38\u8fd8\u662f\u4f1a\u6df7\u6742<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\uff0c<code>\u6709\u6ca1\u6709\u4e00\u79cd\u65b9\u6cd5\uff0c\u80fd\u591f\u4e00\u52b3\u6c38\u9038\u7684\u89e3\u51b3\u6240\u6709\u6821\u9a8c\u7684\u95ee\u9898\u4ee5\u53ca\u964d\u4f4e\u540e\u7eed\u7684\u7ef4\u62a4\u6210\u672c\u548c\u5f02\u5e38\u5904\u7406\u6210\u672c\u5462\uff1f<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u258d\u95ee\u98983 &#8211; \u4e1a\u52a1\u4ee3\u7801\u7684\u6e05\u6670\u5ea6\u300d<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u6bb5\u4ee3\u7801\u91cc\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String areaCode = null;\nString[] areas = new String[]{\"0571\", \"021\", \"010\"};\nfor (int i = 0; i &lt; phone.length(); i++) {\n    String prefix = phone.substring(0, i);\n    if (Arrays.asList(areas).contains(prefix)) {\n        areaCode = prefix;\n        break;\n    }\n}\nSalesRep rep = salesRepRepo.findRep(areaCode);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b9e\u9645\u4e0a\u51fa\u73b0\u4e86\u53e6\u5916\u4e00\u79cd\u5e38\u89c1\u7684\u60c5\u51b5\uff0c\u90a3\u5c31\u662f\u4ece\u4e00\u4e9b\u5165\u53c2\u91cc\u62bd\u53d6\u4e00\u90e8\u5206\u6570\u636e\uff0c\u7136\u540e\u8c03\u7528\u4e00\u4e2a\u5916\u90e8\u4f9d\u8d56\u83b7\u53d6\u66f4\u591a\u7684\u6570\u636e\uff0c\u7136\u540e\u901a\u5e38\u4ece\u65b0\u7684\u6570\u636e\u4e2d\u518d\u62bd\u53d6\u90e8\u5206\u6570\u636e\u7528\u4f5c\u5176\u4ed6\u7684\u4f5c\u7528\u3002\u8fd9\u79cd\u4ee3\u7801\u901a\u5e38\u88ab\u79f0\u4f5c\u201c\u80f6\u6c34\u4ee3\u7801\u201d\uff0c\u5176\u672c\u8d28\u662f\u7531\u4e8e\u5916\u90e8\u4f9d\u8d56\u7684\u670d\u52a1\u7684\u5165\u53c2\u5e76\u4e0d\u7b26\u5408\u6211\u4eec\u539f\u59cb\u7684\u5165\u53c2\u5bfc\u81f4\u7684\u3002\u6bd4\u5982\uff0c\u5982\u679cSalesRepRepository\u5305\u542b\u4e00\u4e2afindRepByPhone\u7684\u65b9\u6cd5\uff0c\u5219\u4e0a\u9762\u5927\u90e8\u5206\u7684\u4ee3\u7801\u90fd\u4e0d\u5fc5\u8981\u4e86\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\uff0c\u4e00\u4e2a\u5e38\u89c1\u7684\u529e\u6cd5\u662f\u5c06\u8fd9\u6bb5\u4ee3\u7801\u62bd\u79bb\u51fa\u6765\uff0c\u53d8\u6210\u72ec\u7acb\u7684\u4e00\u4e2a\u6216\u591a\u4e2a\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private static String findAreaCode(String phone) {\n    for (int i = 0; i &lt; phone.length(); i++) {\n        String prefix = phone.substring(0, i);\n        if (isAreaCode(prefix)) {\n            return prefix;\n        }\n    }\n    return null;\n}\nprivate static boolean isAreaCode(String prefix) {\n    String[] areas = new String[]{\"0571\", \"021\"};\n    return Arrays.asList(areas).contains(prefix);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7136\u540e\u539f\u59cb\u4ee3\u7801\u53d8\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String areaCode = findAreaCode(phone);\nSalesRep rep = salesRepRepo.findRep(areaCode);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u800c\u4e3a\u4e86\u590d\u7528\u4ee5\u4e0a\u7684\u65b9\u6cd5\uff0c\u53ef\u80fd\u4f1a\u62bd\u79bb\u51fa\u4e00\u4e2a\u9759\u6001\u5de5\u5177\u7c7b PhoneUtils \u3002\u4f46\u662f\u8fd9\u91cc\u8981\u601d\u8003\u7684\u662f\uff0c\u9759\u6001\u5de5\u5177\u7c7b\u662f\u5426\u662f\u6700\u597d\u7684\u5b9e\u73b0\u65b9\u5f0f\u5462\uff1f\u5f53\u4f60\u7684\u9879\u76ee\u91cc\u5145\u65a5\u7740\u5927\u91cf\u7684\u9759\u6001\u5de5\u5177\u7c7b\uff0c\u4e1a\u52a1\u4ee3\u7801\u6563\u5728\u591a\u4e2a\u6587\u4ef6\u5f53\u4e2d\u65f6\uff0c\u4f60\u662f\u5426\u8fd8\u80fd\u627e\u5230\u6838\u5fc3\u7684\u4e1a\u52a1\u903b\u8f91\u5462\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u258d\u95ee\u98984 &#8211; \u53ef\u6d4b\u8bd5\u6027\u300d<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3a\u4e86\u4fdd\u8bc1\u4ee3\u7801\u8d28\u91cf\uff0c\u6bcf\u4e2a\u65b9\u6cd5\u91cc\u7684\u6bcf\u4e2a\u5165\u53c2\u7684\u6bcf\u4e2a\u53ef\u80fd\u51fa\u73b0\u7684\u6761\u4ef6\u90fd\u8981\u6709 TC \u8986\u76d6\uff08\u5047\u8bbe\u6211\u4eec\u5148\u4e0d\u53bb\u6d4b\u8bd5\u5185\u90e8\u4e1a\u52a1\u903b\u8f91\uff09\uff0c\u6240\u4ee5\u5728\u6211\u4eec\u8fd9\u4e2a\u65b9\u6cd5\u91cc\u9700\u8981\u4ee5\u4e0b\u7684 TC \uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/ucc.alicdn.com\/pic\/developer-ecology\/9842bb583278406793f564daaca97261.png\" alt=\"TC\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">TC\n<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u5982\u4e00\u4e2a\u65b9\u6cd5\u6709 N \u4e2a\u53c2\u6570\uff0c\u6bcf\u4e2a\u53c2\u6570\u6709 M \u4e2a\u6821\u9a8c\u903b\u8f91\uff0c\u81f3\u5c11\u8981\u6709 N * M \u4e2a TC \u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u8fd9\u65f6\u5019\u5728\u8be5\u65b9\u6cd5\u4e2d\u52a0\u5165\u4e00\u4e2a\u65b0\u7684\u5165\u53c2\u5b57\u6bb5 fax \uff0c\u5373\u4f7f fax \u548c phone \u7684\u6821\u9a8c\u903b\u8f91\u5b8c\u5168\u4e00\u81f4\uff0c\u4e3a\u4e86\u4fdd\u8bc1 TC \u8986\u76d6\u7387\uff0c\u4e5f\u4e00\u6837\u9700\u8981 M \u4e2a\u65b0\u7684 TC \u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u800c\u5047\u8bbe\u6709 P \u4e2a\u65b9\u6cd5\u4e2d\u90fd\u7528\u5230\u4e86 phone \u8fd9\u4e2a\u5b57\u6bb5\uff0c\u8fd9 P \u4e2a\u65b9\u6cd5\u90fd\u9700\u8981\u5bf9\u8be5\u5b57\u6bb5\u8fdb\u884c\u6d4b\u8bd5\uff0c\u4e5f\u5c31\u662f\u8bf4\u6574\u4f53\u9700\u8981\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>P *N* M<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e2a\u6d4b\u8bd5\u7528\u4f8b\u624d\u80fd\u5b8c\u5168\u8986\u76d6\u6240\u6709\u6570\u636e\u9a8c\u8bc1\u7684\u95ee\u9898\uff0c\u5728\u65e5\u5e38\u9879\u76ee\u4e2d\uff0c\u8fd9\u4e2a\u6d4b\u8bd5\u7684\u6210\u672c\u975e\u5e38\u4e4b\u9ad8\uff0c\u5bfc\u81f4\u5927\u91cf\u7684\u4ee3\u7801\u6ca1\u88ab\u8986\u76d6\u5230\u3002\u800c\u6ca1\u88ab\u6d4b\u8bd5\u8986\u76d6\u5230\u7684\u4ee3\u7801\u624d\u662f\u6700\u6709\u53ef\u80fd\u51fa\u73b0\u95ee\u9898\u7684\u5730\u65b9\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u60c5\u51b5\u4e0b\uff0c\u964d\u4f4e\u6d4b\u8bd5\u6210\u672c == \u63d0\u5347\u4ee3\u7801\u8d28\u91cf\uff0c\u5982\u4f55\u80fd\u591f\u964d\u4f4e\u6d4b\u8bd5\u7684\u6210\u672c\u5462\uff1f<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2\u3001\u89e3\u51b3\u65b9\u6848<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u56de\u5934\u5148\u91cd\u65b0\u770b\u4e00\u4e0b\u539f\u59cb\u7684 use case\uff0c\u5e76\u4e14\u6807\u6ce8\u5176\u4e2d\u53ef\u80fd\u91cd\u8981\u7684\u6982\u5ff5\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e00\u4e2a\u65b0\u5e94\u7528\u5728\u5168\u56fd\u901a\u8fc7 \u5730\u63a8\u4e1a\u52a1\u5458 \u505a\u63a8\u5e7f\uff0c\u9700\u8981\u505a\u4e00\u4e2a\u7528\u6237\u7684\u6ce8\u518c\u7cfb\u7edf\uff0c\u5728\u7528\u6237\u6ce8\u518c\u540e\u80fd\u591f\u901a\u8fc7\u7528\u6237\u7535\u8bdd\u53f7\u7684\u533a\u53f7\u5bf9\u4e1a\u52a1\u5458\u53d1\u5956\u91d1\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u5206\u6790\u4e86 use case \u540e\uff0c\u53d1\u73b0\u5176\u4e2d\u5730\u63a8\u4e1a\u52a1\u5458\u3001\u7528\u6237\u672c\u8eab\u81ea\u5e26 ID \u5c5e\u6027\uff0c\u5c5e\u4e8e Entity\uff08\u5b9e\u4f53\uff09\uff0c\u800c\u6ce8\u518c\u7cfb\u7edf\u5c5e\u4e8e Application Service\uff08\u5e94\u7528\u670d\u52a1\uff09\uff0c\u8fd9\u51e0\u4e2a\u6982\u5ff5\u5df2\u7ecf\u6709\u5b58\u5728\u3002\u4f46\u662f\u53d1\u73b0\u7535\u8bdd\u53f7\u8fd9\u4e2a\u6982\u5ff5\u5374\u5b8c\u5168\u88ab\u9690\u85cf\u5230\u4e86\u4ee3\u7801\u4e4b\u4e2d\u3002\u6211\u4eec\u53ef\u4ee5\u95ee\u4e00\u4e0b\u81ea\u5df1\uff0c\u53d6\u7535\u8bdd\u53f7\u7684\u533a\u53f7\u7684\u903b\u8f91\u662f\u5426\u5c5e\u4e8e\u7528\u6237\uff08\u7528\u6237\u7684\u533a\u53f7\uff1f\uff09\uff1f\u662f\u5426\u5c5e\u4e8e\u6ce8\u518c\u670d\u52a1\uff08\u6ce8\u518c\u7684\u533a\u53f7\uff1f\uff09\uff1f\u5982\u679c\u90fd\u4e0d\u662f\u5f88\u8d34\u5207\uff0c\u90a3\u5c31\u8bf4\u660e\u8fd9\u4e2a\u903b\u8f91\u5e94\u8be5\u5c5e\u4e8e\u4e00\u4e2a\u72ec\u7acb\u7684\u6982\u5ff5\u3002\u6240\u4ee5\u8fd9\u91cc\u5f15\u5165\u6211\u4eec\u7b2c\u4e00\u4e2a\u539f\u5219\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300cMake Implicit Concepts Explicit\u300d<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u5c06\u9690\u6027\u7684\u6982\u5ff5\u663e\u6027\u5316\u300d<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u539f\u6765\u7535\u8bdd\u53f7\u4ec5\u4ec5\u662f\u7528\u6237\u7684\u4e00\u4e2a\u53c2\u6570\uff0c\u5c5e\u4e8e\u9690\u5f62\u6982\u5ff5\uff0c\u4f46\u5b9e\u9645\u4e0a\u7535\u8bdd\u53f7\u7684\u533a\u53f7\u624d\u662f\u771f\u6b63\u7684\u4e1a\u52a1\u903b\u8f91\uff0c\u800c\u6211\u4eec\u9700\u8981\u5c06\u7535\u8bdd\u53f7\u7684\u6982\u5ff5\u663e\u6027\u5316\uff0c\u901a\u8fc7\u5199\u4e00\u4e2aValue Object\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class PhoneNumber {\n  \n    private final String number;\n    public String getNumber() {\n        return number;\n    }\n\n    public PhoneNumber(String number) {\n        if (number == null) {\n            throw new ValidationException(\"number\u4e0d\u80fd\u4e3a\u7a7a\");\n        } else if (isValid(number)) {\n            throw new ValidationException(\"number\u683c\u5f0f\u9519\u8bef\");\n        }\n        this.number = number;\n    }\n\n    public String getAreaCode() {\n        for (int i = 0; i &lt; number.length(); i++) {\n            String prefix = number.substring(0, i);\n            if (isAreaCode(prefix)) {\n                return prefix;\n            }\n        }\n        return null;\n    }\n\n    private static boolean isAreaCode(String prefix) {\n        String[] areas = new String[]{\"0571\", \"021\", \"010\"};\n        return Arrays.asList(areas).contains(prefix);\n    }\n\n    public static boolean isValid(String number) {\n        String pattern = \"^0?[1-9]{2,3}-?\\\\d{8}$\";\n        return number.matches(pattern);\n    }\n\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u91cc\u9762\u6709\u51e0\u4e2a\u5f88\u91cd\u8981\u7684\u5143\u7d20\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7 private final String number \u786e\u4fdd PhoneNumber \u662f\u4e00\u4e2a\uff08Immutable\uff09Value Object\u3002\uff08\u4e00\u822c\u6765\u8bf4 VO \u90fd\u662f Immutable \u7684\uff0c\u8fd9\u91cc\u53ea\u662f\u91cd\u70b9\u5f3a\u8c03\u4e00\u4e0b\uff09<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6821\u9a8c\u903b\u8f91\u90fd\u653e\u5728\u4e86 constructor \u91cc\u9762\uff0c\u786e\u4fdd\u53ea\u8981 PhoneNumber \u7c7b\u88ab\u521b\u5efa\u51fa\u6765\u540e\uff0c\u4e00\u5b9a\u662f\u6821\u9a8c\u901a\u8fc7\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e4b\u524d\u7684 findAreaCode \u65b9\u6cd5\u53d8\u6210\u4e86 PhoneNumber \u7c7b\u91cc\u7684 getAreaCode \uff0c\u7a81\u51fa\u4e86 areaCode \u662f PhoneNumber \u7684\u4e00\u4e2a\u8ba1\u7b97\u5c5e\u6027\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u6837\u505a\u5b8c\u4e4b\u540e\uff0c\u6211\u4eec\u53d1\u73b0\u628a PhoneNumber \u663e\u6027\u5316\u4e4b\u540e\uff0c\u5176\u5b9e\u662f\u751f\u6210\u4e86\u4e00\u4e2a Type\uff08\u6570\u636e\u7c7b\u578b\uff09\u548c\u4e00\u4e2a Class\uff08\u7c7b\uff09\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Type \u6307\u6211\u4eec\u5728\u4eca\u540e\u7684\u4ee3\u7801\u91cc\u53ef\u4ee5\u901a\u8fc7 PhoneNumber \u53bb\u663e\u6027\u7684\u6807\u8bc6\u7535\u8bdd\u53f7\u8fd9\u4e2a\u6982\u5ff5<\/li><li>Class \u6307\u6211\u4eec\u53ef\u4ee5\u628a\u6240\u6709\u8ddf\u7535\u8bdd\u53f7\u76f8\u5173\u7684\u903b\u8f91\u5b8c\u6574\u7684\u6536\u96c6\u5230\u4e00\u4e2a\u6587\u4ef6\u91cc<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e24\u4e2a\u6982\u5ff5\u52a0\u8d77\u6765\uff0c\u6784\u9020\u6210\u4e86\u672c\u6587\u6807\u9898\u7684 Domain Primitive\uff08DP\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u770b\u4e00\u4e0b\u5168\u9762\u4f7f\u7528\u4e86 DP \u4e4b\u540e\u6548\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class User {\n    UserId userId;\n    Name name;\n    PhoneNumber phone;\n    Address address;\n    RepId repId;\n}\n\npublic User register(\n  @NotNull Name name,\n  @NotNull PhoneNumber phone,\n  @NotNull Address address\n) {\n    \/\/ \u627e\u5230\u533a\u57df\u5185\u7684SalesRep\n    SalesRep rep = salesRepRepo.findRep(phone.getAreaCode());\n\n    \/\/ \u6700\u540e\u521b\u5efa\u7528\u6237\uff0c\u843d\u76d8\uff0c\u7136\u540e\u8fd4\u56de\uff0c\u8fd9\u90e8\u5206\u4ee3\u7801\u5b9e\u9645\u4e0a\u4e5f\u80fd\u7528Builder\u89e3\u51b3\n    User user = new User();\n    user.name = name;\n    user.phone = phone;\n    user.address = address;\n    if (rep != null) {\n        user.repId = rep.repId;\n    }\n\n    return userRepo.saveUser(user);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5728\u4f7f\u7528\u4e86 DP \u4e4b\u540e\uff0c\u6240\u6709\u7684\u6570\u636e\u9a8c\u8bc1\u903b\u8f91\u548c\u975e\u4e1a\u52a1\u6d41\u7a0b\u7684\u903b\u8f91\u90fd\u6d88\u5931\u4e86\uff0c\u5269\u4e0b\u90fd\u662f\u6838\u5fc3\u4e1a\u52a1\u903b\u8f91\uff0c\u53ef\u4ee5\u4e00\u76ee\u4e86\u7136\u3002\u6211\u4eec\u91cd\u65b0\u7528\u4e0a\u9762\u7684\u56db\u4e2a\u7ef4\u5ea6\u8bc4\u4f30\u4e00\u4e0b\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u258d\u8bc4\u4f301 &#8211; \u63a5\u53e3\u7684\u6e05\u6670\u5ea6\u300d<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u91cd\u6784\u540e\u7684\u65b9\u6cd5\u7b7e\u540d\u53d8\u6210\u4e86\u5f88\u6e05\u6670\u7684\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public User register(Name, PhoneNumber, Address)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u800c\u4e4b\u524d\u5bb9\u6613\u51fa\u73b0\u7684bug\uff0c\u5982\u679c\u6309\u7167\u73b0\u5728\u7684\u5199\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>service.register(new Name(\"\u6bb7\u6d69\"), new Address(\"\u6d59\u6c5f\u7701\u676d\u5dde\u5e02\u4f59\u676d\u533a\u6587\u4e09\u897f\u8def969\u53f7\"), new PhoneNumber(\"0571-12345678\"));<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8ba9\u63a5\u53e3 API \u53d8\u5f97\u5f88\u5e72\u51c0\uff0c\u6613\u62d3\u5c55\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u258d\u8bc4\u4f302 &#8211; \u6570\u636e\u9a8c\u8bc1\u548c\u9519\u8bef\u5904\u7406\u300d<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public User register(\n  @NotNull Name name,\n  @NotNull PhoneNumber phone,\n  @NotNull Address address\n) \/\/ no throws\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u524d\u6587\u4ee3\u7801\u5c55\u793a\u7684\uff0c\u91cd\u6784\u540e\u7684\u65b9\u6cd5\u91cc\uff0c\u5b8c\u5168\u6ca1\u6709\u4e86\u4efb\u4f55\u6570\u636e\u9a8c\u8bc1\u7684\u903b\u8f91\uff0c\u4e5f\u4e0d\u4f1a\u629b ValidationException \u3002\u539f\u56e0\u662f\u56e0\u4e3a DP \u7684\u7279\u6027\uff0c\u53ea\u8981\u662f\u80fd\u591f\u5e26\u5230\u5165\u53c2\u91cc\u7684\u4e00\u5b9a\u662f\u6b63\u786e\u7684\u6216 null\uff08Bean Validation \u6216 lombok \u7684\u6ce8\u89e3\u80fd\u89e3\u51b3 null \u7684\u95ee\u9898\uff09\u3002\u6240\u4ee5\u6211\u4eec\u628a\u6570\u636e\u9a8c\u8bc1\u7684\u5de5\u4f5c\u91cf\u524d\u7f6e\u5230\u4e86\u8c03\u7528\u65b9\uff0c\u800c\u8c03\u7528\u65b9\u672c\u6765\u5c31\u662f\u5e94\u8be5\u63d0\u4f9b\u5408\u6cd5\u6570\u636e\u7684\uff0c\u6240\u4ee5\u66f4\u52a0\u5408\u9002\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u518d\u5c55\u5f00\u6765\u770b\uff0c\u4f7f\u7528DP\u7684\u53e6\u4e00\u4e2a\u597d\u5904\u5c31\u662f\u4ee3\u7801\u9075\u5faa\u4e86 DRY \u539f\u5219\u548c\u5355\u4e00\u6027\u539f\u5219\uff0c\u5982\u679c\u672a\u6765\u9700\u8981\u4fee\u6539 PhoneNumber \u7684\u6821\u9a8c\u903b\u8f91\uff0c\u53ea\u9700\u8981\u5728\u4e00\u4e2a\u6587\u4ef6\u91cc\u4fee\u6539\u5373\u53ef\uff0c\u6240\u6709\u4f7f\u7528\u5230\u4e86 PhoneNumber \u7684\u5730\u65b9\u90fd\u4f1a\u751f\u6548\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u258d\u8bc4\u4f303 &#8211; \u4e1a\u52a1\u4ee3\u7801\u7684\u6e05\u6670\u5ea6\u300d<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SalesRep rep = salesRepRepo.findRep(phone.getAreaCode());\nUser user = xxx;\nreturn userRepo.save(user);<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u9664\u4e86\u5728\u4e1a\u52a1\u65b9\u6cd5\u91cc\u4e0d\u9700\u8981\u6821\u9a8c\u6570\u636e\u4e4b\u5916\uff0c\u539f\u6765\u7684\u4e00\u6bb5\u80f6\u6c34\u4ee3\u7801 findAreaCode \u88ab\u6539\u4e3a\u4e86 PhoneNumber \u7c7b\u7684\u4e00\u4e2a\u8ba1\u7b97\u5c5e\u6027 getAreaCode \uff0c\u8ba9\u4ee3\u7801\u6e05\u6670\u5ea6\u5927\u5927\u63d0\u5347\u3002\u800c\u4e14\u80f6\u6c34\u4ee3\u7801\u901a\u5e38\u90fd\u4e0d\u53ef\u590d\u7528\uff0c\u4f46\u662f\u4f7f\u7528\u4e86 DP \u540e\uff0c\u53d8\u6210\u4e86\u53ef\u590d\u7528\u3001\u53ef\u6d4b\u8bd5\u7684\u4ee3\u7801\u3002\u6211\u4eec\u80fd\u770b\u5230\uff0c\u5728\u5228\u9664\u4e86\u6570\u636e\u9a8c\u8bc1\u4ee3\u7801\u3001\u80f6\u6c34\u4ee3\u7801\u4e4b\u540e\uff0c\u5269\u4e0b\u7684\u90fd\u662f\u6838\u5fc3\u4e1a\u52a1\u903b\u8f91\u3002\uff08 Entity \u76f8\u5173\u7684\u91cd\u6784\u5728\u540e\u9762\u6587\u7ae0\u4f1a\u8c08\u5230\uff0c\u8fd9\u6b21\u5148\u5ffd\u7565\uff09<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u258d\u8bc4\u4f304 &#8211; \u53ef\u6d4b\u8bd5\u6027\u300d<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/ucc.alicdn.com\/pic\/developer-ecology\/cd132c509ee84eb68ccfaa290e777d30.png\" alt=\"TC\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">TC\n<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u6211\u4eec\u5c06 PhoneNumber \u62bd\u53d6\u51fa\u6765\u4e4b\u540e\uff0c\u5728\u6765\u770b\u6d4b\u8bd5\u7684 TC \uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u9996\u5148 PhoneNumber \u672c\u8eab\u8fd8\u662f\u9700\u8981 M \u4e2a\u6d4b\u8bd5\u7528\u4f8b\uff0c\u4f46\u662f\u7531\u4e8e\u6211\u4eec\u53ea\u9700\u8981\u6d4b\u8bd5\u5355\u4e00\u5bf9\u8c61\uff0c\u6bcf\u4e2a\u7528\u4f8b\u7684\u4ee3\u7801\u91cf\u4f1a\u5927\u5927\u964d\u4f4e\uff0c\u7ef4\u62a4\u6210\u672c\u964d\u4f4e\u3002<\/li><li>\u6bcf\u4e2a\u65b9\u6cd5\u91cc\u7684\u6bcf\u4e2a\u53c2\u6570\uff0c\u73b0\u5728\u53ea\u9700\u8981\u8986\u76d6\u4e3a null \u7684\u60c5\u51b5\u5c31\u53ef\u4ee5\u4e86\uff0c\u5176\u4ed6\u7684 case \u4e0d\u53ef\u80fd\u53d1\u751f\uff08\u56e0\u4e3a\u53ea\u8981\u4e0d\u662f null \u5c31\u4e00\u5b9a\u662f\u5408\u6cd5\u7684\uff09<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\uff0c\u5355\u4e2a\u65b9\u6cd5\u7684 TC \u4ece\u539f\u6765\u7684 N * M \u53d8\u6210\u4e86\u4eca\u5929\u7684 N + M \u3002\u540c\u6837\u7684\uff0c\u591a\u4e2a\u65b9\u6cd5\u7684 TC \u6570\u91cf\u53d8\u6210\u4e86<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300cN + M + P\u300d<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u6570\u91cf\u4e00\u822c\u6765\u8bf4\u8981\u8fdc\u4f4e\u4e8e\u539f\u6765\u7684\u6570\u91cf N <em>M<\/em> P \uff0c\u8ba9\u6d4b\u8bd5\u6210\u672c\u6781\u5927\u7684\u964d\u4f4e\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u258d\u8bc4\u4f30\u603b\u7ed3\u300d<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3\u3001\u8fdb\u9636\u4f7f\u7528<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4e0a\u6587\u6211\u4ecb\u7ecd\u4e86 DP \u7684\u7b2c\u4e00\u4e2a\u539f\u5219\uff1a\u5c06\u9690\u6027\u7684\u6982\u5ff5\u663e\u6027\u5316\u3002\u5728\u8fd9\u91cc\u6211\u5c06\u4ecb\u7ecd DP \u7684\u53e6\u5916\u4e24\u4e2a\u539f\u5219\uff0c\u7528\u4e00\u4e2a\u65b0\u7684\u6848\u4f8b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u258d\u6848\u4f8b1 &#8211; \u8f6c\u8d26\u300d<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u73b0\u5728\u8981\u5b9e\u73b0\u4e00\u4e2a\u529f\u80fd\uff0c\u8ba9A\u7528\u6237\u53ef\u4ee5\u652f\u4ed8 x \u5143\u7ed9\u7528\u6237 B \uff0c\u53ef\u80fd\u7684\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void pay(BigDecimal money, Long recipientId) {\n    BankService.transfer(money, \"CNY\", recipientId);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u8fd9\u4e2a\u662f\u5883\u5185\u8f6c\u8d26\uff0c\u5e76\u4e14\u5883\u5185\u7684\u8d27\u5e01\u6c38\u8fdc\u4e0d\u53d8\uff0c\u8be5\u65b9\u6cd5\u8c8c\u4f3c\u6ca1\u5565\u95ee\u9898\uff0c\u4f46\u5982\u679c\u6709\u4e00\u5929\u8d27\u5e01\u53d8\u66f4\u4e86\uff08\u6bd4\u5982\u6b27\u5143\u533a\u66fe\u7ecf\u51fa\u73b0\u7684\u95ee\u9898\uff09\uff0c\u6216\u8005\u6211\u4eec\u9700\u8981\u505a\u8de8\u5883\u8f6c\u8d26\uff0c\u8be5\u65b9\u6cd5\u662f\u660e\u663e\u7684 bug \uff0c\u56e0\u4e3a money \u5bf9\u5e94\u7684\u8d27\u5e01\u4e0d\u4e00\u5b9a\u662f CNY \u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a case \u91cc\uff0c\u5f53\u6211\u4eec\u8bf4\u201c\u652f\u4ed8 x \u5143\u201d\u65f6\uff0c\u9664\u4e86 x \u672c\u8eab\u7684\u6570\u5b57\u4e4b\u5916\uff0c\u5b9e\u9645\u4e0a\u662f\u6709\u4e00\u4e2a\u9690\u542b\u7684\u6982\u5ff5\u90a3\u5c31\u662f\u8d27\u5e01\u201c\u5143\u201d\u3002\u4f46\u662f\u5728\u539f\u59cb\u7684\u5165\u53c2\u91cc\uff0c\u4e4b\u6240\u4ee5\u53ea\u7528\u4e86 BigDecimal \u7684\u539f\u56e0\u662f\u6211\u4eec\u8ba4\u4e3a CNY \u8d27\u5e01\u662f\u9ed8\u8ba4\u7684\uff0c\u662f\u4e00\u4e2a\u9690\u542b\u7684\u6761\u4ef6\uff0c\u4f46\u662f\u5728\u6211\u4eec\u5199\u4ee3\u7801\u65f6\uff0c\u9700\u8981\u628a\u6240\u6709\u9690\u6027\u7684\u6761\u4ef6\u663e\u6027\u5316\uff0c\u800c\u8fd9\u4e9b\u6761\u4ef6\u6574\u4f53\u7ec4\u6210\u5f53\u524d\u7684\u4e0a\u4e0b\u6587\u3002\u6240\u4ee5 DP \u7684\u7b2c\u4e8c\u4e2a\u539f\u5219\u662f\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300cMake Implicit Context Explicit\u300d<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u5c06 \u9690\u6027\u7684 \u4e0a\u4e0b\u6587 \u663e\u6027\u5316\u300d<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\u5f53\u6211\u4eec\u505a\u8fd9\u4e2a\u652f\u4ed8\u529f\u80fd\u65f6\uff0c\u5b9e\u9645\u4e0a\u9700\u8981\u7684\u4e00\u4e2a\u5165\u53c2\u662f\u652f\u4ed8\u91d1\u989d + \u652f\u4ed8\u8d27\u5e01\u3002\u6211\u4eec\u53ef\u4ee5\u628a\u8fd9\u4e24\u4e2a\u6982\u5ff5\u7ec4\u5408\u6210\u4e3a\u4e00\u4e2a\u72ec\u7acb\u7684\u5b8c\u6574\u6982\u5ff5\uff1aMoney\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Value\npublic class Money {\n    private BigDecimal amount;\n    private Currency currency;\n    public Money(BigDecimal amount, Currency currency) {\n        this.amount = amount;\n        this.currency = currency;\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u800c\u539f\u6709\u7684\u4ee3\u7801\u5219\u53d8\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void pay(Money money, Long recipientId) {\n    BankService.transfer(money, recipientId);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u5c06\u9ed8\u8ba4\u8d27\u5e01\u8fd9\u4e2a\u9690\u6027\u7684\u4e0a\u4e0b\u6587\u6982\u5ff5\u663e\u6027\u5316\uff0c\u5e76\u4e14\u548c\u91d1\u989d\u5408\u5e76\u4e3a Money \uff0c\u6211\u4eec\u53ef\u4ee5\u907f\u514d\u5f88\u591a\u5f53\u524d\u770b\u4e0d\u51fa\u6765\uff0c\u4f46\u672a\u6765\u53ef\u80fd\u4f1a\u66b4\u96f7\u7684bug\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u258d\u6848\u4f8b2 &#8211; \u8de8\u5883\u8f6c\u8d26\u300d<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u524d\u9762\u7684\u6848\u4f8b\u5347\u7ea7\u4e00\u4e0b\uff0c\u5047\u8bbe\u7528\u6237\u53ef\u80fd\u8981\u505a\u8de8\u5883\u8f6c\u8d26\u4ece CNY \u5230 USD \uff0c\u5e76\u4e14\u8d27\u5e01\u6c47\u7387\u968f\u65f6\u5728\u6ce2\u52a8\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void pay(Money money, Currency targetCurrency, Long recipientId) {\n    if (money.getCurrency().equals(targetCurrency)) {\n        BankService.transfer(money, recipientId);\n    } else {\n        BigDecimal rate = ExchangeService.getRate(money.getCurrency(), targetCurrency);\n        BigDecimal targetAmount = money.getAmount().multiply(new BigDecimal(rate));\n        Money targetMoney = new Money(targetAmount, targetCurrency);\n        BankService.transfer(targetMoney, recipientId);\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2acase\u91cc\uff0c\u7531\u4e8e targetCurrency \u4e0d\u4e00\u5b9a\u548c money \u7684 Curreny \u4e00\u81f4\uff0c\u9700\u8981\u8c03\u7528\u4e00\u4e2a\u670d\u52a1\u53bb\u53d6\u6c47\u7387\uff0c\u7136\u540e\u505a\u8ba1\u7b97\u3002\u6700\u540e\u7528\u8ba1\u7b97\u540e\u7684\u7ed3\u679c\u505a\u8f6c\u8d26\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2acase\u6700\u5927\u7684\u95ee\u9898\u5728\u4e8e\uff0c\u91d1\u989d\u7684\u8ba1\u7b97\u88ab\u5305\u542b\u5728\u4e86\u652f\u4ed8\u7684\u670d\u52a1\u4e2d\uff0c\u6d89\u53ca\u5230\u7684\u5bf9\u8c61\u4e5f\u67092\u4e2a Currency \uff0c2 \u4e2a Money \uff0c1 \u4e2a BigDecimal \uff0c\u603b\u5171 5 \u4e2a\u5bf9\u8c61\u3002\u8fd9\u79cd\u6d89\u53ca\u5230\u591a\u4e2a\u5bf9\u8c61\u7684\u4e1a\u52a1\u903b\u8f91\uff0c\u9700\u8981\u7528 DP \u5305\u88c5\u6389\uff0c\u6240\u4ee5\u8fd9\u91cc\u5f15\u51fa DP \u7684\u7b2c\u4e09\u4e2a\u539f\u5219\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300cEncapsulate Multi-Object Behavior\u300d<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u5c01\u88c5 \u591a\u5bf9\u8c61 \u884c\u4e3a\u300d<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a case \u91cc\uff0c\u53ef\u4ee5\u5c06\u8f6c\u6362\u6c47\u7387\u7684\u529f\u80fd\uff0c\u5c01\u88c5\u5230\u4e00\u4e2a\u53eb\u505a ExchangeRate \u7684 DP \u91cc\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Value\npublic class ExchangeRate {\n    private BigDecimal rate;\n    private Currency from;\n    private Currency to;\n\n    public ExchangeRate(BigDecimal rate, Currency from, Currency to) {\n        this.rate = rate;\n        this.from = from;\n        this.to = to;\n    }\n\n    public Money exchange(Money fromMoney) {\n        notNull(fromMoney);\n        isTrue(this.from.equals(fromMoney.getCurrency()));\n        BigDecimal targetAmount = fromMoney.getAmount().multiply(rate);\n        return new Money(targetAmount, to);\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">ExchangeRate \u6c47\u7387\u5bf9\u8c61\uff0c\u901a\u8fc7\u5c01\u88c5\u91d1\u989d\u8ba1\u7b97\u903b\u8f91\u4ee5\u53ca\u5404\u79cd\u6821\u9a8c\u903b\u8f91\uff0c\u8ba9\u539f\u59cb\u4ee3\u7801\u53d8\u5f97\u6781\u5176\u7b80\u5355\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public void pay(Money money, Currency targetCurrency, Long recipientId) {\n    ExchangeRate rate = ExchangeService.getRate(money.getCurrency(), targetCurrency);\n    Money targetMoney = rate.exchange(money);\n    BankService.transfer(targetMoney, recipientId);\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">4\u3001\u8ba8\u8bba\u548c\u603b\u7ed3<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u258dDomain Primitive \u7684\u5b9a\u4e49\u300d<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8ba9\u6211\u4eec\u91cd\u65b0\u6765\u5b9a\u4e49\u4e00\u4e0b Domain Primitive \uff1aDomain Primitive \u662f\u4e00\u4e2a\u5728\u7279\u5b9a\u9886\u57df\u91cc\uff0c\u62e5\u6709\u7cbe\u51c6\u5b9a\u4e49\u7684\u3001\u53ef\u81ea\u6211\u9a8c\u8bc1\u7684\u3001\u62e5\u6709\u884c\u4e3a\u7684 Value Object \u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>DP\u662f\u4e00\u4e2a\u4f20\u7edf\u610f\u4e49\u4e0a\u7684Value Object\uff0c\u62e5\u6709Immutable\u7684\u7279\u6027<\/li><li>DP\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u6982\u5ff5\u6574\u4f53\uff0c\u62e5\u6709\u7cbe\u51c6\u5b9a\u4e49<\/li><li>DP\u4f7f\u7528\u4e1a\u52a1\u57df\u4e2d\u7684\u539f\u751f\u8bed\u8a00<\/li><li>DP\u53ef\u4ee5\u662f\u4e1a\u52a1\u57df\u7684\u6700\u5c0f\u7ec4\u6210\u90e8\u5206\u3001\u4e5f\u53ef\u4ee5\u6784\u5efa\u590d\u6742\u7ec4\u5408<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u6ce8\uff1aDomain Primitive\u7684\u6982\u5ff5\u548c\u547d\u540d\u6765\u81ea\u4e8eDan Bergh Johnsson &amp; Daniel Deogun\u7684\u4e66 Secure by Design\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u258d\u4f7f\u7528 Domain Primitive \u7684\u4e09\u539f\u5219\u300d<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u8ba9\u9690\u6027\u7684\u6982\u5ff5\u663e\u6027\u5316<\/li><li>\u8ba9\u9690\u6027\u7684\u4e0a\u4e0b\u6587\u663e\u6027\u5316<\/li><li>\u5c01\u88c5\u591a\u5bf9\u8c61\u884c\u4e3a<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u258dDomain Primitive \u548c DDD \u91cc Value Object \u7684\u533a\u522b\u300d<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 DDD \u4e2d\uff0c Value Object \u8fd9\u4e2a\u6982\u5ff5\u5176\u5b9e\u5df2\u7ecf\u5b58\u5728\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5728 Evans \u7684 DDD \u84dd\u76ae\u4e66\u4e2d\uff0cValue Object \u66f4\u591a\u7684\u662f\u4e00\u4e2a\u975e Entity \u7684\u503c\u5bf9\u8c61<\/li><li>\u5728Vernon\u7684IDDD\u7ea2\u76ae\u4e66\u4e2d\uff0c\u4f5c\u8005\u66f4\u591a\u7684\u5173\u6ce8\u4e86Value Object\u7684Immutability\u3001Equals\u65b9\u6cd5\u3001Factory\u65b9\u6cd5\u7b49<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Domain Primitive \u662f Value Object \u7684\u8fdb\u9636\u7248\uff0c\u5728\u539f\u59cb VO \u7684\u57fa\u7840\u4e0a\u8981\u6c42\u6bcf\u4e2a DP \u62e5\u6709\u6982\u5ff5\u7684\u6574\u4f53\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f\u503c\u5bf9\u8c61\u3002\u5728 VO \u7684 Immutable \u57fa\u7840\u4e0a\u589e\u52a0\u4e86 Validity \u548c\u884c\u4e3a\u3002\u5f53\u7136\u540c\u6837\u7684\u8981\u6c42\u65e0\u526f\u4f5c\u7528\uff08side-effect free\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u258dDomain Primitive \u548c Data Transfer Object (DTO) \u7684\u533a\u522b\u300d<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u65e5\u5e38\u5f00\u53d1\u4e2d\u7ecf\u5e38\u4f1a\u78b0\u5230\u7684\u53e6\u4e00\u4e2a\u6570\u636e\u7ed3\u6784\u662f DTO \uff0c\u6bd4\u5982\u65b9\u6cd5\u7684\u5165\u53c2\u548c\u51fa\u53c2\u3002DP \u548c DTO \u7684\u533a\u522b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/ucc.alicdn.com\/pic\/developer-ecology\/3de118a54f4b41879b361c6c030d5a70.png\" alt=\"image.png\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u258d\u4ec0\u4e48\u60c5\u51b5\u4e0b\u5e94\u8be5\u7528 Domain Primitive\u300d<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e38\u89c1\u7684 DP \u7684\u4f7f\u7528\u573a\u666f\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u6709\u683c\u5f0f\u9650\u5236\u7684 String\uff1a\u6bd4\u5982Name\uff0cPhoneNumber\uff0cOrderNumber\uff0cZipCode\uff0cAddress\u7b49<\/li><li>\u6709\u9650\u5236\u7684Integer\uff1a\u6bd4\u5982OrderId\uff08&gt;0\uff09\uff0cPercentage\uff080-100%\uff09\uff0cQuantity\uff08&gt;=0\uff09\u7b49<\/li><li>\u53ef\u679a\u4e3e\u7684 int \uff1a\u6bd4\u5982 Status\uff08\u4e00\u822c\u4e0d\u7528Enum\u56e0\u4e3a\u53cd\u5e8f\u5217\u5316\u95ee\u9898\uff09<\/li><li>Double \u6216 BigDecimal\uff1a\u4e00\u822c\u7528\u5230\u7684 Double \u6216 BigDecimal \u90fd\u662f\u6709\u4e1a\u52a1\u542b\u4e49\u7684\uff0c\u6bd4\u5982 Temperature\u3001Money\u3001Amount\u3001ExchangeRate\u3001Rating \u7b49<\/li><li>\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff1a\u6bd4\u5982 Map&gt; \u7b49\uff0c\u5c3d\u91cf\u80fd\u628a Map \u7684\u6240\u6709\u64cd\u4f5c\u5305\u88c5\u6389\uff0c\u4ec5\u66b4\u9732\u5fc5\u8981\u884c\u4e3a<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">5\u3001\u5b9e\u6218 &#8211; \u8001\u5e94\u7528\u91cd\u6784\u7684\u6d41\u7a0b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u65b0\u5e94\u7528\u4e2d\u4f7f\u7528 DP \u662f\u6bd4\u8f83\u7b80\u5355\u7684\uff0c\u4f46\u5728\u8001\u5e94\u7528\u4e2d\u4f7f\u7528 DP \u662f\u53ef\u4ee5\u9075\u5faa\u4ee5\u4e0b\u6d41\u7a0b\u6309\u90e8\u5c31\u73ed\u7684\u5347\u7ea7\u3002\u5728\u6b64\u7528\u672c\u6587\u7684\u7b2c\u4e00\u4e2a case \u4e3a\u4f8b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u258d\u7b2c\u4e00\u6b65 &#8211; \u521b\u5efa Domain Primitive\uff0c\u6536\u96c6\u6240\u6709 DP \u884c\u4e3a\u300d<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u524d\u6587\u4e2d\uff0c\u6211\u4eec\u53d1\u73b0\u53d6\u7535\u8bdd\u53f7\u7684\u533a\u53f7\u8fd9\u4e2a\u662f\u4e00\u4e2a\u53ef\u4ee5\u72ec\u7acb\u51fa\u6765\u7684\u3001\u53ef\u4ee5\u653e\u5165 PhoneNumber \u8fd9\u4e2a Class \u7684\u903b\u8f91\u3002\u7c7b\u4f3c\u7684\uff0c\u5728\u771f\u5b9e\u7684\u9879\u76ee\u4e2d\uff0c\u4ee5\u524d\u6563\u843d\u5728\u5404\u4e2a\u670d\u52a1\u6216\u5de5\u5177\u7c7b\u91cc\u9762\u7684\u4ee3\u7801\uff0c\u53ef\u4ee5\u90fd\u62bd\u51fa\u6765\u653e\u5728 DP \u91cc\uff0c\u6210\u4e3a DP \u81ea\u5df1\u7684\u884c\u4e3a\u6216\u5c5e\u6027\u3002\u8fd9\u91cc\u9762\u7684\u539f\u5219\u662f\uff1a\u6240\u6709\u62bd\u79bb\u51fa\u6765\u7684\u65b9\u6cd5\u8981\u505a\u5230\u65e0\u72b6\u6001\uff0c\u6bd4\u5982\u539f\u6765\u662f static \u7684\u65b9\u6cd5\u3002\u5982\u679c\u539f\u6765\u7684\u65b9\u6cd5\u6709\u72b6\u6001\u53d8\u66f4\uff0c\u9700\u8981\u5c06\u6539\u53d8\u72b6\u6001\u7684\u90e8\u5206\u548c\u4e0d\u6539\u72b6\u6001\u7684\u90e8\u5206\u5206\u79bb\uff0c\u7136\u540e\u5c06\u65e0\u72b6\u6001\u7684\u90e8\u5206\u878d\u5165 DP \u3002\u56e0\u4e3a DP \u672c\u8eab\u4e0d\u80fd\u5e26\u72b6\u6001\uff0c\u6240\u4ee5\u4e00\u5207\u9700\u8981\u6539\u53d8\u72b6\u6001\u7684\u4ee3\u7801\u90fd\u4e0d\u5c5e\u4e8e DP \u7684\u8303\u7574\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">(\u4ee3\u7801\u53c2\u8003 PhoneNumber \u7684\u4ee3\u7801\uff0c\u8fd9\u91cc\u4e0d\u518d\u91cd\u590d)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u258d\u7b2c\u4e8c\u6b65 &#8211; \u66ff\u6362\u6570\u636e\u6821\u9a8c\u548c\u65e0\u72b6\u6001\u903b\u8f91\u300d<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3a\u4e86\u4fdd\u969c\u73b0\u6709\u65b9\u6cd5\u7684\u517c\u5bb9\u6027\uff0c\u5728\u7b2c\u4e8c\u6b65\u4e0d\u4f1a\u53bb\u4fee\u6539\u63a5\u53e3\u7684\u7b7e\u540d\uff0c\u800c\u662f\u901a\u8fc7\u4ee3\u7801\u66ff\u6362\u539f\u6709\u7684\u6821\u9a8c\u903b\u8f91\u548c\u6839 DP \u76f8\u5173\u7684\u4e1a\u52a1\u903b\u8f91\u3002\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public User register(String name, String phone, String address)\n        throws ValidationException {\n    if (name == null || name.length() == 0) {\n        throw new ValidationException(\"name\");\n    }\n    if (phone == null || !isValidPhoneNumber(phone)) {\n        throw new ValidationException(\"phone\");\n    }\n    \n    String areaCode = null;\n    String[] areas = new String[]{\"0571\", \"021\", \"010\"};\n    for (int i = 0; i &lt; phone.length(); i++) {\n        String prefix = phone.substring(0, i);\n        if (Arrays.asList(areas).contains(prefix)) {\n            areaCode = prefix;\n            break;\n        }\n    }\n    SalesRep rep = salesRepRepo.findRep(areaCode);\n    \/\/ \u5176\u4ed6\u4ee3\u7801...\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7 DP \u66ff\u6362\u4ee3\u7801\u540e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public User register(String name, String phone, String address)\n        throws ValidationException {\n    \n    Name _name = new Name(name);\n    PhoneNumber _phone = new PhoneNumber(phone);\n    Address _address = new Address(address);\n    \n    SalesRep rep = salesRepRepo.findRep(_phone.getAreaCode());\n    \/\/ \u5176\u4ed6\u4ee3\u7801...\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7 new PhoneNumber(phone) \u8fd9\u79cd\u4ee3\u7801\uff0c\u66ff\u4ee3\u4e86\u539f\u6709\u7684\u6821\u9a8c\u4ee3\u7801\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7 _phone.getAreaCode() \u66ff\u6362\u4e86\u539f\u6709\u7684\u65e0\u72b6\u6001\u7684\u4e1a\u52a1\u903b\u8f91\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u258d\u7b2c\u4e09\u6b65 &#8211; \u521b\u5efa\u65b0\u63a5\u53e3\u300d<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u521b\u5efa\u65b0\u63a5\u53e3\uff0c\u5c06DP\u7684\u4ee3\u7801\u63d0\u5347\u5230\u63a5\u53e3\u53c2\u6570\u5c42\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public User register(Name name, PhoneNumber phone, Address address) {\n    SalesRep rep = salesRepRepo.findRep(phone.getAreaCode());\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u300c\u258d\u7b2c\u56db\u6b65 &#8211; \u4fee\u6539\u5916\u90e8\u8c03\u7528\u300d<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5916\u90e8\u8c03\u7528\u65b9\u9700\u8981\u4fee\u6539\u8c03\u7528\u94fe\u8def\uff0c\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>service.register(\"\u4e1a\u4f59\u8349\", \"0571-12345678\", \"\u6d59\u6c5f\u7701\u676d\u5dde\u5e02\u4f59\u676d\u533a\u6587\u4e09\u897f\u8def969\u53f7\");<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6539\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>service.register(new Name(\"\u4e1a\u4f59\u8349\"), new PhoneNumber(\"0571-12345678\"), new Address(\"\u6d59\u6c5f\u7701\u676d\u5dde\u5e02\u4f59\u676d\u533a\u6587\u4e09\u897f\u8def969\u53f7\"));<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u4ee5\u4e0a 4 \u6b65\uff0c\u5c31\u80fd\u8ba9\u4f60\u7684\u4ee3\u7801\u53d8\u5f97\u66f4\u52a0\u7b80\u6d01\u3001\u4f18\u96c5\u3001\u5065\u58ee\u3001\u5b89\u5168\u3002\u4f60\u8fd8\u5728\u7b49\u4ec0\u4e48\uff1f\u4eca\u5929\u5c31\u53bb\u5c1d\u8bd5\u5427\uff01<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u53c2\u8003\u8d44\u6599<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/martinfowler.com\/bliki\/AnemicDomainModel.html\">https:\/\/martinfowler.com\/bliki\/AnemicDomainModel.html<\/a><\/li><li><a href=\"https:\/\/developer.aliyun.com\/article\/716908\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"https:\/\/developer.aliyun.com\/article\/716908\uff08\u5728\u65b0\u7a97\u53e3\u6253\u5f00\uff09\">https:\/\/developer.aliyun.com\/article\/716908<\/a><\/li><\/ul>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=5207\">SpringBoot \u5fae\u670d\u52a1\u57fa\u4e8e DDD \u7684\u4e00\u4e9b\u4ee3\u7801\u5b9e\u8df5<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u7b80\u4ecb \u5173\u4e8eDDD\u7684\u4e00\u7cfb\u5217\u6587\u7ae0\uff0c\u5e0c\u671b\u80fd\u7ee7\u7eed\u5728\u603b\u7ed3\u524d\u4eba\u7684\u57fa\u7840\u4e0a\u53d1\u626c\u5149\u5927DDD\u7684\u601d\u60f3\uff0c\u4f46\u662f\u901a\u8fc7\u4e00\u5957\u6211\u8ba4\u4e3a\u5408\u7406\u7684\u4ee3\u7801\u7ed3 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[2114,1522,581,510,802],"class_list":["post-5207","post","type-post","status-publish","format-standard","hentry","category-java","tag-ddd","tag-springboot","tag-581","tag-510","tag-802"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5207","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5207"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5207\/revisions"}],"predecessor-version":[{"id":5208,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5207\/revisions\/5208"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}