命令模式Command(对象行为型)
来源:互联网 发布:陌生人语音聊天软件 编辑:程序博客网 时间:2024/05/17 06:31
参考文档:
1.设计模式-可复用面向对象软件的基础
2.http://blog.csdn.net/hguisu/article/details/7549895(设计模式 ( 十三 ) 命令模式Command(对象行为型))
命令模式Command(对象行为型)
理论方面的知识,可以参考 参考文档中的内容。这里讲一下代码实现。
看一下Command模式的结构:
这里的案例,是设计模式书中,Command模式动机一节中的Command类的实现的大致框架。
我们将定义OpenCommand、PasteCommand、MacroCommand。首先是抽象的Command类:
Command.java:
package com.rick.designpattern.command;import java.lang.reflect.InvocationTargetException;/** * Created by MyPC on 2017/6/13. */public abstract class Command { public Command() { } public abstract void execute() throws InvocationTargetException, IllegalAccessException;}
OpenCommand.java:
package com.rick.designpattern.command;/** * Created by MyPC on 2017/6/13. */public class OpenCommand extends Command { private Application application; public OpenCommand(Application application) { this.application = application; } @Override public void execute() { String name = AskUser.getName(); if (null != name) { Document document = new Document(name); application.add(document); document.open(); } }}
PasteCommand.java:
package com.rick.designpattern.command;/** * Created by MyPC on 2017/6/13. */public class PasteCommand extends Command { private Document document; public PasteCommand(Document document) { this.document = document; } @Override public void execute() { document.paste(); }}
SimpleCommand.java:
package com.rick.designpattern.command;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;/** * Created by MyPC on 2017/6/13. */public class SimpleCommand<T> extends Command { private T t; private Method method; public SimpleCommand(T t, Method method) { this.t = t; this.method = method; } @Override public void execute() throws InvocationTargetException, IllegalAccessException { method.invoke(t, null); }}
MacroCommand.java:
package com.rick.designpattern.command;import java.lang.reflect.InvocationTargetException;import java.util.ArrayList;import java.util.List;/** * Created by MyPC on 2017/6/13. */public class MacroCommand extends Command { List<Command> commands = new ArrayList<Command>(); public void add(Command command) { commands.add(command); } public void remove(Command command) { commands.remove(command); } @Override public void execute() throws InvocationTargetException, IllegalAccessException { for (Command command : commands) { command.execute(); } }}
Application.java:
package com.rick.designpattern.command;/** * Created by MyPC on 2017/6/13. */public class Application { private Document document; public void add(Document document) { this.document = document; }}
AskUser.java:
package com.rick.designpattern.command;/** * Created by MyPC on 2017/6/13. */public class AskUser { public static String getName(){ return "test.txt"; }}
Document.java:
package com.rick.designpattern.command;/** * Created by MyPC on 2017/6/13. */public class Document { private String name; public Document(String name) { this.name = name; } public void open() { System.out.println("Document in open is " + name); } public void paste() { System.out.println("Document in paste"); }}
MyClass.java:
package com.rick.designpattern.command;/** * Created by MyPC on 2017/6/13. */public class MyClass { public void handler() { System.out.println("MyClass is handler"); }}
Client.java:
package com.rick.designpattern.command;import java.lang.reflect.InvocationTargetException;/** * Created by MyPC on 2017/6/13. */public class Client { public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { MyClass receiver = new MyClass(); Command command = new SimpleCommand<MyClass>(receiver, receiver.getClass().getMethod("handler", null)); command.execute(); Document document = new Document("TEST.txt"); Application application = new Application(); OpenCommand openCommand = new OpenCommand(application); openCommand.execute(); PasteCommand pasteCommand = new PasteCommand(document); pasteCommand.execute(); MacroCommand macroCommand = new MacroCommand(); macroCommand.add(command); macroCommand.add(openCommand); macroCommand.add(pasteCommand); macroCommand.execute(); }}
阅读全文
0 0
- 命令模式Command(对象行为型)
- COMMAND 命令模式 --对象行为型模式
- 设计模式之命令(Command)---对象行为型模式
- 设计模式 ( 十三 ) 命令模式Command(对象行为型)
- 设计模式 ( 十三 ) 命令模式Command(对象行为型)
- 设计模式 ( 十三 ) 命令模式Command(对象行为型)
- 设计模式 ( 十三 ) 命令模式Command(对象行为型)
- COMMAND(命令)— 对象行为型模式
- 3.2 Command(命令) -- 对象行为型模式
- 行为型模式-----命令模式(Command)
- 命令模式(Command Action 事务模式 对象行为模式)
- 对象行为型模式——命令模式(Command)
- 十四、命令模式Command(行为型)
- 行为型-命令模式(Command)
- C#面向对象设计模式纵横谈 学习笔记15 Command命令模式(行为型模式)
- 设计模式之对象行为型模式 — COMMAND (命令)模式
- Command命令(行为型模式)
- 行为型模式-命令command
- Redis 该选择哪种持久化配置
- java笔记-LinkedList面试复习
- 欢迎使用CSDN-markdown编辑器
- linux下文件恢复工具extundelete
- 基于ServerSocket的多人聊天室模拟实现
- 命令模式Command(对象行为型)
- Ajax 的传统实现 实例
- Hive的数据类型与基础操作
- 关于gid 与groups 的区别
- java学习路线图
- 前端构建工具gulp超详细配置, 使用教程(图文)
- 贫血模式or领域模式
- Python 文件内容修改-循环+with语句防止忘关资源
- 【脚本语言系列】关于Python网络应用Telnet,你需要知道的事