您的位置:首页 > 编程语言 > Java开发

Java模式 - Command(命令模式)

2011-11-28 10:19 771 查看
[http://www.blogjava.net/supercrsky/articles/200497.html]

[http://xuzhenqinandy.iteye.com/blog/206011]

[http://www.linuxidc.com/Linux/2011-08/41686.htm]

[http://www.360doc.com/content/06/1127/10/5874_273854.shtml]

命令模式有以下角色来组成

1) 命令角色(Command):声明执行操作的接口。有java接口或者抽象类来实现。

2) 具体命令角色(Concrete Command):将一个接收者对象绑定于一个动作;调用接收者相应的操作,以实现命令角色声明的执行操作的接口。

3) 客户角色(Client):创建一个具体命令对象(并可以设定它的接收者)。

4) 请求者角色(Invoker):调用命令对象执行这个请求。

5) 接收者角色(Receiver):知道如何实施与执行一个请求相关的操作。任何类都可能作为一个接收者。

命令模式在SWING中经常用到,这种模式经常用在按钮上面。我现在把SWING中的这种模式提取出来供大家一起来讨论。

首先建立命令借口

Java代码

public interface CommandInterface { public void excute(); }

public interface CommandInterface {
public void excute();
}


在建立两个不同的命令执行类

Java代码

public class InCommand implements CommandInterface{ /** * 具体的命令执行内容 */ public void excute() { System.out.println("int command"); } } public class OutCommand implements CommandInterface{ /** * 具体的命令执行内容 */ public void excute() { System.out.println("out command"); } }

public class InCommand implements CommandInterface{

/**
* 具体的命令执行内容
*/
public void excute() {
System.out.println("int command");
}

}

public class OutCommand implements CommandInterface{

/**
* 具体的命令执行内容
*/
public void excute() {
System.out.println("out command");
}

}


建立增加命令的借口

Java代码

public interface AddCommandInterface { public void setCommand(CommandInterface comd); public CommandInterface getCommand(); }

public interface AddCommandInterface {
public void setCommand(CommandInterface comd);
public CommandInterface getCommand();
}


增加命令的实现类

Java代码

public class OutAddCommand implements AddCommandInterface{ private CommandInterface comd = null; /** * 获得命令 */ public CommandInterface getCommand() { return this.comd; } /** * 设置命令 */ public void setCommand(CommandInterface comd) { this.comd = comd; } }

public class OutAddCommand implements AddCommandInterface{

private CommandInterface comd = null;

/**
* 获得命令
*/
public CommandInterface getCommand() {

return this.comd;
}

/**
* 设置命令
*/
public void setCommand(CommandInterface comd) {
this.comd = comd;
}

}


建立事件激发借口

Java代码

public interface ActionInterface { public void actionPerformed(AddCommandInterface comInter); }

public interface ActionInterface {
public void actionPerformed(AddCommandInterface comInter);
}


事件激发借口的实现类

Java代码

public class ActionCommand implements ActionInterface{ public void actionPerformed(AddCommandInterface comInter) { comInter.getCommand().excute(); } }

public class ActionCommand implements ActionInterface{
public void actionPerformed(AddCommandInterface comInter)
{
comInter.getCommand().excute();
}
}


对命令接口的实现

Java代码

public class Actualize { private AddCommandInterface addCommand = null; public Actualize() { } /** * 增加命令 * @param addCommand */ public void addCommandInter(AddCommandInterface addCommand) { this.addCommand = addCommand; } /** * 执行命令 * @param action */ public void addAction(ActionInterface action) { action.actionPerformed(this.addCommand); } }

public class Actualize {

private AddCommandInterface addCommand = null;

public Actualize()
{

}

/**
* 增加命令
* @param addCommand
*/
public void addCommandInter(AddCommandInterface addCommand)
{
this.addCommand = addCommand;
}

/**
* 执行命令
* @param action
*/
public void addAction(ActionInterface action)
{
action.actionPerformed(this.addCommand);
}
}


封装接口的实现

Java代码

public class ExcuteCommand { public static void exec(CommandInterface com) { Actualize actu = new Actualize(); OutAddCommand outAdd = new OutAddCommand(); outAdd.setCommand(com); actu.addCommandInter(outAdd); actu.addAction(new ActionCommand()); } }

public class ExcuteCommand {
public static void exec(CommandInterface com) {
Actualize actu = new Actualize();

OutAddCommand outAdd = new OutAddCommand();

outAdd.setCommand(com);

actu.addCommandInter(outAdd);

actu.addAction(new ActionCommand());
}
}


具体命令的调用

Java代码

public class TestCommand { public static void main(String[] args) { OutCommand out = new OutCommand(); ExcuteCommand.exec(out); InCommand in = new InCommand(); ExcuteCommand.exec(in); } }

public class TestCommand {
public static void main(String[] args)
{
OutCommand out = new OutCommand();
ExcuteCommand.exec(out);

InCommand in = new InCommand();
ExcuteCommand.exec(in);
}
}


输出结果:

Java代码

out command int command

out command
int command
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: