{"id":1124,"date":"2023-03-16T21:24:38","date_gmt":"2023-03-16T13:24:38","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1124"},"modified":"2023-04-29T10:02:18","modified_gmt":"2023-04-29T02:02:18","slug":"design-pattern-template-method-pattern","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/16\/design-pattern-template-method-pattern\/","title":{"rendered":"\u8bbe\u8ba1\u6a21\u5f0f\uff0815\uff09\u6a21\u677f\u65b9\u6cd5\u6a21\u5f0f"},"content":{"rendered":"<p>\u6a21\u677f\u65b9\u6cd5\u6a21\u5f0f\u662f\u7ed3\u6784\u6700\u7b80\u5355\u7684\u884c\u4e3a\u578b\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u4e5f\u662f\u6240\u6709\u6a21\u5f0f\u4e2d\u6700\u4e3a\u5e38\u89c1\u7684\u51e0\u4e2a\u6a21\u5f0f\u4e4b\u4e00\uff0c\u662f\u57fa\u4e8e\u7ee7\u627f\u7684\u4ee3\u7801\u590d\u7528\u7684\u57fa\u672c\u6280\u672f\u3002\u5728\u5176\u7ed3\u6784\u4e2d\u53ea\u5b58\u5728\u7236\u7c7b\u4e0e\u5b50\u7c7b\u4e4b\u95f4\u7684\u7ee7\u627f\u5173\u7cfb\u3002<\/p>\n<p><!-- more --><\/p>\n<h2>\u5b9a\u4e49<\/h2>\n<p>\u5b9a\u4e49\u4e00\u4e2a\u64cd\u4f5c\u4e2d\u7684\u7b97\u6cd5\u7684\u6846\u67b6\uff0c\u800c\u5c06\u4e00\u4e9b\u6b65\u9aa4\u5ef6\u8fdf\u5230\u5b50\u7c7b\u4e2d\uff0c\u4f7f\u5f97\u5b50\u7c7b\u53ef\u4ee5\u4e0d\u6539\u53d8\u4e00\u4e2a\u7b97\u6cd5\u7684\u7ed3\u6784\u5373\u53ef\u91cd\u5b9a\u4e49\u8be5\u7b97\u6cd5\u7684\u67d0\u4e9b\u7279\u5b9a\u6b65\u9aa4\u3002<\/p>\n<h2>\u6a21\u677f\u65b9\u6cd5\u6a21\u5f0f\u4e2d\u7684\u65b9\u6cd5<\/h2>\n<h3>\u6a21\u677f\u65b9\u6cd5<\/h3>\n<p>\u4e00\u4e2a\u6a21\u677f\u65b9\u6cd5\u662f\u5b9a\u4e49\u5728\u62bd\u8c61\u7c7b\u4e2d\u7684\uff0c\u628a\u57fa\u672c\u64cd\u4f5c\u65b9\u6cd5\u7ec4\u5408\u5728\u4e00\u8d77\u5f62\u6210\u4e00\u4e2a\u603b\u7b97\u6cd5\u6216\u4e00\u4e2a\u603b\u884c\u4e3a\u7684\u65b9\u6cd5\u3002\u8fd9\u4e2a\u6a21\u677f\u65b9\u6cd5\u5b9a\u4e49\u5728\u62bd\u8c61\u7c7b\u4e2d\uff0c\u5e76\u7531\u5b50\u7c7b\u4e0d\u52a0\u4ee5\u4fee\u6539\u5730\u5b8c\u5168\u7ee7\u627f\u4e0b\u6765\u3002\u6240\u4ee5\u6a21\u677f\u65b9\u6cd5\u5927\u591a\u4f1a\u5b9a\u4e49\u4e3afinal\u7c7b\u578b\uff0c\u6307\u660e\u4e3b\u8981\u7684\u903b\u8f91\u529f\u80fd\u5728\u5b50\u7c7b\u4e2d\u4e0d\u80fd\u88ab\u91cd\u5199\u3002\u6a21\u677f\u65b9\u6cd5\u662f\u4e00\u4e2a\u5177\u4f53\u65b9\u6cd5\uff0c\u5b83\u7ed9\u51fa\u4e86\u4e00\u4e2a\u9876\u5c42\u903b\u8f91\u6846\u67b6\uff0c\u800c\u903b\u8f91\u7684\u7ec4\u6210\u6b65\u9aa4\u5728\u62bd\u8c61\u7c7b\u4e2d\u53ef\u4ee5\u662f\u5177\u4f53\u65b9\u6cd5\uff0c\u4e5f\u53ef\u4ee5\u662f\u62bd\u8c61\u65b9\u6cd5\u3002\u7531\u4e8e\u6a21\u677f\u65b9\u6cd5\u662f\u5177\u4f53\u65b9\u6cd5\uff0c\u56e0\u6b64\u6a21\u677f\u65b9\u6cd5\u6a21\u5f0f\u4e2d\u7684\u62bd\u8c61\u5c42\u53ea\u80fd\u662f\u62bd\u8c61\u7c7b\uff0c\u800c\u4e0d\u662f\u63a5\u53e3\u3002<\/p>\n<h3>\u57fa\u672c\u65b9\u6cd5<\/h3>\n<p>\uff081\uff09\u62bd\u8c61\u65b9\u6cd5\uff1a\u4e00\u4e2a\u62bd\u8c61\u65b9\u6cd5\u7531\u62bd\u8c61\u7c7b\u58f0\u660e\uff0c\u7531\u5177\u4f53\u5b50\u7c7b\u5b9e\u73b0\u3002\u5728Java\u8bed\u8a00\u91cc\u62bd\u8c61\u65b9\u6cd5\u4ee5abstract\u5173\u952e\u5b57\u6807\u793a\u3002<br \/>\n\uff082\uff09\u94a9\u5b50\u65b9\u6cd5\uff1a\u4e00\u4e2a\u94a9\u5b50\u65b9\u6cd5\u7531\u62bd\u8c61\u7c7b\u58f0\u660e\u5e76\u5b9e\u73b0\uff0c\u800c\u5b50\u7c7b\u4f1a\u52a0\u4ee5\u6269\u5c55\u3002\u5b50\u7c7b\u53ef\u4ee5\u901a\u8fc7\u6269\u5c55\u94a9\u5b50\u65b9\u6cd5\u6765\u5f71\u54cd\u6a21\u677f\u65b9\u6cd5\u7684\u903b\u8f91\u3002<\/p>\n<h2>\u4f7f\u7528\u573a\u666f<\/h2>\n<p>\uff081\uff09\u591a\u4e2a\u5b50\u7c7b\u6709\u516c\u6709\u7684\u65b9\u6cd5\uff0c\u5e76\u4e14\u903b\u8f91\u57fa\u672c\u76f8\u540c\u3002<br \/>\n\uff082\uff09\u91cd\u8981\u3001\u590d\u6742\u7684\u7b97\u6cd5\uff0c\u53ef\u4ee5\u628a\u6838\u5fc3\u7b97\u6cd5\u8bbe\u8ba1\u4e3a\u6a21\u677f\u65b9\u6cd5\uff0c\u5468\u8fb9\u7684\u76f8\u5173\u7ec6\u8282\u529f\u80fd\u7531\u5404\u4e2a\u5b50\u7c7b\u5b9e\u73b0\u3002<br \/>\n\uff083\uff09\u91cd\u6784\u65f6\uff0c\u6a21\u677f\u65b9\u6cd5\u662f\u4e00\u4e2a\u7ecf\u5e38\u4f7f\u7528\u7684\u6a21\u5f0f\uff0c\u628a\u76f8\u540c\u7684\u4ee3\u7801\u62bd\u53d6\u5230\u7236\u7c7b\u4e2d\uff0c\u7136\u540e\u901a\u8fc7\u94a9\u5b50\u65b9\u6cd5\u7ea6\u675f\u5176\u884c\u4e3a\u3002<\/p>\n<h2>\u7b80\u5355\u5b9e\u73b0<\/h2>\n<p>\u4ee5\u7535\u8111\u5f00\u673a\u4e3a\u4f8b\uff0c\u5047\u8bbe\u73b0\u5728\u6709\u4e24\u53f0\u7535\u8111\uff0c\u4e00\u53f0Windows\u7cfb\u7edf\u7535\u8111\uff0c\u4e00\u53f0Mac\u7cfb\u7edf\u7535\u8111\u3002\u4f46\u662f\u5f00\u673a\u6d41\u7a0b\u57fa\u672c\u4e00\u81f4\uff1a\u6b65\u9aa4\u4e3a\u5f00\u542f\u7535\u6e90\u3001\u7cfb\u7edf\u68c0\u67e5\u3001\u52a0\u8f7d\u7cfb\u7edf\u3001\u68c0\u67e5\u662f\u5426\u9700\u8981\u767b\u5f55\u3002<\/p>\n<p>\u62bd\u8c61\u7684 Computer<\/p>\n<pre><code class=\"language-java\">\/**\n * \u62bd\u8c61\u7684 Computer\n *\/\npublic abstract class AbstractComputer {\n    \/\/\u4e0b\u9762\u662f\u62bd\u8c61\u65b9\u6cd5\uff0c\u5b50\u7c7b\u53ef\u4ee5\u8986\u76d6,\u4e0d\u5141\u8bb8\u5916\u90e8\u76f4\u63a5\u8c03\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6240\u4ee5\u7528protected\n    \/**\n     * \u5f00\u542f\u7535\u6e90\n     *\/\n    protected abstract void powerOn();\n\n    \/**\n     * \u68c0\u67e5\u786c\u4ef6\n     *\/\n    protected abstract void checkHardware();\n\n    \/**\n     * \u8f7d\u5165\u64cd\u4f5c\u7cfb\u7edf\n     *\/\n    protected abstract void loadOS();\n\n    \/**\n     * \u767b\u5f55\n     *\/\n    protected abstract void login();\n\n    \/\/\u4e0b\u9762\u662f\u94a9\u5b50\u65b9\u6cd5\uff0c\u58f0\u660e\u5e76\u5b9e\u73b0\n    \/**\n     * \u662f\u5426\u9700\u8981\u767b\u5f55\n     * \n     * @return true\u4e3a\u9700\u8981\u767b\u5f55\n     *\/\n    protected boolean isLogin() {\n        return true;\n    }\n\n    \/\/\u4e0b\u9762\u662f\u6a21\u677f\u65b9\u6cd5\uff0c\u5b9a\u4e49\u4e3afinal\uff0c\u5b50\u7c7b\u4e0d\u80fd\u8986\u76d6\u6b64\u65b9\u6cd5 \n    \/**\n     * \u542f\u52a8\u8ba1\u7b97\u673a\u65b9\u6cd5\uff0c\u6b65\u9aa4\u4e3a\u5f00\u542f\u7535\u6e90\u3001\u7cfb\u7edf\u68c0\u67e5\u3001\u52a0\u8f7d\u7cfb\u7edf\u3001\u68c0\u67e5\u662f\u5426\u767b\u5f55\u3002\n     *\/\n    public final void startUp() {\n        System.out.println(&quot;--------\u5f00\u673a START--------&quot;);\n        powerOn();\n        checkHardware();\n        loadOS();\n        if (isLogin()) {\n            login();\n        }\n        System.out.println(&quot;-------- \u5f00\u673a END --------&quot;);\n    }\n}<\/code><\/pre>\n<p>Windows\u7cfb\u7edf\u7535\u8111\uff08\u4e0d\u9700\u767b\u5f55\uff09\uff1a<\/p>\n<pre><code class=\"language-java\">\/**\n * Windows\u7cfb\u7edf\u7535\u8111\n *\/\npublic class WindowsComputer extends AbstractComputer {\n\n    @Override\n    protected void powerOn() {\n        System.out.println(&quot;Windows\u7535\u8111\u5f00\u542f\u7535\u6e90&quot;);\n    }\n\n    @Override\n    protected void checkHardware() {\n        System.out.println(&quot;Windows\u7535\u8111\u68c0\u67e5\u786c\u4ef6&quot;);\n    }\n\n    @Override\n    protected void loadOS() {\n        System.out.println(&quot;Windows\u7535\u8111\u8f7d\u5165\u64cd\u4f5c\u7cfb\u7edf&quot;);\n    }\n\n    @Override\n    protected void login() {\n\n    }\n\n    @Override\n    protected boolean isLogin() {\n        return false; \/\/\u8fd4\u56defalse\uff0c\u4e0d\u9700\u767b\u5f55\n    }\n}<\/code><\/pre>\n<p>Mac\u7cfb\u7edf\u7535\u8111\uff08\u9700\u767b\u5f55\uff09\uff1a<\/p>\n<pre><code class=\"language-java\">\/**\n * Mac\u7cfb\u7edf\u7535\u8111\n *\/\npublic class MacComputer extends AbstractComputer {\n\n    @Override\n    protected void powerOn() {\n        System.out.println(&quot;Mac\u7535\u8111\u5f00\u542f\u7535\u6e90&quot;);\n    }\n\n    @Override\n    protected void checkHardware() {\n        System.out.println(&quot;Mac\u7535\u8111\u68c0\u67e5\u786c\u4ef6&quot;);\n    }\n\n    @Override\n    protected void loadOS() {\n        System.out.println(&quot;Mac\u7535\u8111\u8f7d\u5165\u64cd\u4f5c\u7cfb\u7edf&quot;);\n    }\n\n    @Override\n    protected void login() {\n        System.out.println(&quot;Mac\u7535\u8111\u767b\u5f55&quot;);\n    }\n}<\/code><\/pre>\n<p>\u8c03\u7528\uff1a<\/p>\n<pre><code class=\"language-java\">public class Client {\n    public static void main(String[] args) {\n        AbstractComputer comp = new WindowsComputer();\n        comp.startUp();\n\n        comp = new MacComputer();\n        comp.startUp();\n    }\n}<\/code><\/pre>\n<p>\u7ed3\u679c\uff1a<\/p>\n<pre><code>--------\u5f00\u673a START--------\nWindows\u7535\u8111\u5f00\u542f\u7535\u6e90\nWindows\u7535\u8111\u68c0\u67e5\u786c\u4ef6\nWindows\u7535\u8111\u8f7d\u5165\u64cd\u4f5c\u7cfb\u7edf\n-------- \u5f00\u673a END --------\n--------\u5f00\u673a START--------\nMac\u7535\u8111\u5f00\u542f\u7535\u6e90\nMac\u7535\u8111\u68c0\u67e5\u786c\u4ef6\nMac\u7535\u8111\u8f7d\u5165\u64cd\u4f5c\u7cfb\u7edf\nMac\u7535\u8111\u767b\u5f55\n-------- \u5f00\u673a END --------<\/code><\/pre>\n<h2>Android\u6e90\u7801\u4e2d\u7684\u6a21\u677f\u65b9\u6cd5\u6a21\u5f0f<\/h2>\n<ul>\n<li>AsyncTask<\/li>\n<\/ul>\n<p>\u5728\u4f7f\u7528<code>AsyncTask<\/code>\u65f6\uff0c\u6211\u4eec\u90fd\u77e5\u9053\u628a\u8017\u65f6\u64cd\u4f5c\u653e\u5230<code>doInBackground(Params\u2026 params)<\/code>\u4e2d\uff0c\u5728<code>doInBackground<\/code>\u4e4b\u524d\uff0c\u5982\u679c\u60f3\u505a\u4e00\u4e9b\u521d\u59cb\u5316\u64cd\u4f5c\uff0c\u53ef\u4ee5\u628a\u5b9e\u73b0\u5199\u5728<code>onPreExecute<\/code>\u4e2d\uff0c\u5f53<code>doInBackground<\/code>\u6267\u884c\u5b8c\u540e\u4f1a\u6267\u884c<code>onPostExecute<\/code>\u65b9\u6cd5\uff0c\u800c\u6211\u4eec\u53ea\u9700\u8981\u6784\u5efa<code>AsyncTask<\/code>\u5bf9\u8c61\uff0c\u7136\u540e\u6267\u884c<code>execute<\/code>\u65b9\u6cd5\u3002<\/p>\n<ul>\n<li>Activity\u7684\u751f\u547d\u5468\u671f<\/li>\n<\/ul>\n<p><code>ActivityThread<\/code>\u7684main\u51fd\u6570\u88ab\u8c03\u7528\u540e\uff0c\u4f9d\u6b21\u6267\u884c<code>Activity<\/code>\u7684<code>onCreate<\/code>\u3001<code>onStart<\/code>\u3001<code>onResume<\/code>\u51fd\u6570\uff0c\u7528\u6237\u901a\u5e38\u5728<code>Activity<\/code>\u7684\u5b50\u7c7b\u4e2d\u8986\u5199<code>onCreate<\/code>\u65b9\u6cd5\uff0c\u5e76\u4e14\u5728\u8be5\u65b9\u6cd5\u4e2d\u8c03\u7528<code>setContentView<\/code>\u6765\u8bbe\u7f6e\u5e03\u5c40\u3002<\/p>\n<h2>\u533a\u522b<\/h2>\n<p>\uff081\uff09\u5de5\u5382\u65b9\u6cd5\u662f\u6a21\u677f\u65b9\u6cd5\u7684\u4e00\u79cd\u7279\u6b8a\u7248\u672c<br \/>\n\uff082\uff09\u7b56\u7565\u6a21\u5f0f\u548c\u6a21\u677f\u65b9\u6cd5\u6a21\u5f0f\u90fd\u662f\u5c01\u88c5\u7b97\u6cd5\uff0c\u4e00\u4e2a\u7528\u7ec4\u5408\uff0c\u4e00\u4e2a\u7528\u7ee7\u627f<br \/>\n\uff083\uff09\u7b56\u7565\u6a21\u5f0f\u548c\u6a21\u677f\u6a21\u5f0f\u901a\u5e38\u53ef\u4ee5\u4e92\u76f8\u66ff\u6362\u3002\u5b83\u4eec\u90fd\u50cf\u8bd5\u5377\uff0c\u7b56\u7565\u6a21\u5f0f\u662f\u9009\u62e9\u9898\uff0c\u6a21\u677f\u6a21\u5f0f\u662f\u586b\u7a7a\u9898<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u6a21\u677f\u65b9\u6cd5\u6a21\u5f0f\u75284\u4e2a\u5b57\u6982\u62ec\u5c31\u662f\uff1a\u6d41\u7a0b\u5c01\u88c5\u3002\u4e5f\u5c31\u662f\u628a\u67d0\u4e2a\u56fa\u5b9a\u7684\u6d41\u7a0b\u5c01\u88c5\u5230\u4e00\u4e2afinal\u65b9\u6cd5\u4e2d\uff0c\u5e76\u4e14\u8ba9\u5b50\u7c7b\u80fd\u591f\u5b9a\u5236\u8fd9\u4e2a\u6d41\u7a0b\u4e2d\u7684\u67d0\u4e9b\u6216\u8005\u6240\u6709\u6b65\u9aa4\uff0c\u8fd9\u5c31\u8981\u6c42\u7236\u7c7b\u63d0\u53d6\u516c\u7528\u7684\u4ee3\u7801\uff0c\u63d0\u5347\u4ee3\u7801\u7684\u590d\u7528\u7387\uff0c\u540c\u65f6\u5e26\u6765\u4e86\u66f4\u597d\u7684\u53ef\u6269\u5c55\u6027\u3002<\/p>\n<ul>\n<li>\u4f18\u70b9<\/li>\n<\/ul>\n<p>\uff081\uff09\u5c01\u88c5\u4e0d\u53d8\u90e8\u5206\uff0c\u6269\u5c55\u53ef\u53d8\u90e8\u5206<br \/>\n\uff082\uff09\u63d0\u53d6\u516c\u5171\u90e8\u5206\u4ee3\u7801\uff0c\u4fbf\u4e8e\u7ef4\u62a4<\/p>\n<ul>\n<li>\u7f3a\u70b9<\/li>\n<\/ul>\n<p>\u9700\u8981\u4e3a\u6bcf\u4e00\u4e2a\u57fa\u672c\u65b9\u6cd5\u7684\u4e0d\u540c\u5b9e\u73b0\u63d0\u4f9b\u4e00\u4e2a\u5b50\u7c7b\uff0c\u5982\u679c\u7236\u7c7b\u4e2d\u53ef\u53d8\u7684\u57fa\u672c\u65b9\u6cd5\u592a\u591a\uff0c\u5c06\u4f1a\u5bfc\u81f4\u7c7b\u7684\u4e2a\u6570\u589e\u52a0\uff0c\u7cfb\u7edf\u66f4\u52a0\u5e9e\u5927\uff0c\u8bbe\u8ba1\u4e5f\u66f4\u52a0\u62bd\u8c61\uff0c\u6b64\u65f6\uff0c\u53ef\u7ed3\u5408\u6865\u63a5\u6a21\u5f0f\u6765\u8fdb\u884c\u8bbe\u8ba1\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6a21\u677f\u65b9\u6cd5\u6a21\u5f0f\u662f\u7ed3\u6784\u6700\u7b80\u5355\u7684\u884c\u4e3a\u578b\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u4e5f\u662f\u6240\u6709\u6a21\u5f0f\u4e2d\u6700\u4e3a\u5e38\u89c1\u7684\u51e0\u4e2a\u6a21\u5f0f\u4e4b\u4e00\uff0c\u662f\u57fa\u4e8e\u7ee7\u627f\u7684\u4ee3\u7801\u590d\u7528\u7684\u57fa\u672c\u6280\u672f [&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-1124","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\/1124","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=1124"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1124\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}