命令模式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
原创粉丝点击