{"id":10006339,"date":"2022-08-29T17:10:51","date_gmt":"2022-08-29T09:10:51","guid":{"rendered":"https:\/\/ityet.com\/?p=10006339"},"modified":"2022-08-29T17:10:51","modified_gmt":"2022-08-29T09:10:51","slug":"%e5%90%8c%e4%ba%8b%e5%86%99%e4%ba%86%e4%b8%80%e4%b8%aa%e8%b4%a3%e4%bb%bb%e9%93%be%e6%a8%a1%e5%bc%8f%ef%bc%8cbug-%e6%97%a0%e6%95%b0","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006339","title":{"rendered":"\u540c\u4e8b\u5199\u4e86\u4e00\u4e2a\u8d23\u4efb\u94fe\u6a21\u5f0f\uff0cbug \u65e0\u6570&#8230;"},"content":{"rendered":"<h2><strong>\u80cc\u666f<\/strong><\/h2>\n<p>\u6700\u8fd1\uff0c\u6211\u8ba9\u56e2\u961f\u5185\u4e00\u4f4d\u6210\u5458\u5199\u4e86\u4e00\u4e2a\u5bfc\u5165\u529f\u80fd\u3002\u4ed6\u4f7f\u7528\u4e86\u8d23\u4efb\u94fe\u6a21\u5f0f\uff0c\u4ee3\u7801\u5806\u7684\u975e\u5e38\u591a\uff0cbug \u4e5f\u591a\uff0c\u6ca1\u6709\u8fbe\u5230\u6211\u9884\u671f\u7684\u6548\u679c\u3002<\/p>\n<p>\u5b9e\u9645\u4e0a\uff0c\u9488\u5bf9\u5bfc\u5165\u529f\u80fd\uff0c\u6211\u8ba4\u4e3a\u6a21\u7248\u65b9\u6cd5\u66f4\u5408\u9002\uff01\u4e3a\u6b64\uff0c\u9694\u58c1\u56e2\u961f\u4e5f\u62ff\u51fa\u6211\u4eec\u7684\u6848\u4f8b\uff0c\u8fdb\u884c\u4e86\u96c6\u4f53 code review\u3002<\/p>\n<p>\u5b66\u597d\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u4e14\u4e0d\u8981\u4e3a\u4e86\u7ec3\u4e60\uff0c\u5f3a\u884c\u4f7f\u7528\uff01\u8ba9\u539f\u672c 100 \u884c\u5c31\u80fd\u5b9e\u73b0\u7684\u529f\u80fd\uff0c\u5199\u4e86 3000 \u884c\uff01\u5bf9\u9519\u6682\u4e14\u4e0d\u8bba\uff0c\u6211\u4eec\u5148\u4e00\u8d77\u770b\u770b\u8d23\u4efb\u94fe\u8bbe\u8ba1\u6a21\u5f0f\u5427\uff01<\/p>\n<h2><strong>\u4ec0\u4e48\u662f\u8d23\u4efb\u94fe<\/strong><\/h2>\n<p>\u8d23\u4efb\u94fe\u6a21\u5f0f\u662f\u4e00\u79cd\u884c\u4e3a\u8bbe\u8ba1\u6a21\u5f0f\uff0c \u5141\u8bb8\u4f60\u5c06\u8bf7\u6c42\u6cbf\u7740\u5904\u7406\u8005\u94fe\u8fdb\u884c\u53d1\u9001\u3002\u6536\u5230\u8bf7\u6c42\u540e\uff0c \u6bcf\u4e2a\u5904\u7406\u8005\u5747\u53ef\u5bf9\u8bf7\u6c42\u8fdb\u884c\u5904\u7406\uff0c \u6216\u5c06\u5176\u4f20\u9012\u7ed9\u94fe\u4e0a\u7684\u4e0b\u4e2a\u5904\u7406\u8005\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042392758\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h2><strong>\u4f7f\u7528\u573a\u666f<\/strong><\/h2>\n<p>\u8d23\u4efb\u94fe\u7684\u4f7f\u7528\u573a\u666f\u8fd8\u662f\u6bd4\u8f83\u591a\u7684\uff1a<\/p>\n<ul>\n<li>\u591a\u6761\u4ef6\u6d41\u7a0b\u5224\u65ad\uff1a\u6743\u9650\u63a7\u5236<\/li>\n<li>ERP \u7cfb\u7edf\u6d41\u7a0b\u5ba1\u6279\uff1a\u603b\u7ecf\u7406\u3001\u4eba\u4e8b\u7ecf\u7406\u3001\u9879\u76ee\u7ecf\u7406<\/li>\n<li>Java \u8fc7\u6ee4\u5668\u7684\u5e95\u5c42\u5b9e\u73b0 Filter<\/li>\n<\/ul>\n<p>\u5982\u679c\u4e0d\u4f7f\u7528\u8be5\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u90a3\u4e48\u5f53\u9700\u6c42\u6709\u6240\u6539\u53d8\u65f6\uff0c\u5c31\u4f1a\u4f7f\u5f97\u4ee3\u7801\u81c3\u80bf\u6216\u8005\u96be\u4ee5\u7ef4\u62a4\uff0c\u4f8b\u5982\u4e0b\u9762\u7684\u4f8b\u5b50\u3002<\/p>\n<h2><strong>| \u53cd\u4f8b<\/strong><\/h2>\n<p>\u5047\u8bbe\u73b0\u5728\u6709\u4e00\u4e2a\u95ef\u5173\u6e38\u620f\uff0c\u8fdb\u5165\u4e0b\u4e00\u5173\u7684\u6761\u4ef6\u662f\u4e0a\u4e00\u5173\u7684\u5206\u6570\u8981\u9ad8\u4e8e xx\uff1a<\/p>\n<ul>\n<li>\u6e38\u620f\u4e00\u5171 3 \u4e2a\u5173\u5361<\/li>\n<li>\u8fdb\u5165\u7b2c\u4e8c\u5173\u9700\u8981\u7b2c\u4e00\u5173\u7684\u6e38\u620f\u5f97\u5206\u5927\u4e8e\u7b49\u4e8e 80<\/li>\n<li>\u8fdb\u5165\u7b2c\u4e09\u5173\u9700\u8981\u7b2c\u4e8c\u5173\u7684\u6e38\u620f\u5f97\u5206\u5927\u4e8e\u7b49\u4e8e 90<\/li>\n<\/ul>\n<p>\u90a3\u4e48\u4ee3\u7801\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n<pre><code>\/\/\u7b2c\u4e00\u5173\npublic class FirstPassHandler {\n    public int handler(){\n        System.out.println(&quot;\u7b2c\u4e00\u5173--&gt;FirstPassHandler&quot;);\n        return 80;\n    }\n}\n\n\/\/\u7b2c\u4e8c\u5173\npublic class SecondPassHandler {\n    public int handler(){\n        System.out.println(&quot;\u7b2c\u4e8c\u5173--&gt;SecondPassHandler&quot;);\n        return 90;\n    }\n}\n\n\/\/\u7b2c\u4e09\u5173\npublic class ThirdPassHandler {\n    public int handler(){\n        System.out.println(&quot;\u7b2c\u4e09\u5173--&gt;ThirdPassHandler\uff0c\u8fd9\u662f\u6700\u540e\u4e00\u5173\u5566&quot;);\n        return 95;\n    }\n}\n\n\/\/\u5ba2\u6237\u7aef\npublic class HandlerClient {\n    public static void main(String[] args) {\n\n        FirstPassHandler firstPassHandler = new FirstPassHandler();\/\/\u7b2c\u4e00\u5173\n        SecondPassHandler secondPassHandler = new SecondPassHandler();\/\/\u7b2c\u4e8c\u5173\n        ThirdPassHandler thirdPassHandler = new ThirdPassHandler();\/\/\u7b2c\u4e09\u5173\n\n        int firstScore = firstPassHandler.handler();\n        \/\/\u7b2c\u4e00\u5173\u7684\u5206\u6570\u5927\u4e8e\u7b49\u4e8e80\u5219\u8fdb\u5165\u7b2c\u4e8c\u5173\n        if(firstScore &gt;= 80){\n            int secondScore = secondPassHandler.handler();\n            \/\/\u7b2c\u4e8c\u5173\u7684\u5206\u6570\u5927\u4e8e\u7b49\u4e8e90\u5219\u8fdb\u5165\u7b2c\u4e8c\u5173\n            if(secondScore &gt;= 90){\n                thirdPassHandler.handler();\n            }\n        }\n    }\n}<\/code><\/pre>\n<p>\u90a3\u4e48\u5982\u679c\u8fd9\u4e2a\u6e38\u620f\u6709 100 \u5173\uff0c\u6211\u4eec\u7684\u4ee3\u7801\u5f88\u53ef\u80fd\u5c31\u4f1a\u5199\u6210\u8fd9\u4e2a\u6837\u5b50\uff1a<\/p>\n<pre><code>if(\u7b2c1\u5173\u901a\u8fc7){\n    \/\/ \u7b2c2\u5173 \u6e38\u620f\n    if(\u7b2c2\u5173\u901a\u8fc7){\n        \/\/ \u7b2c3\u5173 \u6e38\u620f\n        if(\u7b2c3\u5173\u901a\u8fc7){\n           \/\/ \u7b2c4\u5173 \u6e38\u620f\n            if(\u7b2c4\u5173\u901a\u8fc7){\n                \/\/ \u7b2c5\u5173 \u6e38\u620f\n                if(\u7b2c5\u5173\u901a\u8fc7){\n                    \/\/ \u7b2c6\u5173 \u6e38\u620f\n                    if(\u7b2c6\u5173\u901a\u8fc7){\n                        \/\/...\n                    }\n                }\n            }\n        }\n    }\n}<\/code><\/pre>\n<p>\u8fd9\u79cd\u4ee3\u7801\u4e0d\u4ec5\u5197\u4f59\uff0c\u5e76\u4e14\u5f53\u6211\u4eec\u8981\u5c06\u67d0\u4e24\u5173\u8fdb\u884c\u8c03\u6574\u65f6\u4f1a\u5bf9\u4ee3\u7801\u975e\u5e38\u5927\u7684\u6539\u52a8\uff0c\u8fd9\u79cd\u64cd\u4f5c\u7684\u98ce\u9669\u662f\u5f88\u9ad8\u7684\uff0c\u56e0\u6b64\uff0c\u8be5\u5199\u6cd5\u975e\u5e38\u7cdf\u7cd5\u3002<\/p>\n<p>\u66f4\u591a\u8bbe\u8ba1\u6a21\u5f0f\u7cfb\u5217\u6559\u7a0b\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=Ao0HDD0qktti6jWKPmwVhA%3D%3D.189sGe%2BX1dPUw5ns0ymnUsa09IaTN9W5eHhg%2Fu0yI08%3D\" rel=\"nofollow\">https:\/\/www.javastack.cn\/<\/a><\/p>\n<h2><strong>| \u521d\u6b65\u6539\u9020<\/strong><\/h2>\n<p>\u5982\u4f55\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u94fe\u8868\u5c06\u6bcf\u4e00\u5173\u8fde\u63a5\u8d77\u6765\uff0c\u5f62\u6210\u8d23\u4efb\u94fe\u7684\u65b9\u5f0f\uff0c\u7b2c\u4e00\u5173\u901a\u8fc7\u540e\u662f\u7b2c\u4e8c\u5173\uff0c\u7b2c\u4e8c\u5173\u901a\u8fc7\u540e\u662f\u7b2c\u4e09\u5173&#8230;.<\/p>\n<p>\u8fd9\u6837\u5ba2\u6237\u7aef\u5c31\u4e0d\u9700\u8981\u8fdb\u884c\u591a\u91cd if \u7684\u5224\u65ad\u4e86\uff1a<\/p>\n<pre><code>public class FirstPassHandler {\n    \/**\n     * \u7b2c\u4e00\u5173\u7684\u4e0b\u4e00\u5173\u662f \u7b2c\u4e8c\u5173\n     *\/\n    private SecondPassHandler secondPassHandler;\n\n    public void setSecondPassHandler(SecondPassHandler secondPassHandler) {\n        this.secondPassHandler = secondPassHandler;\n    }\n\n    \/\/\u672c\u5173\u5361\u6e38\u620f\u5f97\u5206\n    private int play(){\n        return 80;\n    }\n\n    public int handler(){\n        System.out.println(&quot;\u7b2c\u4e00\u5173--&gt;FirstPassHandler&quot;);\n        if(play() &gt;= 80){\n            \/\/\u5206\u6570&gt;=80 \u5e76\u4e14\u5b58\u5728\u4e0b\u4e00\u5173\u624d\u8fdb\u5165\u4e0b\u4e00\u5173\n            if(this.secondPassHandler != null){\n                return this.secondPassHandler.handler();\n            }\n        }\n\n        return 80;\n    }\n}\n\npublic class SecondPassHandler {\n\n    \/**\n     * \u7b2c\u4e8c\u5173\u7684\u4e0b\u4e00\u5173\u662f \u7b2c\u4e09\u5173\n     *\/\n    private ThirdPassHandler thirdPassHandler;\n\n    public void setThirdPassHandler(ThirdPassHandler thirdPassHandler) {\n        this.thirdPassHandler = thirdPassHandler;\n    }\n\n    \/\/\u672c\u5173\u5361\u6e38\u620f\u5f97\u5206\n    private int play(){\n        return 90;\n    }\n\n    public int handler(){\n        System.out.println(&quot;\u7b2c\u4e8c\u5173--&gt;SecondPassHandler&quot;);\n\n        if(play() &gt;= 90){\n            \/\/\u5206\u6570&gt;=90 \u5e76\u4e14\u5b58\u5728\u4e0b\u4e00\u5173\u624d\u8fdb\u5165\u4e0b\u4e00\u5173\n            if(this.thirdPassHandler != null){\n                return this.thirdPassHandler.handler();\n            }\n        }\n\n        return 90;\n    }\n}\n\npublic class ThirdPassHandler {\n\n    \/\/\u672c\u5173\u5361\u6e38\u620f\u5f97\u5206\n    private int play(){\n        return 95;\n    }\n\n    \/**\n     * \u8fd9\u662f\u6700\u540e\u4e00\u5173\uff0c\u56e0\u6b64\u6ca1\u6709\u4e0b\u4e00\u5173\n     *\/\n    public int handler(){\n        System.out.println(&quot;\u7b2c\u4e09\u5173--&gt;ThirdPassHandler\uff0c\u8fd9\u662f\u6700\u540e\u4e00\u5173\u5566&quot;);\n        return play();\n    }\n}\n\npublic class HandlerClient {\n    public static void main(String[] args) {\n\n        FirstPassHandler firstPassHandler = new FirstPassHandler();\/\/\u7b2c\u4e00\u5173\n        SecondPassHandler secondPassHandler = new SecondPassHandler();\/\/\u7b2c\u4e8c\u5173\n        ThirdPassHandler thirdPassHandler = new ThirdPassHandler();\/\/\u7b2c\u4e09\u5173\n\n        firstPassHandler.setSecondPassHandler(secondPassHandler);\/\/\u7b2c\u4e00\u5173\u7684\u4e0b\u4e00\u5173\u662f\u7b2c\u4e8c\u5173\n        secondPassHandler.setThirdPassHandler(thirdPassHandler);\/\/\u7b2c\u4e8c\u5173\u7684\u4e0b\u4e00\u5173\u662f\u7b2c\u4e09\u5173\n\n        \/\/\u8bf4\u660e\uff1a\u56e0\u4e3a\u7b2c\u4e09\u5173\u662f\u6700\u540e\u4e00\u5173\uff0c\u56e0\u6b64\u6ca1\u6709\u4e0b\u4e00\u5173\n        \/\/\u5f00\u59cb\u8c03\u7528\u7b2c\u4e00\u5173 \u6bcf\u4e00\u4e2a\u5173\u5361\u662f\u5426\u8fdb\u5165\u4e0b\u4e00\u5173\u5361 \u5728\u6bcf\u4e2a\u5173\u5361\u4e2d\u5224\u65ad\n        firstPassHandler.handler();\n\n    }\n}<\/code><\/pre>\n<h2><strong>| \u7f3a\u70b9<\/strong><\/h2>\n<p>\u73b0\u6709\u6a21\u5f0f\u7684\u7f3a\u70b9\uff1a<\/p>\n<ul>\n<li>\u6bcf\u4e2a\u5173\u5361\u4e2d\u90fd\u6709\u4e0b\u4e00\u5173\u7684\u6210\u5458\u53d8\u91cf\u5e76\u4e14\u662f\u4e0d\u4e00\u6837\u7684\uff0c\u5f62\u6210\u94fe\u5f88\u4e0d\u65b9\u4fbf<\/li>\n<li>\u4ee3\u7801\u7684\u6269\u5c55\u6027\u975e\u5e38\u4e0d\u597d<\/li>\n<\/ul>\n<h2><strong>| \u8d23\u4efb\u94fe\u6539\u9020<\/strong><\/h2>\n<p>\u65e2\u7136\u6bcf\u4e2a\u5173\u5361\u4e2d\u90fd\u6709\u4e0b\u4e00\u5173\u7684\u6210\u5458\u53d8\u91cf\u5e76\u4e14\u662f\u4e0d\u4e00\u6837\u7684\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u5728\u5173\u5361\u4e0a\u62bd\u8c61\u51fa\u4e00\u4e2a\u7236\u7c7b\u6216\u8005\u63a5\u53e3\uff0c\u7136\u540e\u6bcf\u4e2a\u5177\u4f53\u7684\u5173\u5361\u53bb\u7ee7\u627f\u6216\u8005\u5b9e\u73b0\u3002<\/p>\n<p>\u6709\u4e86\u601d\u8def\uff0c\u6211\u4eec\u5148\u6765\u7b80\u5355\u4ecb\u7ecd\u4e00\u4e0b\u8d23\u4efb\u94fe\u8bbe\u8ba1\u6a21\u5f0f\u7684\u57fa\u672c\u7ec4\u6210\uff1a<\/p>\n<ul>\n<li><strong>\u62bd\u8c61\u5904\u7406\u8005\uff08Handler\uff09\u89d2\u8272\uff1a<\/strong>\u5b9a\u4e49\u4e00\u4e2a\u5904\u7406\u8bf7\u6c42\u7684\u63a5\u53e3\uff0c\u5305\u542b\u62bd\u8c61\u5904\u7406\u65b9\u6cd5\u548c\u4e00\u4e2a\u540e\u7ee7\u8fde\u63a5\u3002<\/li>\n<li><strong>\u5177\u4f53\u5904\u7406\u8005\uff08Concrete Handler\uff09\u89d2\u8272\uff1a<\/strong>\u5b9e\u73b0\u62bd\u8c61\u5904\u7406\u8005\u7684\u5904\u7406\u65b9\u6cd5\uff0c\u5224\u65ad\u80fd\u5426\u5904\u7406\u672c\u6b21\u8bf7\u6c42\uff0c\u5982\u679c\u53ef\u4ee5\u5904\u7406\u8bf7\u6c42\u5219\u5904\u7406\uff0c\u5426\u5219\u5c06\u8be5\u8bf7\u6c42\u8f6c\u7ed9\u5b83\u7684\u540e\u7ee7\u8005\u3002<\/li>\n<li><strong>\u5ba2\u6237\u7c7b\uff08Client\uff09\u89d2\u8272\uff1a<\/strong>\u521b\u5efa\u5904\u7406\u94fe\uff0c\u5e76\u5411\u94fe\u5934\u7684\u5177\u4f53\u5904\u7406\u8005\u5bf9\u8c61\u63d0\u4ea4\u8bf7\u6c42\uff0c\u5b83\u4e0d\u5173\u5fc3\u5904\u7406\u7ec6\u8282\u548c\u8bf7\u6c42\u7684\u4f20\u9012\u8fc7\u7a0b\u3002<\/li>\n<\/ul>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042392759\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<pre><code>public abstract class AbstractHandler {\n\n    \/**\n     * \u4e0b\u4e00\u5173\u7528\u5f53\u524d\u62bd\u8c61\u7c7b\u6765\u63a5\u6536\n     *\/\n    protected AbstractHandler next;\n\n    public void setNext(AbstractHandler next) {\n        this.next = next;\n    }\n\n    public abstract int handler();\n}\n\npublic class FirstPassHandler extends AbstractHandler{\n\n    private int play(){\n        return 80;\n    }\n\n    @Override\n    public int handler(){\n        System.out.println(&quot;\u7b2c\u4e00\u5173--&gt;FirstPassHandler&quot;);\n        int score = play();\n        if(score &gt;= 80){\n            \/\/\u5206\u6570&gt;=80 \u5e76\u4e14\u5b58\u5728\u4e0b\u4e00\u5173\u624d\u8fdb\u5165\u4e0b\u4e00\u5173\n            if(this.next != null){\n                return this.next.handler();\n            }\n        }\n        return score;\n    }\n}\n\npublic class SecondPassHandler extends AbstractHandler{\n\n    private int play(){\n        return 90;\n    }\n\n    public int handler(){\n        System.out.println(&quot;\u7b2c\u4e8c\u5173--&gt;SecondPassHandler&quot;);\n\n        int score = play();\n        if(score &gt;= 90){\n            \/\/\u5206\u6570&gt;=90 \u5e76\u4e14\u5b58\u5728\u4e0b\u4e00\u5173\u624d\u8fdb\u5165\u4e0b\u4e00\u5173\n            if(this.next != null){\n                return this.next.handler();\n            }\n        }\n\n        return score;\n    }\n}\n\npublic class ThirdPassHandler extends AbstractHandler{\n\n    private int play(){\n        return 95;\n    }\n\n    public int handler(){\n        System.out.println(&quot;\u7b2c\u4e09\u5173--&gt;ThirdPassHandler&quot;);\n        int score = play();\n        if(score &gt;= 95){\n            \/\/\u5206\u6570&gt;=95 \u5e76\u4e14\u5b58\u5728\u4e0b\u4e00\u5173\u624d\u8fdb\u5165\u4e0b\u4e00\u5173\n            if(this.next != null){\n                return this.next.handler();\n            }\n        }\n        return score;\n    }\n}\n\npublic class HandlerClient {\n    public static void main(String[] args) {\n\n        FirstPassHandler firstPassHandler = new FirstPassHandler();\/\/\u7b2c\u4e00\u5173\n        SecondPassHandler secondPassHandler = new SecondPassHandler();\/\/\u7b2c\u4e8c\u5173\n        ThirdPassHandler thirdPassHandler = new ThirdPassHandler();\/\/\u7b2c\u4e09\u5173\n\n        \/\/ \u548c\u4e0a\u9762\u6ca1\u6709\u66f4\u6539\u7684\u5ba2\u6237\u7aef\u4ee3\u7801\u76f8\u6bd4\uff0c\u53ea\u6709\u8fd9\u91cc\u7684set\u65b9\u6cd5\u53d1\u751f\u53d8\u5316\uff0c\u5176\u4ed6\u90fd\u662f\u4e00\u6837\u7684\n        firstPassHandler.setNext(secondPassHandler);\/\/\u7b2c\u4e00\u5173\u7684\u4e0b\u4e00\u5173\u662f\u7b2c\u4e8c\u5173\n        secondPassHandler.setNext(thirdPassHandler);\/\/\u7b2c\u4e8c\u5173\u7684\u4e0b\u4e00\u5173\u662f\u7b2c\u4e09\u5173\n\n        \/\/\u8bf4\u660e\uff1a\u56e0\u4e3a\u7b2c\u4e09\u5173\u662f\u6700\u540e\u4e00\u5173\uff0c\u56e0\u6b64\u6ca1\u6709\u4e0b\u4e00\u5173\n\n        \/\/\u4ece\u7b2c\u4e00\u4e2a\u5173\u5361\u5f00\u59cb\n        firstPassHandler.handler();\n\n    }\n}<\/code><\/pre>\n<h2><strong>| \u8d23\u4efb\u94fe\u5de5\u5382\u6539\u9020<\/strong><\/h2>\n<p>\u5bf9\u4e8e\u4e0a\u9762\u7684\u8bf7\u6c42\u94fe\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u628a\u8fd9\u4e2a\u5173\u7cfb\u7ef4\u62a4\u5230\u914d\u7f6e\u6587\u4ef6\u4e2d\u6216\u8005\u4e00\u4e2a\u679a\u4e3e\u4e2d\u3002\u6211\u5c06\u4f7f\u7528\u679a\u4e3e\u6765\u6559\u4f1a\u5927\u5bb6\u600e\u4e48\u52a8\u6001\u7684\u914d\u7f6e\u8bf7\u6c42\u94fe\u5e76\u4e14\u5c06\u6bcf\u4e2a\u8bf7\u6c42\u8005\u5f62\u6210\u4e00\u6761\u8c03\u7528\u94fe\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042392760\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<pre><code>public enum GatewayEnum {\n    \/\/ handlerId, \u62e6\u622a\u8005\u540d\u79f0\uff0c\u5168\u9650\u5b9a\u7c7b\u540d\uff0cpreHandlerId\uff0cnextHandlerId\n    API_HANDLER(new GatewayEntity(1, &quot;api\u63a5\u53e3\u9650\u6d41&quot;, &quot;cn.dgut.design.chain_of_responsibility.GateWay.impl.ApiLimitGatewayHandler&quot;, null, 2)),\n    BLACKLIST_HANDLER(new GatewayEntity(2, &quot;\u9ed1\u540d\u5355\u62e6\u622a&quot;, &quot;cn.dgut.design.chain_of_responsibility.GateWay.impl.BlacklistGatewayHandler&quot;, 1, 3)),\n    SESSION_HANDLER(new GatewayEntity(3, &quot;\u7528\u6237\u4f1a\u8bdd\u62e6\u622a&quot;, &quot;cn.dgut.design.chain_of_responsibility.GateWay.impl.SessionGatewayHandler&quot;, 2, null)),\n    ;\n\n    GatewayEntity gatewayEntity;\n\n    public GatewayEntity getGatewayEntity() {\n        return gatewayEntity;\n    }\n\n    GatewayEnum(GatewayEntity gatewayEntity) {\n        this.gatewayEntity = gatewayEntity;\n    }\n}\n\npublic class GatewayEntity {\n\n    private String name;\n\n    private String conference;\n\n    private Integer handlerId;\n\n    private Integer preHandlerId;\n\n    private Integer nextHandlerId;\n}\n\npublic interface GatewayDao {\n\n    \/**\n     * \u6839\u636e handlerId \u83b7\u53d6\u914d\u7f6e\u9879\n     * @param handlerId\n     * @return\n     *\/\n    GatewayEntity getGatewayEntity(Integer handlerId);\n\n    \/**\n     * \u83b7\u53d6\u7b2c\u4e00\u4e2a\u5904\u7406\u8005\n     * @return\n     *\/\n    GatewayEntity getFirstGatewayEntity();\n}\n\npublic class GatewayImpl implements GatewayDao {\n\n    \/**\n     * \u521d\u59cb\u5316\uff0c\u5c06\u679a\u4e3e\u4e2d\u914d\u7f6e\u7684handler\u521d\u59cb\u5316\u5230map\u4e2d\uff0c\u65b9\u4fbf\u83b7\u53d6\n     *\/\n    private static Map&lt;Integer, GatewayEntity&gt; gatewayEntityMap = new HashMap&lt;&gt;();\n\n    static {\n        GatewayEnum[] values = GatewayEnum.values();\n        for (GatewayEnum value : values) {\n            GatewayEntity gatewayEntity = value.getGatewayEntity();\n            gatewayEntityMap.put(gatewayEntity.getHandlerId(), gatewayEntity);\n        }\n    }\n\n    @Override\n    public GatewayEntity getGatewayEntity(Integer handlerId) {\n        return gatewayEntityMap.get(handlerId);\n    }\n\n    @Override\n    public GatewayEntity getFirstGatewayEntity() {\n        for (Map.Entry&lt;Integer, GatewayEntity&gt; entry : gatewayEntityMap.entrySet()) {\n            GatewayEntity value = entry.getValue();\n            \/\/  \u6ca1\u6709\u4e0a\u4e00\u4e2ahandler\u7684\u5c31\u662f\u7b2c\u4e00\u4e2a\n            if (value.getPreHandlerId() == null) {\n                return value;\n            }\n        }\n        return null;\n    }\n}\n\npublic class GatewayHandlerEnumFactory {\n\n    private static GatewayDao gatewayDao = new GatewayImpl();\n\n    \/\/ \u63d0\u4f9b\u9759\u6001\u65b9\u6cd5\uff0c\u83b7\u53d6\u7b2c\u4e00\u4e2ahandler\n    public static GatewayHandler getFirstGatewayHandler() {\n\n        GatewayEntity firstGatewayEntity = gatewayDao.getFirstGatewayEntity();\n        GatewayHandler firstGatewayHandler = newGatewayHandler(firstGatewayEntity);\n        if (firstGatewayHandler == null) {\n            return null;\n        }\n\n        GatewayEntity tempGatewayEntity = firstGatewayEntity;\n        Integer nextHandlerId = null;\n        GatewayHandler tempGatewayHandler = firstGatewayHandler;\n        \/\/ \u8fed\u4ee3\u904d\u5386\u6240\u6709handler\uff0c\u4ee5\u53ca\u5c06\u5b83\u4eec\u94fe\u63a5\u8d77\u6765\n        while ((nextHandlerId = tempGatewayEntity.getNextHandlerId()) != null) {\n            GatewayEntity gatewayEntity = gatewayDao.getGatewayEntity(nextHandlerId);\n            GatewayHandler gatewayHandler = newGatewayHandler(gatewayEntity);\n            tempGatewayHandler.setNext(gatewayHandler);\n            tempGatewayHandler = gatewayHandler;\n            tempGatewayEntity = gatewayEntity;\n        }\n    \/\/ \u8fd4\u56de\u7b2c\u4e00\u4e2ahandler\n        return firstGatewayHandler;\n    }\n\n    \/**\n     * \u53cd\u5c04\u5b9e\u4f53\u5316\u5177\u4f53\u7684\u5904\u7406\u8005\n     * @param firstGatewayEntity\n     * @return\n     *\/\n    private static GatewayHandler newGatewayHandler(GatewayEntity firstGatewayEntity) {\n        \/\/ \u83b7\u53d6\u5168\u9650\u5b9a\u7c7b\u540d\n        String className = firstGatewayEntity.getConference();\n        try {\n            \/\/ \u6839\u636e\u5168\u9650\u5b9a\u7c7b\u540d\uff0c\u52a0\u8f7d\u5e76\u521d\u59cb\u5316\u8be5\u7c7b\uff0c\u5373\u4f1a\u521d\u59cb\u5316\u8be5\u7c7b\u7684\u9759\u6001\u6bb5\n            Class&lt;?&gt; clazz = Class.forName(className);\n            return (GatewayHandler) clazz.newInstance();\n        } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {\n            e.printStackTrace();\n        }\n        return null;\n    }\n\n}\n\npublic class GetewayClient {\n    public static void main(String[] args) {\n        GetewayHandler firstGetewayHandler = GetewayHandlerEnumFactory.getFirstGetewayHandler();\n        firstGetewayHandler.service();\n    }\n}<\/code><\/pre>\n<p>\u8bbe\u8ba1\u6a21\u5f0f\u6709\u5f88\u591a\uff0c\u8d23\u4efb\u94fe\u53ea\u662f\u5176\u4e2d\u7684\u4e00\u79cd\uff0c\u6211\u89c9\u5f97\u5f88\u6709\u610f\u601d\uff0c\u975e\u5e38\u503c\u5f97\u4e00\u5b66\u3002\u8bbe\u8ba1\u6a21\u5f0f\u786e\u5b9e\u662f\u4e00\u95e8\u827a\u672f\uff0c\u4ecd\u9700\u52aa\u529b\u5440\uff01<\/p>\n<blockquote><p>\u7248\u6743\u58f0\u660e\uff1a\u672c\u6587\u4e3aCSDN\u535a\u4e3b\u300cJava\u5c0f\u6d77.\u300d\u7684\u539f\u521b\u6587\u7ae0\uff0c\u9075\u5faaCC 4.0 BY-SA\u7248\u6743\u534f\u8bae\uff0c\u8f6c\u8f7d\u8bf7\u9644\u4e0a\u539f\u6587\u51fa\u5904\u94fe\u63a5\u53ca\u672c\u58f0\u660e\u3002\u539f\u6587\u94fe\u63a5\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=8iKLQyDBn47oQgf%2BYds8ag%3D%3D.iMOdOFGhDePNz7dORpYfYemVsTWl2uxrtMQCLZrF4j8volt2ohebBw76ZuKwuVoa2Ug%2B6u7Q0tZBFCcUCrJ%2FpA%3D%3D\" rel=\"nofollow\">https:\/\/blog.csdn.net\/q147275&#8230;<\/a><\/p><\/blockquote>\n<p><strong>\u8fd1\u671f\u70ed\u6587\u63a8\u8350\uff1a<\/strong><\/p>\n<p>1.<a href=\"https:\/\/link.segmentfault.com\/?enc=WWxkgHUqHMndEkIvsuUBfQ%3D%3D.BIo0b0j2UxbrnKJPRAEq0gG61iCWmwIN4P4ROBUCFcY%3D\" rel=\"nofollow\">1,000+ \u9053 Java\u9762\u8bd5\u9898\u53ca\u7b54\u6848\u6574\u7406(2022\u6700\u65b0\u7248)<\/a><\/p>\n<p>2.<a href=\"https:\/\/link.segmentfault.com\/?enc=QVUDlqna5pkaWcUvUUPsOg%3D%3D.YAFKfLD5xKKkmkc8bCNRRIA7w%2FPwPyNa4VzPD20ktEeJbCCvPA1kPyMJmPNFQKsquIogivNCAD16j8c42QRr7A%3D%3D\" rel=\"nofollow\">\u52b2\u7206\uff01Java \u534f\u7a0b\u8981\u6765\u4e86\u3002\u3002\u3002<\/a><\/p>\n<p>3.<a href=\"https:\/\/link.segmentfault.com\/?enc=axvsfaTNcI3RY9jTWDNILg%3D%3D.Qzdfwgskz1GGJxLNl359op71VknYKoBgQaEAEDERK7O6ZjNc2WxQpCWHIkprMuxb\" rel=\"nofollow\">Spring Boot 2.x \u6559\u7a0b\uff0c\u592a\u5168\u4e86\uff01<\/a><\/p>\n<p>4.<a href=\"https:\/\/link.segmentfault.com\/?enc=s2nYteUTpB%2BMBoS4rsJrtQ%3D%3D.zuZuLI7rPxzUvRmPFCgWiAF%2FssMl%2BVc13QPH3CkTK%2FyAZoRCuXGwe%2Bcurt6RmzNdDk4w9GKnNvxrEaZuwR9u%2Bw%3D%3D\" rel=\"nofollow\">\u522b\u518d\u5199\u6ee1\u5c4f\u7684\u7206\u7206\u7206\u70b8\u7c7b\u4e86\uff0c\u8bd5\u8bd5\u88c5\u9970\u5668\u6a21\u5f0f\uff0c\u8fd9\u624d\u662f\u4f18\u96c5\u7684\u65b9\u5f0f\uff01\uff01<\/a><\/p>\n<p>5.<a href=\"https:\/\/link.segmentfault.com\/?enc=7qFSK6spG8lBHiIcHjq8Vg%3D%3D.LjwhvSsbRKiNx5%2BxPbbPRkM67utr%2FNoFP%2FHAYnyDGpiqvMzOrCRBKjvEUfqC9WQkpM4nDRAheq%2BOV0UbAqus3FlUE7NL4T%2FB5NRrUqLRTQU%3D\" rel=\"nofollow\">\u300aJava\u5f00\u53d1\u624b\u518c\uff08\u5d69\u5c71\u7248\uff09\u300b\u6700\u65b0\u53d1\u5e03\uff0c\u901f\u901f\u4e0b\u8f7d\uff01<\/a><\/p>\n<p>\u89c9\u5f97\u4e0d\u9519\uff0c\u522b\u5fd8\u4e86\u968f\u624b\u70b9\u8d5e+\u8f6c\u53d1\u54e6\uff01<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042392756\">https:\/\/segmentfault.com\/a\/1190000042392756<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u80cc\u666f \u6700\u8fd1\uff0c\u6211\u8ba9\u56e2\u961f\u5185\u4e00\u4f4d\u6210\u5458\u5199\u4e86\u4e00\u4e2a\u5bfc\u5165\u529f\u80fd\u3002\u4ed6\u4f7f\u7528\u4e86\u8d23\u4efb\u94fe\u6a21\u5f0f\uff0c\u4ee3\u7801\u5806\u7684\u975e\u5e38\u591a\uff0cbug \u4e5f\u591a\uff0c\u6ca1\u6709\u8fbe\u5230\u6211\u9884 [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006339","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006339","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006339"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006339\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006339"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}