{"id":1119,"date":"2023-03-16T21:20:36","date_gmt":"2023-03-16T13:20:36","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1119"},"modified":"2023-04-29T11:17:37","modified_gmt":"2023-04-29T03:17:37","slug":"design-pattern-interpreter-pattern","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/16\/design-pattern-interpreter-pattern\/","title":{"rendered":"\u8bbe\u8ba1\u6a21\u5f0f\uff0810\uff09\u89e3\u91ca\u5668\u6a21\u5f0f"},"content":{"rendered":"<p>\u89e3\u91ca\u5668\u6a21\u5f0f\u662f\u4e00\u79cd\u7528\u7684\u6bd4\u8f83\u5c11\u7684\u884c\u4e3a\u578b\u6a21\u5f0f\uff0c\u5176\u63d0\u4f9b\u4e86\u4e00\u79cd\u89e3\u91ca\u8bed\u8a00\u7684\u8bed\u6cd5\u6216\u8868\u8fbe\u5f0f\u7684\u65b9\u5f0f\u3002\u4f46\u662f\u5b83\u7684\u4f7f\u7528\u573a\u666f\u786e\u5b9e\u5f88\u5e7f\u6cdb\uff0c\u53ea\u662f\u56e0\u4e3a\u6211\u4eec\u81ea\u5df1\u5f88\u5c11\u4f1a\u53bb\u6784\u9020\u4e00\u4e2a\u8bed\u8a00\u7684\u6587\u6cd5\uff0c\u6240\u4ee5\u4f7f\u7528\u8f83\u5c11\u3002<\/p>\n<p><!-- more --><\/p>\n<h2>\u5b9a\u4e49<\/h2>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u8bed\u8a00\uff0c\u5b9a\u4e49\u5b83\u7684\u6587\u6cd5\u7684\u4e00\u79cd\u8868\u793a\uff0c\u5e76\u5b9a\u4e49\u4e00\u4e2a\u89e3\u91ca\u5668\uff0c\u8be5\u89e3\u91ca\u5668\u4f7f\u7528\u8be5\u8868\u793a\u6765\u89e3\u91ca\u8bed\u8a00\u4e2d\u7684\u53e5\u5b50\uff08\u5176\u4e2d\u8bed\u8a00\u5c31\u662f\u6211\u4eec\u9700\u8981\u89e3\u91ca\u7684\u5bf9\u8c61\uff0c\u6587\u6cd5\u5c31\u662f\u8fd9\u4e2a\u8bed\u8a00\u7684\u89c4\u5f8b\uff0c\u89e3\u91ca\u5668\u5c31\u662f\u7ffb\u8bd1\u673a\uff0c\u901a\u8fc7\u6587\u6cd5\u6765\u7ffb\u8bd1\u8bed\u8a00\uff09\u3002<\/p>\n<h2>\u4f7f\u7528\u573a\u666f<\/h2>\n<ul>\n<li>\u5982\u679c\u67d0\u4e2a\u7b80\u5355\u7684\u8bed\u8a00\u9700\u8981\u89e3\u91ca\u6267\u884c\u800c\u4e14\u53ef\u4ee5\u5c06\u8be5\u8bed\u8a00\u4e2d\u7684\u8bed\u53e5\u8868\u793a\u4e3a\u4e00\u4e2a\u62bd\u8c61\u7684\u8bed\u6cd5\u6811\u65f6\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u89e3\u91ca\u5668\u6a21\u5f0f\u3002 <\/li>\n<li>\u5728\u67d0\u4e9b\u7279\u5b9a\u7684\u9886\u57df\u51fa\u73b0\u4e0d\u65ad\u91cd\u590d\u7684\u95ee\u9898\u65f6\uff0c\u53ef\u4ee5\u5c06\u8be5\u9886\u57df\u7684\u95ee\u9898\u8f6c\u5316\u4e3a\u4e00\u79cd\u8bed\u6cd5\u89c4\u5219\u4e0b\u7684\u8bed\u53e5\uff0c\u7136\u540e\u6784\u5efa\u89e3\u91ca\u5668\u6765\u89e3\u91ca\u8be5\u8bed\u53e5\u3002<\/li>\n<\/ul>\n<h2>\u7b80\u5355\u5b9e\u73b0<\/h2>\n<ul>\n<li>\u6211\u4eec\u4f7f\u7528\u89e3\u91ca\u5668\u6a21\u5f0f\u5bf9\u201cm+n+p\u201d\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u8fdb\u884c\u89e3\u91ca\uff0c\u90a3\u4e48\u4ee3\u8868\u6570\u5b57\u7684m\u3001n\u548cp\u5c31\u53ef\u4ee5\u770b\u6210\u7ec8\u7ed3\u7b26\u53f7\uff0c\u800c\u201c+\u201d\u8fd9\u4e2a\u8fd0\u7b97\u7b26\u53f7\u53ef\u4ee5\u5f53\u505a\u975e\u7ec8\u7ed3\u7b26\u53f7\u3002 <\/li>\n<li><code>TerminalExpression<\/code>\uff1a\u7ec8\u7ed3\u7b26\u8868\u8fbe\u5f0f\uff0c\u5b9e\u73b0\u6587\u6cd5\u4e2d\u4e0e\u7ec8\u7ed3\u7b26\u6709\u5173\u7684\u89e3\u91ca\u64cd\u4f5c\u3002\u6587\u6cd5\u4e2d\u6bcf\u4e2a\u7ec8\u7ed3\u7b26\u90fd\u6709\u4e00\u4e2a\u5177\u4f53\u7684\u7ec8\u7ed3\u8868\u8fbe\u5f0f\u4e0e\u4e4b\u5bf9\u5e94\u3002 <\/li>\n<li><code>NonterminalExpression<\/code>\uff1a\u975e\u7ec8\u7ed3\u7b26\u8868\u8fbe\u5f0f\uff0c\u5b9e\u73b0\u6587\u6cd5\u4e2d\u4e0e\u975e\u7ec8\u7ed3\u7b26\u6709\u5173\u7684\u89e3\u91ca\u64cd\u4f5c\u3002\u975e\u7ec8\u7ed3\u7b26\u8868\u8fbe\u5f0f\u6839\u636e\u903b\u8f91\u7684\u590d\u6742\u7a0b\u5ea6\u800c\u589e\u52a0\uff0c\u539f\u5219\u4e0a\u6bcf\u4e2a\u6587\u6cd5\u89c4\u5219\u90fd\u5bf9\u5e94\u4e00\u4e2a\u975e\u7ec8\u7ed3\u7b26\u8868\u8fbe\u5f0f\u3002<\/li>\n<\/ul>\n<p>\u62bd\u8c61\u7684\u7b97\u6570\u8fd0\u7b97\u89e3\u91ca\u5668<\/p>\n<pre><code class=\"language-java\">public abstract class ArithemticExpression {\n    \/**\n     * \u62bd\u8c61\u7684\u89e3\u6790\u65b9\u6cd5 \n     * \u5177\u4f53\u7684\u89e3\u6790\u903b\u8f91\u7531\u5177\u4f53\u7684\u5b50\u7c7b\u5b9e\u73b0\n     * \n     * @return \u89e3\u6790\u5f97\u5230\u5177\u4f53\u7684\u503c\n     *\/\n    public abstract int interpreter();\n}<\/code><\/pre>\n<p>\u6570\u5b57\u89e3\u91ca\u5668<\/p>\n<pre><code class=\"language-java\">public class NumExpression extends ArithemticExpression {\n\n    private int num;\n\n    public NumExpression(int num) {\n        this.num = num;\n    }\n\n    @Override\n    public int interpreter() {\n        return num;\n    }\n}<\/code><\/pre>\n<p>\u8fd0\u7b97\u7b26\u53f7\u89e3\u91ca\u5668<\/p>\n<pre><code class=\"language-java\">public abstract class OperatorExpression extends ArithemticExpression {\n\n    protected ArithemticExpression exp1, exp2;\n\n    public OperatorExpression(ArithemticExpression exp1, ArithemticExpression exp2) {\n        this.exp1 = exp1;\n        this.exp2 = exp2;\n    }\n}<\/code><\/pre>\n<p>\u5177\u4f53\u7684\u52a0\u6cd5\u8fd0\u7b97\u7b26\u89e3\u91ca\u5668<\/p>\n<pre><code class=\"language-java\">public class AdditionExpression extends OperatorExpression {\n\n    public AdditionExpression(ArithemticExpression exp1,\n            ArithemticExpression exp2) {\n        super(exp1, exp2);\n    }\n\n    @Override\n    public int interpreter() {\n        return exp1.interpreter() + exp2.interpreter();\n    }\n\n}<\/code><\/pre>\n<p>\u5904\u7406\u89e3\u91ca\u5668<\/p>\n<pre><code class=\"language-java\">public class Calculator {\n\n    \/\/\u58f0\u660e\u4e00\u4e2aStack\u6808\u50a8\u5b58\u5e76\u64cd\u4f5c\u6240\u6709\u76f8\u5173\u7684\u89e3\u91ca\u5668\n    private Stack&lt;ArithemticExpression&gt; mExpStack = new Stack&lt;ArithemticExpression&gt;();\n\n    public Calculator(String expression) {\n        \/\/\u58f0\u660e\u4e24\u4e2aArithemticExpression\u7c7b\u578b\u7684\u4e34\u65f6\u53d8\u91cf\uff0c\u50a8\u5b58\u8fd0\u7b97\u7b26\u5de6\u53f3\u4e24\u8fb9\u7684\u6570\u5b57\u89e3\u91ca\u5668\n        ArithemticExpression exp1,exp2;\n\n        \/\/\u6839\u636e\u7a7a\u683c\u5206\u5272\u8868\u8fbe\u5f0f\u5b57\u7b26\u4e32(\u6bd4\u59821 + 2 + 3 + 4)\n        String[] elements = expression.split(&quot; &quot;);\n\n        \/*\n         * \u904d\u5386\u8868\u8fbe\u5f0f\u5143\u7d20\u6570\u7ec4\n         *\/\n        for(int i = 0; i &lt; elements.length; i++){\n            \/*\n             * \u5224\u65ad\u8fd0\u7b97\u7b26\u53f7\n             *\/\n            switch (elements[i].charAt(0)) {\n            case &#039;+&#039;:\n                \/\/\u5982\u679c\u662f\u52a0\u53f7\uff0c\u5219\u5c06\u6808\u4e2d\u7684\u89e3\u91ca\u5668\u5f39\u51fa\u4f5c\u4e3a\u8fd0\u7b97\u7b26\u53f7\u5de6\u8fb9\u7684\u89e3\u91ca\u5668\n                exp1 = mExpStack.pop();\n                \/\/\u540c\u65f6\u5c06\u8fd0\u7b97\u7b26\u53f7\u6570\u7ec4\u4e0b\u6807\u7684\u4e0b\u4e00\u4e2a\u5143\u7d20\u6784\u9020\u4e3a\u4e00\u4e2a\u6570\u5b57\u89e3\u91ca\u5668\n                exp2 = new NumExpression(Integer.parseInt(elements[++i]));\n                \/\/\u901a\u8fc7\u4e0a\u9762\u7684\u4e24\u4e2a\u6570\u5b57\u89e3\u91ca\u5668\u6784\u9020\u52a0\u6cd5\u8fd0\u7b97\u89e3\u91ca\u5668 \n                mExpStack.push(new AdditionExpression(exp1, exp2));\n                break;\n\n            default:\n                \/*\n                 * \u5982\u679c\u4e3a\u6570\u5b57\uff0c\u76f4\u63a5\u6784\u9020\u6570\u5b57\u89e3\u91ca\u5668\u5e76\u538b\u5165\u6808\n                 *\/\n                mExpStack.push(new NumExpression(Integer.valueOf(elements[i])));\n                break;\n            }\n        }\n    }\n\n    \/**\n     * \u8ba1\u7b97\u7ed3\u679c\n     * \n     * @return \u6700\u7ec8\u7684\u8ba1\u7b97\u7ed3\u679c\n     *\/\n    public int calculate(){\n        return mExpStack.pop().interpreter();\n    }\n}<\/code><\/pre>\n<p>\u8c03\u7528<\/p>\n<pre><code class=\"language-java\">public class Client {\n    public static void main(String[] args) {\n        Calculator c = new Calculator(&quot;22 + 553 + 83 + 5&quot;);\n        System.out.println(&quot;\u8ba1\u7b97\u7ed3\u679c\uff1a&quot; + c.calculate());\n    }\n}<\/code><\/pre>\n<p>\u7ed3\u679c\uff1a<\/p>\n<pre><code>\u8ba1\u7b97\u7ed3\u679c\uff1a663<\/code><\/pre>\n<p>\u5982\u679c\u76f8\u52a0\u5982\u51cf\u6cd5\u7684\u64cd\u4f5c\uff0c\u5728Calculator\u4e2d\u52a0\u5165\u76f8\u5e94\u5224\u65ad\u5373\u53ef\uff1a<\/p>\n<pre><code class=\"language-java\">public class SubtractionExpression extends OperatorExpression{\n\n    public SubtractionExpression(ArithemticExpression exp1,\n            ArithemticExpression exp2) {\n        super(exp1, exp2);\n    }\n\n    @Override\n    public int interpreter() {\n        return exp1.interpreter() - exp2.interpreter();\n    }\n\n}<\/code><\/pre>\n<p>Calculator\u4e2d\u52a0\u5165\uff1a<\/p>\n<pre><code class=\"language-java\">case &#039;-&#039;:\n    exp1 = mExpStack.pop();\n    exp2 = new NumExpression(Integer.parseInt(elements[++i]));\n    mExpStack.push(new SubtractionExpression(exp1, exp2));\n    break;<\/code><\/pre>\n<p>\u4ece\u4e0a\u9762\u53ef\u4ee5\u770b\u51fa\u89e3\u91ca\u5668\u6a21\u5f0f\u5f88\u7075\u6d3b\uff0c\u4ed6\u5c06\u590d\u6742\u95ee\u9898\u53ef\u4ee5\u7b80\u5355\u5316\u3001\u6a21\u5757\u5316\u3001\u5206\u79bb\u5b9e\u73b0\u3001\u89e3\u91ca\u6267\u884c\u3002<\/p>\n<h2>Android\u6e90\u7801\u4e2d\u7684\u6a21\u5f0f\u5b9e\u73b0<\/h2>\n<ul>\n<li>PackageParser<\/li>\n<\/ul>\n<p><code>PackageParser<\/code>\u662f\u5bf9<code>AndroidManifest.xml<\/code>\u914d\u7f6e\u6587\u4ef6\u8fdb\u884c\u8bfb\u53d6\u7684<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\uff081\uff09\u4f18\u70b9<\/p>\n<p>\u6700\u5927\u7684\u4f18\u70b9\u4f7f\u5176\u7075\u6d3b\u7684\u6269\u5c55\u6027\uff0c\u5f53\u6211\u4eec\u60f3\u5bf9\u6587\u6cd5\u89c4\u5219\u8fdb\u884c\u6269\u5c55\u5ef6\u4f38\u65f6\uff0c\u53ea\u9700\u8981\u589e\u52a0\u76f8\u5e94\u7684\u975e\u7ec8\u7ed3\u7b26\u89e3\u91ca\u5668\uff0c\u5e76\u5728\u6784\u5efa\u62bd\u8c61\u8bed\u6cd5\u6811\u65f6\uff0c\u4f7f\u7528\u5230\u65b0\u589e\u7684\u89e3\u91ca\u5668\u5bf9\u8c61\u8fdb\u884c\u5177\u4f53\u7684\u89e3\u91ca\u5373\u53ef\uff0c\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<p>\uff082\uff09\u7f3a\u70b9<\/p>\n<ul>\n<li>\u6bcf\u4e2a\u8bed\u6cd5\u90fd\u8981\u4ea7\u751f\u4e00\u4e2a\u975e\u7ec8\u7ed3\u7b26\u8868\u8fbe\u5f0f\uff0c\u8bed\u6cd5\u89c4\u5219\u6bd4\u8f83\u590d\u6742\u65f6\uff0c\u5c31\u53ef\u80fd\u4ea7\u751f\u5927\u91cf\u7684\u7c7b\u6587\u4ef6\uff0c\u4e3a\u7ef4\u62a4\u5e26\u6765\u4e86\u975e\u5e38\u591a\u7684\u9ebb\u70e6\u3002 <\/li>\n<li>\u89e3\u91ca\u5668\u6a21\u5f0f\u7531\u4e8e\u4f7f\u7528\u4e86\u5927\u91cf\u7684\u5faa\u73af\u548c\u9012\u5f52\uff0c\u6548\u7387\u662f\u4e2a\u95ee\u9898\uff0c\u7279\u522b\u662f\u7528\u4e8e\u89e3\u6790\u590d\u6742\u3001\u5197\u957f\u7684\u8bed\u6cd5\u65f6\uff0c\u6548\u7387\u662f\u96be\u4ee5\u5fcd\u53d7\u7684\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u89e3\u91ca\u5668\u6a21\u5f0f\u662f\u4e00\u79cd\u7528\u7684\u6bd4\u8f83\u5c11\u7684\u884c\u4e3a\u578b\u6a21\u5f0f\uff0c\u5176\u63d0\u4f9b\u4e86\u4e00\u79cd\u89e3\u91ca\u8bed\u8a00\u7684\u8bed\u6cd5\u6216\u8868\u8fbe\u5f0f\u7684\u65b9\u5f0f\u3002\u4f46\u662f\u5b83\u7684\u4f7f\u7528\u573a\u666f\u786e\u5b9e\u5f88\u5e7f\u6cdb\uff0c [&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-1119","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\/1119","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=1119"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1119\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}