{"id":1082,"date":"2019-10-26T06:42:34","date_gmt":"2019-10-25T22:42:34","guid":{"rendered":"http:\/\/www.rain1024.com\/?p=1082"},"modified":"2023-08-07T20:57:24","modified_gmt":"2023-08-07T12:57:24","slug":"1082","status":"publish","type":"post","link":"http:\/\/rain1024.com\/index.php\/2019\/10\/26\/1082\/","title":{"rendered":"\u300aHeadFirst\u8bbe\u8ba1\u6a21\u5f0f\u300b\u7b2c\u516d\u7ae0\u547d\u4ee4\u6a21\u5f0f-\u8bfb\u4e66\u7b14\u8bb0"},"content":{"rendered":"<h1>\u300aHeadFirst\u8bbe\u8ba1\u6a21\u5f0f\u300b\u7b2c\u516d\u7ae0\u547d\u4ee4\u6a21\u5f0f-\u8bfb\u4e66\u7b14\u8bb0<\/h1>\n<h3>\u6848\u4f8b\u4ee3\u7801\u94fe\u63a5\uff1ahttps:\/\/github.com\/rainweb521\/My-tutorial\/tree\/master\/Design_patterns<\/h3>\n<h2>1.\u80cc\u666f<\/h2>\n<h3>1.1 \u7528\u9910\u5385\u6765\u5206\u6790<\/h3>\n<blockquote><p>\n  \u4ece\u9910\u5385\u5f00\u59cb\u8bf4\u8d77\uff0c\u4ee5\u6b64\u6765\u89e3\u91ca\u547d\u4ee4\u6a21\u5f0f\u7684\u6d41\u7a0b\u3002<\/p>\n<p>  \u8ba2\u5355\u5c01\u88c5\u4e86\u51c6\u5907\u9910\u70b9\u7684\u8bf7\u6c42<\/p>\n<p>  \u5973\u62db\u5f85\u63a5\u53d7\u8ba2\u5355\uff0c\u7136\u540e\u8c03\u7528\u8ba2\u5355\u7684orderUp()\u65b9\u6cd5<\/p>\n<p>  \u53a8\u5e08\u51c6\u5907\u9910\u70b9<\/p>\n<p>  \u8fd9\u6837\uff0c\u987e\u5ba2\u548c\u5973\u62db\u5f85\u662f\u89e3\u8026\u7684\uff0c\u4e00\u5929\u5185\uff0c\u4e0d\u540c\u7684\u987e\u5ba2\u6709\u4e0d\u540c\u7684\u8ba2\u5355\uff0c\u5973\u62db\u5f85\u77e5\u9053\u6240\u6709\u7684\u8ba2\u5355\u90fd\u652f\u6301orderUp()\u65b9\u6cd5\uff0c\u6bcf\u6b21\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\u5c31\u662f\u4e86\u3002<\/p>\n<p>  \u5973\u62db\u5f85\u548c\u53a8\u5e08\u4e5f\u662f\u89e3\u8026\u7684\uff0c\u8ba2\u5355\u5c01\u88c5\u4e86\u9910\u70b9\u7684\u7ec6\u8282\uff0c\u5979\u53ea\u8c03\u7528\u6bcf\u4e2a\u8ba2\u5355\u7684\u65b9\u6cd5\u5373\u53ef\uff0c\u800c\u53a8\u5e08\u4e5f\u662f\u6839\u636e\u8ba2\u5355\u53bb\u505a\u5bf9\u5e94\u7684\u9910\u70b9\u3002\u4ed6\u4eec\u4e4b\u95f4\u4e0d\u9700\u8981\u76f4\u63a5\u6c9f\u901a\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"http:\/\/cos.rain1024.com\/markdown\/image-20191023073214000.png\" alt=\"\" \/><\/p>\n<h3>1.2 \u56de\u5230\u9065\u63a7\u5668<\/h3>\n<p>\u8fd9\u4e00\u7ae0\u9700\u8981\u5b9e\u73b0\u4e00\u4e2a\u9065\u63a7\u5668\uff0c\u9065\u63a7\u5668\u4e0a\u53ea\u6709\u4e03\u4e2a\u63d2\u69fd\uff0c\u800c\u5382\u5546\u6709\u5f88\u591a\u7684\u7c7b\uff0c\u5e76\u4e14\u4ee5\u540e\u8fd8\u4f1a\u65b0\u589e\uff0c\u6240\u4ee5\u9700\u8981\u6839\u636e\u7c7b\u7684\u7279\u5f81\u53bb\u62bd\u53d6\u5171\u540c\u70b9\uff0c\u8fd9\u65f6\u5c31\u7528\u5230\u4e86\u547d\u4ee4\u6a21\u5f0f\uff0c\u5c06\"\u52a8\u4f5c\u7684\u8bf7\u6c42\u8005\"\u4ece\"\u52a8\u4f5c\u7684\u6267\u884c\u8005\u201c\u5bf9\u8c61\u4e2d\u89e3\u8026\u3002<\/p>\n<p>\u5229\u7528\u547d\u4ee4\u5bf9\u8c61\uff0c\u628a\u8bf7\u6c42\uff08\u4f8b\u5982\u6253\u5f00\u7535\u706f\uff09\u5c01\u88c5\u6210\u4e00\u4e2a\u7279\u5b9a\u5bf9\u8c61\uff08\u4f8b\u5982\u5ba2\u5385\u7535\u706f\u5bf9\u8c61\uff09\u3002\u6240\u4ee5\uff0c\u5982\u679c\u5bf9\u6bcf\u4e2a\u6309\u94ae\u90fd\u5b58\u50a8\u4e00\u4e2a\u547d\u4ee4\u5bf9\u8c61\uff0c\u90a3\u4e48\u5f53\u6309\u94ae\u88ab\u6309\u4e0b\u7684\u65f6\u5019\uff0c\u5c31\u53ef\u4ee5\u8bf7\u547d\u4ee4\u5bf9\u8c61\u505a\u76f8\u5173\u7684\u5de5\u4f5c\u3002\u9065\u63a7\u5668\u5e76\u4e0d\u9700\u8981\u77e5\u9053\u5de5\u4f5c\u5185\u5bb9\u662f\u4ec0\u4e48\uff0c\u53ea\u8981\u6709\u4e2a\u547d\u4ee4\u5bf9\u8c61\u80fd\u548c\u6b63\u786e\u7684\u5bf9\u8c61\u6c9f\u901a\uff0c\u628a\u4e8b\u60c5\u505a\u597d\u5c31\u53ef\u4ee5\u4e86\u3002\u6240\u4ee5\uff0c\u9065\u63a7\u5668\u548c\u7535\u706f\u5bf9\u8c61\u89e3\u8026\u4e86\u3002<br \/>\n\u7531\u4e8e\u5bf9\u8c61\u4e4b\u95f4\u662f\u5982\u6b64\u7684\u89e3\u8026\uff0c\u8981\u63cf\u8ff0\u8fd9\u4e2a\u6a21\u5f0f\u5b9e\u9645\u7684\u5de5\u4f5c\u5e76\u4e0d\u5bb9\u6613\u3002<br \/>\n\u4f7f\u7528\u8fd9\u4e2a\u6a21\u5f0f\uff0c\u6211\u4eec\u80fd\u591f\u521b\u5efa\u4e00\u4e2aAPI\uff0c\u5c06\u8fd9\u4e9b\u547d\u4ee4\u5bf9\u8c61\u52a0\u8f7d\u5230\u6309\u94ae\u63d2\u69fd\uff0c\u8ba9\u9065\u63a7\u5668\u7684\u4ee3\u7801\u5c3d\u91cf\u4fdd\u6301\u7b80\u5355\u3002\u800c\u628a\u5bb6\u7535\u81ea\u52a8\u5316\u7684\u5de5\u4f5c\u548c\u8fdb\u884c\u8be5\u5de5\u4f5c\u7684\u5bf9\u8c61\u4e00\u8d77\u5c01\u88c5\u5728\u547d\u4ee4\u5bf9\u8c61\u4e2d\u3002<\/p>\n<h2>2.\u5f00\u59cb\u8bbe\u8ba1<\/h2>\n<h3>2.1\u7b2c\u4e00\u4e2a\u547d\u4ee4\u5bf9\u8c61<\/h3>\n<p>\u5b9e\u73b0\u547d\u4ee4\u63a5\u53e3\uff0c\u8ba9\u6240\u6709\u7684\u547d\u4ee4\u5bf9\u8c61\u5b9e\u73b0\u76f8\u540c\u7684\u5305\u542b\u4e00\u4e2a\u65b9\u6cd5\u7684\u63a5\u53e3\u3002<\/p>\n<pre><code>public interface Command {\n    public void execute();\n}\n\n<\/code><\/pre>\n<p>\u5b9e\u73b0\u6253\u5f00\u7535\u706f\u7684\u547d\u4ee4<\/p>\n<pre><code>public class LightOnCommand implements Command {\n    Light light;\n\/\/    \u7ed9\u6784\u9020\u5668\u4f20\u5165\u67d0\u4e2a\u7535\u706f\uff0c\u65b9\u4fbf\u4ee5\u540e\u8c03\u7528execute\n    public LightOnCommand(Light light){\n        this.light = light;\n    }\n\/\/    \u8c03\u7528\u63a5\u53d7\u5bf9\u8c61\u7684on\u65b9\u6cd5\n    public void execute() {\n        light.on();\n    }\n}\n<\/code><\/pre>\n<p>\u706f\u7684\u7c7b<\/p>\n<pre><code>public class Light {\n        private String name;\n    public Light(String living_room) {\n        this.name = living_room;\n    }\n    public Light() {\n    }\n    public void on(){\n        System.out.println(\"\u6253\u5f00\u706f\");\n    }\n    public void off(){\n        System.out.println(\"\u5173\u95ed\u706f\");\n    }\n}\n<\/code><\/pre>\n<h3>2.2 \u4f7f\u7528\u547d\u4ee4\u5bf9\u8c61<\/h3>\n<p>\u5047\u8bbe\u53ea\u6709\u4e00\u4e2a\u9065\u63a7\u5668\uff0c\u53ea\u6709\u4e00\u4e2a\u6309\u94ae\u548c\u5bf9\u5e94\u7684\u63d2\u69fd\u3002<\/p>\n<pre><code>public class SimpleRemoteController {\n\/\/    \u63d2\u69fd\u6301\u6709\u547d\u4ee4\uff0c\u800c\u8fd9\u4e2a\u547d\u4ee4\u63a7\u5236\u7740\u4e00\u4e2a\u88c5\u7f6e\n    Command slot;\n\n    public SimpleRemoteController() { }\n\n\/\/    \u7528\u6765\u8bbe\u7f6e\u63d2\u69fd\u63a7\u5236\u7684\u547d\u4ee4\uff0c\u5982\u679c\u60f3\u8981\u6539\u53d8\u547d\u4ee4\uff0c\u53ef\u4ee5\u591a\u6b21\u8c03\u7528\n    public void setCommand(Command command) {\n        this.slot = command;\n    }\n\/\/    \u6309\u4e0b\u6309\u94ae\uff0c\u65b9\u6cd5\u5c31\u4f1a\u88ab\u8c03\u7528\n    public void buttonWasPressed(){\n        slot.execute();\n    }\n}\n<\/code><\/pre>\n<h3>2.3 \u6d4b\u8bd5\u9065\u63a7\u5668<\/h3>\n<pre><code>public class RemoteControllerTest {\n    public static void main(String[] args) {\n\/\/        \u5b9e\u4f8b\u5316\u9065\u63a7\u5668\n        SimpleRemoteController remote = new SimpleRemoteController();\n\/\/        \u521b\u5efa\u4e00\u4e2a\u7535\u706f\u5bf9\u8c61\uff0c\u6b64\u5bf9\u8c61\u5c31\u662f\u8bf7\u6c42\u63a5\u53d7\u8005\n        Light light = new Light();\n\/\/        \u521b\u5efa\u547d\u4ee4\uff0c\u5e76\u5c06\u63a5\u6536\u8005\u4f20\u7ed9\u5b83\n        LightOnCommand lightOn = new LightOnCommand(light);\n\/\/        \u5c06\u547d\u4ee4\u4f20\u7ed9\u8c03\u7528\u8005\n        remote.setCommand(lightOn);\n\/\/        \u6a21\u62df\u6309\u4e0b\u6309\u94ae\n        remote.buttonWasPressed();\n    }\n}\n\n<\/code><\/pre>\n<h2>3.\u8bbe\u8ba1\u6a21\u5f0f<\/h2>\n<h3>3.1 \u6a21\u5f0f\u5b9a\u4e49<\/h3>\n<blockquote><p>\n  \u547d\u4ee4\u6a21\u5f0f\uff1a\u5c06\u8bf7\u6c42\u5c01\u88c5\u6210\u5bf9\u8c61\uff0c\u4ee5\u4fbf\u4f7f\u7528\u4e0d\u7528\u7684\u8bf7\u6c42\uff0c\u961f\u5217\u6216\u8005\u65e5\u5fd7\u6765\u53c2\u6570\u5316\u5176\u4ed6\u5bf9\u8c61\u3002\u547d\u4ee4\u6a21\u5f0f\u4e5f\u652f\u6301\u53ef\u64a4\u9500\u7684\u64cd\u4f5c\u3002\n<\/p><\/blockquote>\n<p>\u73b0\u5728,\u4ed4\u7ec6\u770b\u8fd9\u4e2a\u5b9a\u4e49\u3002\u6211\u4eec\u77e5\u9053\u4e00\u4e2a\u547d\u4ee4\u5bf9\u8c61\u901a\u8fc7\u5728\u7279\u5b9a\u63a5\u6536\u8005\u4e0a\u7ed1\u5b9a\u4e00\u7ec4\u52a8\u4f5c\u6765\u5c01\u88c5\u4e00\u4e2a\u8bf7\u6c42\u3002\u8981\u8fbe\u5230\u8fd9\u4e00\u70b9,\u547d\u4ee4\u5bf9\u8c61\u5c06\u52a8\u4f5c\u548c\u63a5\u6536\u8005\u5305\u8fdb\u5bf9\u8c61\u4e2d\u3002\u8fd9\u4e2a\u5bf9\u8c61\u53ea\u66b4\u9732\u51fa\u4e00\u4e2aexecute\u65b9\u6cd5,\u5f53\u6b64\u65b9\u6cd5\u88ab\u8c03\u7528\u7684\u65f6\u5019,\u63a5\u6536\u8005\u5c31\u4f1a\u8fdb\u884c\u8fd9\u4e9b\u52a8\u4f5c\u3002\u4ece\u5916\u9762\u6765\u770b,\u5176\u4ed6\u5bf9\u8c61\u4e0d\u77e5\u9053\u7a76\u7adf\u54ea\u4e2a\u63a5\u6536\u8005\u8fdb\u884c\u4e86\u54ea\u4e9b\u52a8\u4f5c,\u53ea\u77e5\u9053\u5982\u679c\u8c03\u7528 execute\u65b9\u6cd5,\u8bf7\u6c42\u7684\u76ee\u7684\u5c31\u80fd\u8fbe\u5230\u3002<\/p>\n<h3>3.2 \u7c7b\u56fe<\/h3>\n<p><img decoding=\"async\" src=\"http:\/\/cos.rain1024.com\/markdown\/image-20191023224003672.png\" alt=\"\" \/><\/p>\n<h2>4.\u5f00\u59cb\u5b9e\u73b0<\/h2>\n<h3>4.1 \u5b9e\u73b0\u9065\u63a7\u5668<\/h3>\n<pre><code>public class RemoteControl {\n    Command[] onCommands;\n    Command[] offCommands;\n\n    \/**\n     * \u521d\u59cb\u5316\u8fd9\u4e24\u4e2a\u5f00\u5173\u7684\u6570\u7ec4\n     *\/\n    public RemoteControl(){\n        onCommands = new Command[7];\n        offCommands = new Command[7];\n\n        Command noCommand = new NoCommand();\n        for (int i = 0 ;i &lt; 7;i++){\n            onCommands[i] = noCommand;\n            offCommands[i] = noCommand;\n        }\n    }\n\n    \/**\n     * \u6709\u4e09\u4e2a\u547d\u4ee4\uff0c\u5206\u522b\u662f\u63d2\u69fd\u4f4d\u7f6e\uff0c\u5f00\u7684\u547d\u4ee4\uff0c\u5173\u7684\u547d\u4ee4\n     * @param slot\n     * @param onCommand\n     * @param offCommand\n     *\/\n    public void setCommand(int slot ,Command onCommand,Command offCommand){\n        onCommands[slot] = onCommand;\n        offCommands[slot] = offCommand;\n    }\n    public void onButtonWasPushed(int slot){\n        onCommands[slot].execute();\n    }\n    public void offButtonWasPushed(int slot){\n        offCommands[slot].execute();\n    }\n\n    @Override\n    public String toString() {\n        StringBuffer stringBuffer = new StringBuffer();\n        stringBuffer.append(\"\\n-------Remote Control ------\\n\");\n        for (int i = 0 ;i &lt; onCommands.length;i++){\n            stringBuffer.append(\"[slot\"+i+\"]\"+onCommands[i].getClass().getName()+\"        \"\n            +offCommands[i].getClass().getName()+\"\\n\");\n        }\n        return stringBuffer.toString();\n    }\n}\n\n<\/code><\/pre>\n<pre><code>public class NoCommand implements Command {\n    public void execute() {\n\n    }\n}\n\n<\/code><\/pre>\n<blockquote><p>\n  Nocommand\u5bf9\u8c61\u662f\u4e00\u4e2a\u7a7a\u5bf9\u8c61( null object)\u7684\u4f8b\u5b50\u3002\u5f53\u4f60\u4e0d\u60f3\u8fd4\u56de\u4e00\u4e2a\u6709\u610f\u4e49\u7684\u5bf9\u8c61\u65f6,\u7a7a\u5bf9\u8c61\u5c31\u5f88\u6709\u7528\u3002\u5ba2\u6237\u4e5f\u53ef\u4ee5\u5c06\u5904\u7406nul\u7684\u8d23\u4efb\u8f6c\u79fb\u7ed9\u7a7a\u5bf9\u8c61\u3002\u4e3e\u4f8b\u6765\u8bf4,\u9065\u63a7\u5668\u4e0d\u53ef\u80fd\u4e00\u51fa\u5382\u5c31\u8bbe\u7f6e\u4e86\u6709\u610f\u4e49\u7684\u547d\u4ee4\u5bf9\u8c61,\u6240\u4ee5\u63d0\u4f9b\u4e86 Nocommand\u5bf9\u8c61\u4f5c\u4e3a\u4ee3\u7528\u54c1,\u5f53\u8c03\u7528\u5b83\u7684 execute0\u65b9\u6cd5\u65f6,\u8fd9\u79cd\u5bf9\u8c61\u4ec0\u4e48\u4e8b\u60c5\u90fd\u4e0d\u505a\u3002<br \/>\n  \u5728\u8bb8\u591a\u8bbe\u8ba1\u6a21\u5f0f\u4e2d,\u90fd\u4f1a\u770b\u5230\u7a7a\u5bf9\u8c61\u7684\u4f7f\u7528\u3002\u751a\u81f3\u6709\u4e9b\u65f6\u5019,\u7a7a\u5bf9\u8c61\u672c\u8eab\u4e5f\u88ab\u89c6\u4e3a\u662f\u79cd\u8bbe\u8ba1\u6a21\u5f0f\u3002\n<\/p><\/blockquote>\n<h3>4.2\u5b9e\u73b0\u547d\u4ee4<\/h3>\n<pre><code>public class LightOffCommand implements Command {\n    Light light;\n\n    public LightOffCommand(Light light) {\n        this.light = light;\n    }\n\n    public void execute() {\n        light.off();\n    }\n}\n<\/code><\/pre>\n<h3>4.3\u6d4b\u8bd5\u9065\u63a7\u5668<\/h3>\n<pre><code>public class RemoteLoader {\n    public static void main(String[] args) {\n        RemoteControl remoteControl = new RemoteControl();\n\n        Light livingroomLight = new Light(\"Living Room\");\n        Light kitchenLight = new Light(\"Kitchen\");\n\/\/        ceilingFan = new\n\n        LightOnCommand LivingRoomlightOnCommand = new LightOnCommand(livingroomLight);\n        LightOffCommand LivingRoomlightOffCommand = new LightOffCommand(livingroomLight);\n        LightOnCommand KitchenlightOnCommand = new LightOnCommand(livingroomLight);\n        LightOffCommand KitchenlightOffCommand = new LightOffCommand(kitchenLight);\n\n\/\/        \u6dfb\u52a0\u547d\u4ee4\n        remoteControl.setCommand(0,LivingRoomlightOnCommand,LivingRoomlightOffCommand);\n\n\/\/        \u6253\u5370\u9065\u63a7\u5668\u91cc\u7684\u4fe1\u606f\n        System.out.println(remoteControl);\n\n\/\/        \u6267\u884c\u5f00\u5173\n        remoteControl.onButtonWasPushed(0);\n        remoteControl.offButtonWasPushed(0);\n    }\n\n}\n<\/code><\/pre>\n<h3>4.4 \u67e5\u770b\u6211\u4eec\u7684\u7c7b\u56fe<\/h3>\n<p>\u7ecf\u8fc7\u4ee5\u4e0a\u4ee3\u7801\u7684\u5b9e\u73b0\uff0c\u57fa\u672c\u5b8c\u6210\u4e86\u6240\u9700\u8981\u7684\u529f\u80fd\uff0c\u8fd9\u4e2a\u65f6\u5019\u770b\u4e00\u4e0b\u4ee3\u7801\u7684UML\u56fe\uff0c\u8fd9\u6837\u53ef\u4ee5\u66f4\u597d\u7684\u7406\u89e3\u4ee3\u7801\u4e4b\u95f4\u7684\u7ee7\u627f\u5173\u7cfb\u3002<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/cos.rain1024.com\/markdown\/image-20191024065748724.png\" alt=\"\" \/><\/p>\n<h2>5.\u6dfb\u52a0\u64a4\u9500<\/h2>\n<p>\u597d\u4e86,\u6211\u4eec\u73b0\u5728\u9700\u8981\u5728\u9065\u63a7\u5668\u4e0a\u52a0\u4e0a\u64a4\u9500\u7684\u529f\u80fd\u3002\u8fd9\u4e2a\u529f\u80fd\u4f7f\u7528\u8d77\u6765\u5c31\u50cf\u662f\u8fd9\u6837\u7684:\u6bd4\u65b9\u8bf4\u5ba2\u5385\u7684\u7535\u706f\u662f\u5173\u95ed\u7684,\u7136\u540e\u4f60\u6309\u4e0b\u9065\u63a7\u5668\u4e0a\u7684\u5f00\u542f\u6309\u94ae,\u81ea\u7136\u7535\u706f\u5c31\u88ab\u6253\u5f00\u4e86\u3002\u73b0\u5728\u5982\u679c\u6309\u4e0b\u64a4\u9500\u6309\u94ae,\u90a3\u4e48\u4e0a\u4e00\u4e2a\u52a8\u4f5c\u5c06\u88ab\u5012\u8f6c,\u5728\u8fd9\u4e2a\u4f8b\u5b50\u91cc,\u7535\u706f\u5c06\u88ab\u5173\u95ed\u3002\u5728\u8fdb\u5165\u66f4\u590d\u6742\u7684\u4f8b\u5b50\u4e4b\u524d,\u5148\u8ba9\u64a4\u9500\u6309\u94ae\u80fd\u591f\u5904\u7406\u7535\u706f<\/p>\n<p>\u5728\u547d\u4ee4\u63a5\u53e3\u4e2d\u52a0\u5165undo\u65b9\u6cd5<\/p>\n<pre><code>public interface Command {\n    public void execute();\n    public void undo();\n}\n\n<\/code><\/pre>\n<p>\u5728\u6253\u5f00\u706f\u7684\u547d\u4ee4\u4e2d\uff0c\u6267\u884coff\u64cd\u4f5c<\/p>\n<pre><code>public class LightOnCommand implements Command {\n    Light light;\n\/\/    \u7ed9\u6784\u9020\u5668\u4f20\u5165\u67d0\u4e2a\u7535\u706f\uff0c\u65b9\u4fbf\u4ee5\u540e\u8c03\u7528execute\n    public LightOnCommand(Light light){\n        this.light = light;\n    }\n\/\/    \u8c03\u7528\u63a5\u53d7\u5bf9\u8c61\u7684on\u65b9\u6cd5\n    public void execute() {\n        light.on();\n    }\n\n    public void undo() {\n        light.off();\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u5173\u95ed\u706f\u7684\u547d\u4ee4\u4e2d\uff0c\u6267\u884con\u65b9\u6cd5<\/p>\n<pre><code>public class LightOffCommand implements Command {\n    Light light;\n\n    public LightOffCommand(Light light) {\n        this.light = light;\n    }\n\n    public void execute() {\n        light.off();\n    }\n\n    public void undo() {\n        light.on();\n    }\n}\n<\/code><\/pre>\n<pre><code>public class NoCommand implements Command {\n    public void execute() {\n\n    }\n\n    public void undo() {\n\n    }\n}\n<\/code><\/pre>\n<p>\u5bf9\u9065\u63a7\u5668\u8fdb\u884c\u4e00\u4e9b\u4fee\u6539\uff0c\u6bcf\u6b21\u6309\u4e0b\u6309\u94ae\u7684\u65f6\u5019\uff0c\u90fd\u8bb0\u5f55\u4e0b\u6765\u6309\u52a8\u7684\u54ea\u4e2a\uff0c\u5e76\u4e14\u53ef\u4ee5\u968f\u65f6\u8c03\u7528undo\u65b9\u6cd5\u3002<\/p>\n<pre><code>public class RemoteControl {\n    Command[] onCommands;\n    Command[] offCommands;\n\n    Command undoCommand;\n    \/**\n     * \u521d\u59cb\u5316\u8fd9\u4e24\u4e2a\u5f00\u5173\u7684\u6570\u7ec4\n     *\/\n    public RemoteControl(){\n        onCommands = new Command[7];\n        offCommands = new Command[7];\n\n        Command noCommand = new NoCommand();\n        for (int i = 0 ;i &lt; 7;i++){\n            onCommands[i] = noCommand;\n            offCommands[i] = noCommand;\n        }\n        undoCommand = noCommand;\n    }\n\n    \/**\n     * \u6709\u4e09\u4e2a\u547d\u4ee4\uff0c\u5206\u522b\u662f\u63d2\u69fd\u4f4d\u7f6e\uff0c\u5f00\u7684\u547d\u4ee4\uff0c\u5173\u7684\u547d\u4ee4\n     * @param slot\n     * @param onCommand\n     * @param offCommand\n     *\/\n    public void setCommand(int slot ,Command onCommand,Command offCommand){\n        onCommands[slot] = onCommand;\n        offCommands[slot] = offCommand;\n    }\n    public void onButtonWasPushed(int slot){\n        onCommands[slot].execute();\n        undoCommand = onCommands[slot];\n    }\n    public void offButtonWasPushed(int slot){\n        offCommands[slot].execute();\n        undoCommand = offCommands[slot];\n    }\n\n    public void undoButtonWasPushhed(){\n        undoCommand.undo();\n    }\n\n\n    @Override\n    public String toString() {\n        StringBuffer stringBuffer = new StringBuffer();\n        stringBuffer.append(\"\\n-------Remote Control ------\\n\");\n        for (int i = 0 ;i &lt; onCommands.length;i++){\n            stringBuffer.append(\"[slot\"+i+\"]\"+onCommands[i].getClass().getName()+\"        \"\n            +offCommands[i].getClass().getName()+\"\\n\");\n        }\n        return stringBuffer.toString();\n    }\n}\n<\/code><\/pre>\n<blockquote><p>\n  \u4e66\u4e2d\u8fd8\u52a0\u5165\u4e86\uff0c\u4f7f\u7528\u72b6\u6001\u5b9e\u73b0\u64a4\u9500\uff0c\u5e76\u5b9e\u73b0\u4e86\u7535\u6247\u64a4\u9500\u7684\u529f\u80fd\uff0c\u867d\u7136\u6bd4\u7535\u706f\u8981\u590d\u6742\u4e00\u4e9b\uff0c\u4f46\u603b\u4f53\u601d\u8def\u76f8\u540c\u3002\n<\/p><\/blockquote>\n<h2>6.\u547d\u4ee4\u6a21\u5f0f\u7684\u66f4\u591a\u7528\u9014<\/h2>\n<h3>6.1 \u961f\u5217\u8bf7\u6c42<\/h3>\n<p>\u547d\u4ee4\u53ef\u4ee5\u5c06\u8fd0\u7b97\u5757\u6253\u5305(\u4e00\u4e2a\u63a5\u6536\u8005\u548c\u4e00\u7ec4\u52a8\u4f5c), \u7136\u540e\u5c06\u5b83\u4f20\u6765\u4f20\u53bb,\u5c31\u50cf\u662f\u4e00\u822c\u7684\u5bf9\u8c61\u4e00\u6837\u3002\u73b0\u5728\u5373\u4f7f\u5728\u547d\u4ee4\u5bf9\u8c61\u88ab\u521b\u5efa\u8bb8\u4e45\u4e4b\u540e,\u8fd0\u7b97\u4f9d\u7136\u53ef\u4ee5\u88ab\u8c03\u7528\u3002\u4e8b\u5b9e\u4e0a,\u5b83\u751a\u81f3\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u7ebf\u7a0b\u4e2d\u88ab\u8c03\u7528\u3002\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u8fd9\u6837\u7684\u7279\u6027\u884c\u751f\u4e00\u4e9b\u5e94\u7528,\u4f8b\u5982:\u65e5\u7a0b\u5b89\u6392( Scheduler)\u3001\u7ebf\u7a0b\u6c60\u3001\u5de5\u4f5c\u961f\u5217\u7b49\u3002<br \/>\n\u60f3\u8c61\u6709\u4e00\u4e2a\u5de5\u4f5c\u961f\u5217:\u4f60\u5728\u67d0\u4e00\u7aef\u6dfb\u52a0\u547d\u4ee4,\u7136\u540e\u53e6\u7aef\u5219\u662f\u7ebf\u7a0b\u3002\u7ebf\u7a0b\u8fdb\u884c\u4e0b\u9762\u7684\u52a8\u4f5c:\u4ece\u961f\u5217\u4e2d\u53d6\u51fa\u4e00\u4e2a\u547d\u4ee4,\u8c03\u7528\u5b83\u7684 execute()\u65b9\u6cd5,\u7b49\u5f85\u8fd9\u4e2a\u8c03\u7528\u5b8c\u6210, \u7136\u540e\u5c06\u6b64\u547d\u4ee4\u5bf9\u8c61\u4e22\u5f03,\u518d\u53d6\u51fa\u4e0b\u4e00\u4e2a\u547d\u4ee4<\/p>\n<blockquote><p>\n  \u8bf7\u6ce8\u610f,\u5de5\u4f5c\u961f\u5217\u7c7b\u548c\u8fdb\u884c\u8ba1\u7b97\u7684\u5bf9\u8c61\u4e4b\u95f4\u5b8c\u5168\u662f\u89e3\u8026\u7684\u3002\u6b64\u523b\u7ebf\u7a0b\u53ef\u80fd\u5728\u8fdb\u884c\u8d22\u52a1\u8fd0\u7b97,\u4e0b\u4e00\u523b\u5374\u5728\u8bfb\u53d6\u7f51\u7edc\u6570\u636e\u3002\u5de5\u4f5c\u961f\u5217\u5bf9\u8c61\u4e0d\u5728\u5e73\u5230\u5e95\u505a\u4e9b\u4ec0\u4e48,\u5b83\u4eec\u53ea\u77e5\u9053\u53d6\u51fa\u547d\u4ee4\u5bf9\u8c61,\u7136\u540e\u8c03\u7528\u5176execute\u65b9\u6cd5\u3002\u7c7b\u4f3c\u5730,\u5b83\u4eec\u53ea\u8981\u662f\u5b9e\u73b0\u547d\u4ee4\u6a21\u5f0f\u7684\u5bf9\u8c61,\u5c31\u53ef\u4ee5\u653e\u5165\u961f\u5217\u91cc,\u5f53\u7ebf\u7a0b\u53ef\u7528\u65f6,\u5c31\u8c03\u7528\u6b64\u5bf9\u8c61\u7684 execute\u65b9\u6cd5\u3002\n<\/p><\/blockquote>\n<h3>6.2 \u65e5\u5fd7\u8bf7\u6c42<\/h3>\n<p>\u67d0\u4e9b\u5e94\u7528\u9700\u8981\u6211\u4eec\u5c06\u6240\u6709\u7684\u52a8\u4f5c\u90fd\u8bb0\u5f55\u5728\u65e5\u5fd7\u4e2d\uff0c\u5e76\u80fd\u5728\u7cfb\u7edf\u6b7b\u673a\u4e4b\u540e\uff0c\u91cd\u65b0\u8c03\u7528\u8fd9\u4e9b\u52a8\u4f5c\u6062\u590d\u5230\u4e4b\u524d\u7684\u72b6\u6001\u3002\u901a\u8fc7\u65b0\u589e\u4e24\u4e2a\u65b9\u6cd5\uff08store\uff08\uff09\u3001load\uff09\uff0c\u547d\u4ee4\u6a21\u5f0f\u5c31\u80fd\u591f\u652f\u6301\u8fd9\u4e00\u70b9\u3002\u5728Java\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u5bf9\u8c61\u7684\u5e8f\u5217\u5316\uff08Serialization\uff09\u5b9e\u73b0\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f46\u662f\u4e00\u822c\u8ba4\u4e3a\u5e8f\u5217\u5316\u6700\u597d\u8fd8\u662f\u53ea\u7528\u5728\u5bf9\u8c61\u7684\u6301\u4e45\u5316\u4e0a\uff08persistence\uff09\u8981\u600e\u4e48\u505a\u5462\uff1f\u5f53\u6211\u4eec\u6267\u884c\u547d\u4ee4\u7684\u65f6\u5019\uff0c\u5c06\u5386\u53f2\u8bb0\u5f55\u50a8\u5b58\u5728\u78c1\u76d8\u4e2d\u3002\u4e00\u65e6\u7cfb\u7edf\u6b7b\u673a\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06\u547d\u4ee4\u5bf9\u8c61\u91cd\u65b0\u52a0\u8f7d\uff0c\u5e76\u6210\u6279\u5730\u4f9d\u6b21\u8c03\u7528\u8fd9\u4e9b\u5bf9\u8c61\u7684 execute\uff08\u65b9\u6cd5\u3002\u8fd9\u79cd\u65e5\u5fd7\u7684\u65b9\u5f0f\u5bf9\u4e8e\u9065\u63a7\u5668\u6765\u8bf4\u6ca1\u6709\u610f\u4e49\u7136\u800c\uff0c\u6709\u8bb8\u591a\u8c03\u7528\u5927\u578b\u6570\u636e\u7ed3\u6784\u7684\u52a8\u4f5c\u7684\u5e94\u7528\u65e0\u6cd5\u5728\u6bcf\u6b21\u6539\u53d8\u53d1\u751f\u65f6\u88ab\u5feb\u901f\u5730\u5b58\u50a8\u3002\u901a\u8fc7\u4f7f\u7528\u8bb0\u5f55\u65e5\u5fd7\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u4e0a\u6b21\u68c0\u67e5\u70b9\uff08checkpoint\uff09\u4e4b\u540e\u7684\u6240\u6709\u64cd\u4f5c\u8bb0\u5f55\u4e0b\u6765\uff0c\u5982\u679c\u7cfb\u7edf\u51fa\u72b6\u51b5\uff0c\u4ece\u68c0\u67e5\u70b9\u5f00\u59cb\u5e94\u7528\u8fd9\u4e9b\u64cd\u4f5c\u3002\u6bd4\u65b9\u8bf4\uff0c\u5bf9\u4e8e\u7535\u5b50\u8868\u683c\u5e94\u7528\uff0c\u6211\u4eec\u53ef\u80fd\u60f3\u8981\u5b9e\u73b0\u7684\u9519\u8bef\u6062\u590d\u65b9\u5f0f\u662f\u5c06\u7535\u5b50\u8868\u683c\u7684\u64cd\u4f5c\u8bb0\u5f55\u5728\u65e5\u5fd7\u4e2d\uff0c\u800c\u4e0d\u662f\u6bcf\u6b21\u7535\u5b50\u8868\u683c\u4e00\u6709\u53d8\u5316\u5c31\u8bb0\u5f55\u6574\u4e2a\u7535\u5b50\u8868\u683c\u3002\u5bf9\u66f4\u9ad8\u7ea7\u7684\u5e94\u7528\u800c\u8a00\uff0c\u8fd9\u4e9b\u6280\u5de7\u53ef\u4ee5\u88ab\u6269\u5c55\u5e94\u7528\u5230\u4e8b\u52a1\uff08 transaction\uff09\u5904\u7406\u4e2d\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u4e00\u6574\u7fa4\u64cd\u4f5c\u5fc5\u987b\u5168\u90e8\u8fdb\u884c\u5b8c\u6210\uff0c\u6216\u8005\u6ca1\u6709\u8fdb\u884c\u4efb\u4f55\u7684\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u547d\u4ee4\u6a21\u5f0f\uff1a\u5c06\u8bf7\u6c42\u5c01\u88c5\u6210\u5bf9\u8c61\uff0c\u4ee5\u4fbf\u4f7f\u7528\u4e0d\u7528\u7684\u8bf7\u6c42\uff0c\u961f\u5217\u6216\u8005\u65e5\u5fd7\u6765\u53c2\u6570\u5316\u5176\u4ed6\u5bf9\u8c61\u3002\u547d\u4ee4\u6a21\u5f0f\u4e5f\u652f\u6301\u53ef\u64a4\u9500\u7684\u64cd\u4f5c\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,23],"tags":[91],"class_list":["post-1082","post","type-post","status-publish","format-standard","hentry","category-programme","category-23","tag-91"],"_links":{"self":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/1082","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/comments?post=1082"}],"version-history":[{"count":1,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/1082\/revisions"}],"predecessor-version":[{"id":1371,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/1082\/revisions\/1371"}],"wp:attachment":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media?parent=1082"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/categories?post=1082"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/tags?post=1082"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}