コマンド(コマンド)モードでは、「要求」異なる要求キューまたはその他のオブジェクトを使用するオブジェクトにカプセル化は、パラメータ化ログです。コマンドモードも取り消し可能操作がサポートされています。
直接例を例示する:コードは、スマートホームのリモートコントロールをシミュレートする、リモートコントロールは、プログラマブル7つのスロット(異なる家電機器に、各スロット対応する)を有し、各スロットは、一対のオン/オフボタン、リモコン全体アンドゥボタンに対応します。リモコンは、操作ボタンが押されていると解釈する方法を知っているし、正しい家電製品への要求を発行しますが、リモートコントロール、オートメーション機器、またはどのように給湯器を回すための詳細を知る必要はありません必要があります。
設計において使用さオブジェクト、「アクションの行為者」から切り離す「操作要求元の」コマンド・モード「コマンドオブジェクト」、特定のオブジェクト(例えば、リビングルームランプオブジェクト)内にカプセル化要求(例えば、ライトをオン)。コマンドオブジェクトは、各ボタンに格納されている場合は、ボタンが押された場合、その後、あなたは、関連する作業を行うために、コマンドオブジェクトを求めることができます。リモコンは知っている必要はありませんどのような仕事内容、限りコマンドと、それに物事を成し遂げる、このようにして、通信することができ、正しいオブジェクトのオブジェクトが存在するとして、リモートコントロール(すなわち、発行要求されたオブジェクト、実行者)と、様々な家電製品のオブジェクトが(すなわち、受け入れ及びこれら要求されたオブジェクト、受信機の実装)デカップリングに。
絵は、クラスのコマンドモードを示してい
コードで次の直接見て
コマンド・インタフェース
public interface Command {
public void execute();
public void undo();
}
Aを実現するためにライトをオンにします
import com.crow.Receiver.Light;
public class LightOnCommand implements Command {
Light light;
public LightOnCommand(Light light) {
this.light = light;
}
public void execute() {
light.on();
}
public void undo() {
light.off();
}
}
ランプの種類:
public class Light {
String location;
int level;
public Light(String location) {
this.location = location;
}
public void on() {
level = 100;
System.out.println("Light is on");
}
public void off() {
level = 0;
System.out.println("Light is off");
}
public void dim(int level) {
this.level = level;
if (level == 0) {
off();
}
else {
System.out.println("Light is dimmed to " + level + "%");
}
}
public int getLevel() {
return level;
}
}
コマンド要求者:リモコン、また元に戻す]ボタンを実装して、キーの取り消し
import com.crow.Command.Command;
import com.crow.Command.NoCommand;
//
// This is the invoker
//
public class RemoteControl {
Command[] onCommands;
Command[] offCommands;
Command undoCommand;
public RemoteControl() {
onCommands = new Command[7];
offCommands = new Command[7];
Command noCommand = new NoCommand();
for(int i=0;i<7;i++) {
onCommands[i] = noCommand;
offCommands[i] = noCommand;
}
undoCommand = noCommand;
}
public void setCommand(int slot, Command onCommand, Command offCommand) {
onCommands[slot] = onCommand;
offCommands[slot] = offCommand;
}
public void onButtonWasPushed(int slot) {
onCommands[slot].execute();
undoCommand = onCommands[slot];
}
public void offButtonWasPushed(int slot) {
offCommands[slot].execute();
undoCommand = offCommands[slot];
}
public void undoButtonWasPushed() {
undoCommand.undo();
}
public String toString() {
StringBuffer stringBuff = new StringBuffer();
stringBuff.append("\n------ Remote Control -------\n");
for (int i = 0; i < onCommands.length; i++) {
stringBuff.append("[slot " + i + "] " + onCommands[i].getClass().getName()
+ " " + offCommands[i].getClass().getName() + "\n");
}
stringBuff.append("[undo] " + undoCommand.getClass().getName() + "\n");
return stringBuff.toString();
}
}
また、コマンドの多くを保存するためのコマンド列を引き出すことができ、前記マクロコマンドクラス、およびコマンド使い捨ての実施形態は複数存在します。
public class MacroCommand implements Command {
Command[] commands;
public MacroCommand(Command[] commands) {
this.commands = commands;
}
public void execute() {
for (int i = 0; i < commands.length; i++) {
commands[i].execute();
}
}
/** * NOTE: these commands have to be done backwards to ensure proper undo functionality */
public void undo() {
for (int i = commands.length -1; i >= 0; i--) {
commands[i].undo();
}
}
}
アプリケーション
- ログは要求:我々は、コマンドを実行すると、履歴は、ログを使用することにより、システムがクラッシュした後、我々はコマンドオブジェクトをリロードすることができ、かつターンの呼び出しでこれらのオブジェクトのバッチは()メソッドを実行し、ディスク上に保存されていますシステムは、状況から外れている場合、我々は、最後のチェックポイントレコード以降のすべての操作は、これらの操作は、チェックポイントから回復し始めたことができます。
- トランザクション(取引)プロセス マクロコマンドを使用して、我々は一度や一連のアクションを取り消すに、この方法は、オペレーションのグループ全体は、すべてのトランザクションの原則を実装する任意の操作を、実行完了しない、またはしなければならないことができ