{"id":1116,"date":"2023-03-16T21:18:14","date_gmt":"2023-03-16T13:18:14","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1116"},"modified":"2023-04-29T11:22:31","modified_gmt":"2023-04-29T03:22:31","slug":"design-pattern-strategy-mode","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/16\/design-pattern-strategy-mode\/","title":{"rendered":"\u8bbe\u8ba1\u6a21\u5f0f\uff087\uff09\u7b56\u7565\u6a21\u5f0f"},"content":{"rendered":"<p>\u901a\u5e38\u5982\u679c\u4e00\u4e2a\u95ee\u9898\u6709\u591a\u4e2a\u89e3\u51b3\u65b9\u6848\u65f6\uff0c\u6700\u7b80\u5355\u7684\u5c31\u662f\u5229\u7528if-else\u6216\u8005switch-case\u65b9\u5f0f\u6839\u636e\u4e0d\u540c\u7684\u60c5\u666f\u9009\u62e9\u4e0d\u540c\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u4f46\u662f\u8fd9\u6837\u8026\u5408\u6027\u592a\u9ad8\u3001\u4ee3\u7801\u81c3\u80bf\u3001\u96be\u4ee5\u7ef4\u62a4\u7b49\u3002\u8fd9\u65f6\u5c31\u53ef\u4ee5\u4f7f\u7528\u7b56\u7565\u6a21\u5f0f\u6765\u89e3\u51b3\u3002<\/p>\n<p><!-- more --><\/p>\n<h2>\u5b9a\u4e49<\/h2>\n<p>\u7b56\u7565\u6a21\u5f0f\u5b9a\u4e49\u4e86\u4e00\u7cfb\u5217\u7684\u7b97\u6cd5\uff0c\u5e76\u5c06\u6bcf\u4e00\u4e2a\u7b97\u6cd5\u5c01\u88c5\u8d77\u6765\uff0c\u800c\u4e14\u4f7f\u4ed6\u4eec\u8fd8\u53ef\u4ee5\u76f8\u4e92\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<\/p>\n<h2>\u4f7f\u7528\u573a\u666f<\/h2>\n<ul>\n<li>\u9488\u5bf9\u540c\u4e00\u7c7b\u578b\u95ee\u9898\u7684\u591a\u79cd\u5904\u7406\u65b9\u5f0f\uff0c\u4ec5\u4ec5\u662f\u5177\u4f53\u884c\u4e3a\u6709\u5dee\u522b\u65f6\u3002 <\/li>\n<li>\u9700\u8981\u5b89\u5168\u5730\u5c01\u88c5\u591a\u79cd\u540c\u4e00\u7c7b\u578b\u7684\u64cd\u4f5c\u65f6\u3002 <\/li>\n<li>\u51fa\u73b0\u540c\u4e00\u62bd\u8c61\u7c7b\u6709\u591a\u4e2a\u5b50\u7c7b\uff0c\u800c\u53c8\u9700\u8981\u4f7f\u7528if-else\u6216\u8005switch-case\u6765\u9009\u62e9\u5177\u4f53\u5b50\u7c7b\u65f6\u3002<\/li>\n<\/ul>\n<h2>\u7b80\u5355\u5b9e\u73b0<\/h2>\n<p>\u9700\u6c42\uff1a\u8ba1\u7b97\u56fe\u4e66\u4ef7\u683c\uff0c\u521d\u7ea7\u4f1a\u5458\u6ca1\u6709\u6298\u6263\uff0c\u4e2d\u7ea7\u4f1a\u5458\u62539\u6298\uff0c\u9ad8\u7ea7\u4f1a\u5458\u62538\u6298\u3002\u5982\u679c\u4e00\u822c\u5199\u6cd5\uff0c\u5e94\u8be5\u662fif-else\u5224\u65ad\u4ed6\u662f\u4ec0\u4e48\u7ea7\u522b\u7684\u4f1a\u5458\uff0c\u5728\u8ba1\u7b97\u76f8\u5e94\u7684\u6298\u6263\u3002\u4e0b\u9762\u4f7f\u7528\u7b56\u7565\u6a21\u5f0f\u6765\u8fdb\u884c\u5b9e\u73b0\u3002<\/p>\n<p>\u62bd\u8c61\u6298\u6263\u7c7b<\/p>\n<pre><code class=\"language-java\">public interface MemberStrategy {\n    \/**\n     * \u8ba1\u7b97\u56fe\u4e66\u7684\u4ef7\u683c\n     * @param booksPrice    \u56fe\u4e66\u7684\u539f\u4ef7\n     * @return    \u8ba1\u7b97\u51fa\u6253\u6298\u540e\u7684\u4ef7\u683c\n     *\/\n    public double calcPrice(double booksPrice);\n}<\/code><\/pre>\n<p>\u521d\u7ea7\u4f1a\u5458\u6298\u6263\u7c7b<\/p>\n<pre><code class=\"language-java\">public class PrimaryMemberStrategy implements MemberStrategy {\n    \/**\n     * \u521d\u7ea7\u4f1a\u5458\u6298\u6263\n     *\/\n    @Override\n    public double calcPrice(double booksPrice) {\n        System.out.println(&quot;\u5bf9\u4e8e\u521d\u7ea7\u4f1a\u5458\u7684\u6ca1\u6709\u6298\u6263&quot;);\n        return booksPrice;\n    }\n}<\/code><\/pre>\n<p>\u4e2d\u7ea7\u4f1a\u5458\u6298\u6263\u7c7b<\/p>\n<pre><code class=\"language-java\">public class IntermediateMemberStrategy implements MemberStrategy {\n    \/**\n     * \u4e2d\u7ea7\u4f1a\u5458\u6298\u6263\n     *\/\n    @Override\n    public double calcPrice(double booksPrice) {\n        System.out.println(&quot;\u5bf9\u4e8e\u4e2d\u7ea7\u4f1a\u5458\u7684\u6298\u6263\u4e3a10%&quot;);\n        return booksPrice * 0.9;\n    }\n}<\/code><\/pre>\n<p>\u9ad8\u7ea7\u4f1a\u5458\u6298\u6263\u7c7b<\/p>\n<pre><code class=\"language-java\">public class AdvancedMemberStrategy implements MemberStrategy {\n    \/**\n     * \u9ad8\u7ea7\u4f1a\u5458\u6298\u6263\n     *\/\n    @Override\n    public double calcPrice(double booksPrice) {\n        System.out.println(&quot;\u5bf9\u4e8e\u9ad8\u7ea7\u4f1a\u5458\u7684\u6298\u6263\u4e3a20%&quot;);\n        return booksPrice * 0.8;\n    }\n}<\/code><\/pre>\n<p>\u4ef7\u683c\u7c7b<\/p>\n<pre><code class=\"language-java\">public class Price {\n    \/\/\u6301\u6709\u4e00\u4e2a\u5177\u4f53\u7684\u7b56\u7565\u5bf9\u8c61\n    private MemberStrategy strategy;\n    \/**\n     * \u6784\u9020\u51fd\u6570\uff0c\u4f20\u5165\u4e00\u4e2a\u5177\u4f53\u7684\u7b56\u7565\u5bf9\u8c61\n     * @param strategy    \u5177\u4f53\u7684\u7b56\u7565\u5bf9\u8c61\n     *\/\n    public Price(MemberStrategy strategy) {\n        this.strategy = strategy;\n    }\n\n    \/**\n     * \u8ba1\u7b97\u56fe\u4e66\u7684\u4ef7\u683c\n     * @param booksPrice    \u56fe\u4e66\u7684\u539f\u4ef7\n     * @return    \u8ba1\u7b97\u51fa\u6253\u6298\u540e\u7684\u4ef7\u683c\n     *\/\n    public double quote(double booksPrice) {\n        return this.strategy.calcPrice(booksPrice);\n    }\n}<\/code><\/pre>\n<p>\u5ba2\u6237\u7aef<\/p>\n<pre><code class=\"language-java\">public class Client {\n\n    public static void main(String[] args) {\n        \/\/\u9009\u62e9\u5e76\u521b\u5efa\u9700\u8981\u4f7f\u7528\u7684\u7b56\u7565\u5bf9\u8c61\n        MemberStrategy strategy1 = new AdvancedMemberStrategy();\n        \/\/\u521b\u5efa\u73af\u5883\n        Price price = new Price(strategy1);\n        \/\/\u8ba1\u7b97\u4ef7\u683c\n        double quote = price.quote(300);\n        System.out.println(&quot;\u56fe\u4e66\u7684\u6700\u7ec8\u4ef7\u683c\u4e3a\uff1a&quot; + quote);\n    }\n\n}<\/code><\/pre>\n<p>\u7ed3\u679c<\/p>\n<pre><code>\u5bf9\u4e8e\u9ad8\u7ea7\u4f1a\u5458\u7684\u6298\u6263\u4e3a20%\n\u56fe\u4e66\u7684\u6700\u7ec8\u4ef7\u683c\u4e3a\uff1a240.0<\/code><\/pre>\n<h2>\u7b56\u7565\u6a21\u5f0f\u548c\u5de5\u5382\u6a21\u5f0f\u7684\u533a\u522b<\/h2>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">\u5de5\u5382\u6a21\u5f0f<\/th>\n<th style=\"text-align: left;\">\u7b56\u7565\u6a21\u5f0f<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">\u521b\u5efa\u578b\u7684\u8bbe\u8ba1\u6a21\u5f0f<\/td>\n<td style=\"text-align: left;\">\u884c\u4e3a\u578b\u7684\u8bbe\u8ba1\u6a21\u5f0f<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">\u5173\u6ce8\u5bf9\u8c61\u521b\u5efa<\/td>\n<td style=\"text-align: left;\">\u5173\u6ce8\u884c\u4e3a\u7684\u9009\u62e9<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">\u9ed1\u76d2\u5b50\uff08\u65e0\u9700\u77e5\u9053\u5177\u4f53\u7684\u5b9e\u73b0\u8fc7\u7a0b\uff09<\/td>\n<td style=\"text-align: left;\">\u767d\u76d2\u5b50\uff08\u77e5\u9053\u5177\u4f53\u7684\u5b9e\u73b0\u8fc7\u7a0b\uff09<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Android\u6e90\u7801\u4e2d\u7684\u7b56\u7565\u6a21\u5f0f\u5b9e\u73b0<\/h2>\n<ul>\n<li>\u65f6\u95f4\u63d2\u503c\u5668\uff08TimeInterpolator\uff09<\/li>\n<\/ul>\n<p><code>LinearInterpolator<\/code>\u3001<code>AccelerateInterpolator<\/code>\u3001<code>CycleInterpolator<\/code>\u7b49\u5b9e\u73b0<code>Interpolator<\/code>\uff0c\u901a\u8fc7<code>getInterpolator(float input)<\/code>\u83b7\u53d6\u5f53\u524d\u7684\u65f6\u95f4\u767e\u5206\u6bd4\uff0c\u4ee5\u6b64\u6765\u8ba1\u7b97\u52a8\u753b\u7684\u5c5e\u6027\u503c\u3002<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u7b56\u7565\u6a21\u5f0f\u4e3b\u8981\u7528\u6765\u5206\u79bb\u7b97\u6cd5\uff0c\u5728\u76f8\u540c\u7684\u884c\u4e3a\u62bd\u8c61\u4e0b\u6709\u4e0d\u540c\u7684\u5177\u4f53\u5b9e\u73b0\u7b56\u7565\u3002\u8fd9\u4e2a\u6a21\u5f0f\u5f88\u597d\u5730\u6f14\u793a\u4e86\u5f00\u95ed\u539f\u5219\uff0c\u4e5f\u5c31\u662f\u5b9a\u4e49\u62bd\u8c61\uff0c\u6ce8\u5165\u4e0d\u540c\u7684\u5b9e\u73b0\uff0c\u4ece\u800c\u8fbe\u5230\u5f88\u597d\u7684\u53ef\u6269\u5c55\u6027\u3002<\/p>\n<p>\u4f18\u70b9\uff1a<\/p>\n<ul>\n<li>\u7ed3\u6784\u6e05\u6670\u660e\u4e86\u3001\u4f7f\u7528\u7b80\u5355\u76f4\u89c2<\/li>\n<li>\u8026\u5408\u5ea6\u76f8\u5bf9\u800c\u8a00\u8f83\u4f4e\uff0c\u6269\u5c55\u65b9\u4fbf<\/li>\n<li>\u64cd\u4f5c\u5c01\u88c5\u4e5f\u66f4\u4e3a\u5f7b\u5e95\uff0c\u6570\u636e\u66f4\u4e3a\u5b89\u5168<\/li>\n<\/ul>\n<p>\u7f3a\u70b9\uff1a<\/p>\n<ul>\n<li>\u968f\u7740\u7b56\u7565\u7684\u589e\u52a0\uff0c\u5b50\u7c7b\u4e5f\u4f1a\u53d8\u5f97\u7e41\u591a<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u901a\u5e38\u5982\u679c\u4e00\u4e2a\u95ee\u9898\u6709\u591a\u4e2a\u89e3\u51b3\u65b9\u6848\u65f6\uff0c\u6700\u7b80\u5355\u7684\u5c31\u662f\u5229\u7528if-else\u6216\u8005switch-case\u65b9\u5f0f\u6839\u636e\u4e0d\u540c\u7684\u60c5\u666f\u9009 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[303],"tags":[],"class_list":["post-1116","post","type-post","status-publish","format-standard","hentry","category-design-pattern"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1116","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1116"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1116\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}