{"id":4318,"date":"2023-07-19T23:56:49","date_gmt":"2023-07-19T15:56:49","guid":{"rendered":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=4318"},"modified":"2023-07-19T23:59:05","modified_gmt":"2023-07-19T15:59:05","slug":"%e7%ad%96%e7%95%a5%e6%a8%a1%e5%bc%8f%e7%9a%84%e4%be%8b%e5%ad%90","status":"publish","type":"post","link":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=4318","title":{"rendered":"\u7b56\u7565\u6a21\u5f0f\u7684\u4f8b\u5b50"},"content":{"rendered":"\n<p>\u8fd9\u662f\u4e00\u4e2a\u4f7f\u7528\u7b56\u7565\u6a21\u5f0f\u7684Java\u793a\u4f8b\uff0c\u5176\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6392\u5e8f\u7b56\u7565\u63a5\u53e3\uff0c\u4ee5\u53ca\u4e09\u4e2a\u5b9e\u73b0\u4e86\u8be5\u63a5\u53e3\u7684\u6392\u5e8f\u7b56\u7565\u7c7b\uff1a\u5192\u6ce1\u6392\u5e8f\u3001\u5feb\u901f\u6392\u5e8f\u548c\u63d2\u5165\u6392\u5e8f\u3002\u8fd9\u91cc\u5047\u8bbe\u6211\u4eec\u5df2\u7ecf\u5b9e\u73b0\u4e86\u8fd9\u4e09\u79cd\u6392\u5e8f\u7b97\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>import java.util.List;\n\n\/\/ \u7b56\u7565\u63a5\u53e3\ninterface SortStrategy {\n    void sort(List&lt;Integer> numbers);\n}\n\n\/\/ \u5192\u6ce1\u6392\u5e8f\u7b56\u7565\nclass BubbleSortStrategy implements SortStrategy {\n    public void sort(List&lt;Integer> numbers) {\n        \/\/ ...\u5177\u4f53\u5b9e\u73b0\u7565\n        System.out.println(\"Bubble sort\");\n    }\n}\n\n\/\/ \u5feb\u901f\u6392\u5e8f\u7b56\u7565\nclass QuickSortStrategy implements SortStrategy {\n    public void sort(List&lt;Integer> numbers) {\n        \/\/ ...\u5177\u4f53\u5b9e\u73b0\u7565\n        System.out.println(\"Quick sort\");\n    }\n}\n\n\/\/ \u63d2\u5165\u6392\u5e8f\u7b56\u7565\nclass InsertionSortStrategy implements SortStrategy {\n    public void sort(List&lt;Integer> numbers) {\n        \/\/ ...\u5177\u4f53\u5b9e\u73b0\u7565\n        System.out.println(\"Insertion sort\");\n    }\n}\n\n\/\/ \u4f7f\u7528\u7b56\u7565\u7684\u4e0a\u4e0b\u6587\u7c7b\nclass Sorter {\n    private SortStrategy strategy;\n\n    public Sorter(SortStrategy strategy) {\n        this.strategy = strategy;\n    }\n\n    public void sort(List&lt;Integer> numbers) {\n        strategy.sort(numbers);\n    }\n\n    public void setStrategy(SortStrategy strategy) {\n        this.strategy = strategy;\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7<code>Sorter<\/code>\u7c7b\u7684<code>setStrategy()<\/code>\u65b9\u6cd5\u8f7b\u677e\u66f4\u6539\u6392\u5e8f\u7b56\u7565\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">javaCopy code<code>public class Main {\n    public static void main(String[] args) {\n        List&lt;Integer&gt; numbers = Arrays.asList(3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5);\n\n        Sorter sorter = new Sorter(new BubbleSortStrategy());\n        sorter.sort(numbers);\n\n        sorter.setStrategy(new QuickSortStrategy());\n        sorter.sort(numbers);\n\n        sorter.setStrategy(new InsertionSortStrategy());\n        sorter.sort(numbers);\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5148\u7528\u5192\u6ce1\u6392\u5e8f\u5bf9\u5217\u8868\u6392\u5e8f\uff0c\u7136\u540e\u5207\u6362\u5230\u5feb\u901f\u6392\u5e8f\uff0c\u6700\u540e\u5207\u6362\u5230\u63d2\u5165\u6392\u5e8f\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u7b56\u7565\u6a21\u5f0f\u6765\u5904\u7406\u4e0d\u540c\u6298\u6263\u7b56\u7565\u7684\u7b80\u5355Java\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/ \u7b56\u7565\u63a5\u53e3\ninterface DiscountStrategy {\n    double applyDiscount(double price);\n}\n\n\/\/ \u5bf9\u4e8e\u65b0\u5ba2\u6237\uff0c\u53ef\u80fd\u6ca1\u6709\u6298\u6263\nclass NewCustomerDiscountStrategy implements DiscountStrategy {\n    public double applyDiscount(double price) {\n        return price;\n    }\n}\n\n\/\/ \u5bf9\u4e8e\u5e38\u89c4\u5ba2\u6237\uff0c\u53ef\u80fd\u670910%\u7684\u6298\u6263\nclass RegularCustomerDiscountStrategy implements DiscountStrategy {\n    public double applyDiscount(double price) {\n        return price * 0.9;\n    }\n}\n\n\/\/ \u5bf9\u4e8eVIP\u5ba2\u6237\uff0c\u53ef\u80fd\u670920%\u7684\u6298\u6263\nclass VipCustomerDiscountStrategy implements DiscountStrategy {\n    public double applyDiscount(double price) {\n        return price * 0.8;\n    }\n}\n\n\/\/ \u4f7f\u7528\u7b56\u7565\u7684\u8d2d\u7269\u8f66\u7c7b\nclass ShoppingCart {\n    private DiscountStrategy discountStrategy;\n\n    public ShoppingCart(DiscountStrategy discountStrategy) {\n        this.discountStrategy = discountStrategy;\n    }\n\n    public double checkout(double price) {\n        return discountStrategy.applyDiscount(price);\n    }\n\n    public void setDiscountStrategy(DiscountStrategy discountStrategy) {\n        this.discountStrategy = discountStrategy;\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5148\u4f7f\u7528\u65b0\u5ba2\u6237\u7b56\u7565\u7ed3\u8d26\uff0c\u7136\u540e\u5207\u6362\u5230\u5e38\u89c4\u5ba2\u6237\u7b56\u7565\uff0c\u6700\u540e\u5207\u6362\u5230VIP\u5ba2\u6237\u7b56\u7565\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">javaCopy code<code>public class Main {\n    public static void main(String[] args) {\n        double originalPrice = 100.0;\n\n        ShoppingCart cart = new ShoppingCart(new NewCustomerDiscountStrategy());\n        System.out.println(\"New customer: \" + cart.checkout(originalPrice));\n\n        cart.setDiscountStrategy(new RegularCustomerDiscountStrategy());\n        System.out.println(\"Regular customer: \" + cart.checkout(originalPrice));\n\n        cart.setDiscountStrategy(new VipCustomerDiscountStrategy());\n        System.out.println(\"VIP customer: \" + cart.checkout(originalPrice));\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u8fd0\u884c\u8fd9\u4e2a\u7a0b\u5e8f\uff0c\u4f60\u5c06\u770b\u5230\u6bcf\u79cd\u5ba2\u6237\u7c7b\u578b\u7684\u6298\u6263\u7b56\u7565\u5bf9\u4ef7\u683c\u7684\u5f71\u54cd\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u7b56\u7565\u6a21\u5f0f\u548c\u5de5\u5382\u6a21\u5f0f\u7684\u4e0d\u540c\u70b9\uff1f<\/p>\n\n\n\n<p>\u7b56\u7565\u6a21\u5f0f\u548c\u5de5\u5382\u6a21\u5f0f\u662f\u4e24\u79cd\u5e38\u89c1\u7684\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u90fd\u7528\u4e8e\u5904\u7406\u521b\u5efa\u548c\u4f7f\u7528\u5bf9\u8c61\u7684\u95ee\u9898\uff0c\u4f46\u4ed6\u4eec\u89e3\u51b3\u7684\u95ee\u9898\u548c\u4f7f\u7528\u573a\u666f\u6709\u6240\u4e0d\u540c\u3002<\/p>\n\n\n\n<p><strong>\u7b56\u7565\u6a21\u5f0f(Strategy Pattern)<\/strong> \u662f\u4e00\u79cd\u884c\u4e3a\u578b\u6a21\u5f0f\uff0c\u5b83\u7684\u4e3b\u8981\u601d\u60f3\u662f\u5b9a\u4e49\u4e00\u7cfb\u5217\u7684\u7b97\u6cd5\uff0c\u5c06\u6bcf\u4e00\u4e2a\u7b97\u6cd5\u5c01\u88c5\u8d77\u6765\uff0c\u5e76\u4f7f\u5b83\u4eec\u53ef\u4ee5\u4e92\u76f8\u66ff\u6362\u3002\u7b56\u7565\u6a21\u5f0f\u8ba9\u7b97\u6cd5\u72ec\u7acb\u4e8e\u4f7f\u7528\u5b83\u7684\u5ba2\u6237\u800c\u72ec\u7acb\u53d8\u5316\u3002\u7b56\u7565\u6a21\u5f0f\u4e3b\u8981\u7528\u4e8e\u9009\u62e9\u5e76\u4ea4\u6362\u5728\u8fd0\u884c\u65f6\u53ef\u80fd\u53d1\u751f\u53d8\u5316\u7684\u884c\u4e3a\u7b56\u7565\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u80fd\u4f1a\u6839\u636e\u4e0d\u540c\u7684\u60c5\u51b5\u4f7f\u7528\u4e0d\u540c\u7684\u6392\u5e8f\u7b97\u6cd5\u6216\u6298\u6263\u7b56\u7565\u3002<\/p>\n\n\n\n<p><strong>\u5de5\u5382\u6a21\u5f0f(Factory Pattern)<\/strong> \u662f\u4e00\u79cd\u521b\u5efa\u578b\u6a21\u5f0f\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u521b\u5efa\u5bf9\u8c61\u7684\u6700\u4f73\u65b9\u5f0f\u3002\u5728\u5de5\u5382\u6a21\u5f0f\u4e2d\uff0c\u6211\u4eec\u5728\u521b\u5efa\u5bf9\u8c61\u65f6\u4e0d\u4f1a\u5bf9\u5ba2\u6237\u66b4\u9732\u521b\u5efa\u903b\u8f91\uff0c\u800c\u662f\u901a\u8fc7\u4f7f\u7528\u4e00\u4e2a\u5171\u540c\u7684\u63a5\u53e3\u6765\u6307\u5411\u65b0\u521b\u5efa\u7684\u5bf9\u8c61\u3002\u5de5\u5382\u6a21\u5f0f\u4e3b\u8981\u7528\u4e8e\u7ba1\u7406\u548c\u63a7\u5236\u590d\u6742\u5bf9\u8c61\u7684\u521b\u5efa\u8fc7\u7a0b\uff0c\u5c24\u5176\u662f\u5f53\u5bf9\u8c61\u7684\u521b\u5efa\u8fc7\u7a0b\u6709\u8bb8\u591a\u6b65\u9aa4\u6216\u9700\u8981\u4f7f\u7528\u7279\u5b9a\u7684\u4fe1\u606f\u65f6\u3002<\/p>\n\n\n\n<p>\u7b80\u5355\u6765\u8bf4\uff0c<strong>\u533a\u522b\u5728\u4e8e<\/strong>\uff1a\u7b56\u7565\u6a21\u5f0f\u662f\u5728\u8fd0\u884c\u65f6\u6539\u53d8\u884c\u4e3a\u6216\u7b97\u6cd5\uff0c\u800c\u5de5\u5382\u6a21\u5f0f\u662f\u7528\u6765\u521b\u5efa\u5bf9\u8c61\uff0c\u5c01\u88c5\u4e86\u521b\u5efa\u5bf9\u8c61\u7684\u590d\u6742\u6027\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u5728\u4e00\u4e2a\u7535\u5546\u7cfb\u7edf\u4e2d\uff0c\u9488\u5bf9\u4e0d\u540c\u7684\u7528\u6237\u53ef\u80fd\u6709\u4e0d\u540c\u7684\u6298\u6263\u7b56\u7565\uff0c\u6b64\u65f6\u5c31\u53ef\u4ee5\u4f7f\u7528\u7b56\u7565\u6a21\u5f0f\uff0c\u6839\u636e\u7528\u6237\u7c7b\u578b\u52a8\u6001\u9009\u62e9\u5408\u9002\u7684\u6298\u6263\u7b56\u7565\u3002\u800c\u5982\u679c\u9700\u8981\u521b\u5efa\u7528\u6237\u5bf9\u8c61\uff0c\u7528\u6237\u7c7b\u578b\u5305\u62ec\u65b0\u7528\u6237\u3001\u8001\u7528\u6237\u548cVIP\u7528\u6237\uff0c\u6bcf\u79cd\u7c7b\u578b\u7684\u7528\u6237\u521b\u5efa\u8fc7\u7a0b\u90fd\u4e0d\u540c\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5c31\u9002\u5408\u4f7f\u7528\u5de5\u5382\u6a21\u5f0f\uff0c\u7528\u5de5\u5382\u7c7b\u6765\u8d1f\u8d23\u521b\u5efa\u4e0d\u540c\u7c7b\u578b\u7684\u7528\u6237\u5bf9\u8c61\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u662f\u4e00\u4e2a\u4f7f\u7528\u7b56\u7565\u6a21\u5f0f\u7684Java\u793a\u4f8b\uff0c\u5176\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6392\u5e8f\u7b56\u7565\u63a5\u53e3\uff0c\u4ee5\u53ca\u4e09\u4e2a\u5b9e\u73b0\u4e86\u8be5\u63a5\u53e3\u7684\u6392\u5e8f\u7b56\u7565\u7c7b\uff1a\u5192\u6ce1\u6392\u5e8f\u3001\u5feb [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[131],"tags":[282],"_links":{"self":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4318"}],"collection":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4318"}],"version-history":[{"count":4,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4318\/revisions"}],"predecessor-version":[{"id":4322,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/4318\/revisions\/4322"}],"wp:attachment":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}