[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()
- {
- }
- public void addCommandInter(AddCommandInterface addCommand)
- {
- this.addCommand = addCommand;
- }
- 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