Command 和 Active Object 模式
Command 和 Active Object 模式
Command 模式是封裝了一個(gè)沒(méi)有任何變量的函數(shù)。
public interface Command {public void do(); }簡(jiǎn)單的Command
打印機(jī)工作流
開(kāi)啟/關(guān)閉繼電器—RelayOnCommand、RelayOffCommand;
開(kāi)啟/關(guān)閉發(fā)動(dòng)機(jī)—MotorOnCommand、MotorOffCommand;
開(kāi)啟/關(guān)閉離合器—ClutchOnCommand、ClutchOffCommand;
事務(wù)操作
? ? ? ? 另一個(gè)Command 模式的常見(jiàn)用法是創(chuàng)建和執(zhí)行事務(wù)操作(Transactions)。當(dāng)用戶(hù)決定增加一個(gè)新雇員時(shí),該用戶(hù)必須詳細(xì)指明成功創(chuàng)建一條雇員記錄所需的所有信息。在使用這些信息前,系統(tǒng)需要驗(yàn)證這些信息語(yǔ)法和語(yǔ)義上的正確性。Command 對(duì)象存儲(chǔ)了還未驗(yàn)證的數(shù)據(jù),實(shí)現(xiàn)了實(shí)施驗(yàn)證的方法,并且實(shí)現(xiàn)了最后執(zhí)行事務(wù)操作的方法。
? ? ? ? validate() 方法檢查所有數(shù)據(jù)并確保數(shù)據(jù)是有意義的。
? ? ? ? execute() 方法用已經(jīng)驗(yàn)證過(guò)的數(shù)據(jù)去更新數(shù)據(jù)庫(kù)。
?
Active Object 模式
? ? ? ? Active Object 模式是使用Command 模式的地方之一。這是實(shí)現(xiàn)多線(xiàn)程控制的一項(xiàng)古老的技術(shù)。
ActiveObjectEngine.java
import java.util.LinkedList;public class ActiveObjectEngine {/*** 命令鏈表.*/private LinkedList<Command> itsCommands = new LinkedList<>();/*** 添加命令.* @param c 命令.*/public void addCommand(Command c) {this.itsCommands.add(c);}/*** 運(yùn)行.*/public void run() {while (!itsCommands.isEmpty()) {Command c = itsCommands.getFirst();itsCommands.removeFirst();System.out.println("--->Command.execute()");c.execute();}} }Command.java
public interface Command {public void execute();}SleepCommandTest.java
import org.junit.Assert; import org.junit.Test;public class SleepCommandTest {private boolean commandExecuted = false;@Testpublic void testSleep() {Command wakeupCommand = new Command() {@Overridepublic void execute() {commandExecuted = true;System.out.println("WakeUp...");}};ActiveObjectEngine engine = new ActiveObjectEngine();SleepCommand sleepCommand = new SleepCommand(wakeupCommand, engine, 1000);engine.addCommand(sleepCommand);long start = System.currentTimeMillis();engine.run();long stop = System.currentTimeMillis();long sleepTime = (stop - start);Assert.assertTrue("SleepTime " + sleepTime + " expected > 900", sleepTime > 900);Assert.assertTrue("SleepTime " + sleepTime + " expected < 1100", sleepTime < 1100);Assert.assertTrue("Command Executed", commandExecuted);} }SleepCommand.java
public class SleepCommand implements Command {/*** 喚醒命令.*/private Command wakeupCommand = null;/*** 引擎.*/private ActiveObjectEngine engine = null;/*** 休眠時(shí)間.*/private long sleepTime = 0l;/*** 開(kāi)始時(shí)間.*/private long startTime = 0l;/*** 是否已開(kāi)始.*/private boolean started = false;/*** 構(gòu)造器.*/public SleepCommand(final Command wakeupCommand, final ActiveObjectEngine engine, final long milliseconds) {super();this.wakeupCommand = wakeupCommand;this.engine = engine;this.sleepTime = milliseconds;}/*** 執(zhí)行.*/@Overridepublic void execute() {long currentTime = System.currentTimeMillis();if (!started) {started = true;startTime = currentTime;engine.addCommand(this);} else if ((currentTime - startTime) < sleepTime) {engine.addCommand(this);} else {engine.addCommand(wakeupCommand);}}}????????和等待一個(gè)事件的多線(xiàn)程程序類(lèi)比。當(dāng)多線(xiàn)程程序中的一個(gè)線(xiàn)程等待一個(gè)事件時(shí),它通常使用一些操作系統(tǒng)調(diào)用來(lái)阻塞自己直到事件發(fā)生。這里并沒(méi)有阻塞,如果所等待的((currentTime - startTime) < sleepTime) 這個(gè)事件沒(méi)有發(fā)生,它只是把自己放回到ActiveObjectEngine 中。
? ? ? ? 采用該技術(shù)的變體去構(gòu)建多線(xiàn)程系統(tǒng)已經(jīng)是一個(gè)很常見(jiàn)的實(shí)踐。這種類(lèi)型的線(xiàn)程被稱(chēng)為run-to-completion 任務(wù)(RTC),因?yàn)槊總€(gè)Command 實(shí)例在下一個(gè)Command 實(shí)例可以運(yùn)行之前就運(yùn)行完成了。RTC 意味著Command 實(shí)例不會(huì)阻塞。
DelayedCommand.java
public class DelayedCommand implements Command {/*** 延遲毫秒數(shù).*/private long itsDelay;/*** 字符.*/private char itsChar;/*** 引擎.*/private static ActiveObjectEngine engine = new ActiveObjectEngine();/*** 是否停用.*/private static boolean stop = false;public static void main(String[] args) {engine.addCommand(new DelayedCommand(100, '1'));engine.addCommand(new DelayedCommand(300, '3'));engine.addCommand(new DelayedCommand(500, '5'));engine.addCommand(new DelayedCommand(700, '7'));Command stopCommand = new Command() {@Overridepublic void execute() {DelayedCommand.stop = true;}};engine.addCommand(new SleepCommand(stopCommand, engine, 2000));engine.run();}/*** 構(gòu)造器.*/public DelayedCommand(long delay, char c) {super();itsDelay = delay;itsChar = c;}/*** 執(zhí)行.*/@Overridepublic void execute() {System.out.print(itsChar);if (!stop) {delayAndRepeat();}}private void delayAndRepeat() {engine.addCommand(new SleepCommand(this, engine, itsDelay));} }總結(jié)
以上是生活随笔為你收集整理的Command 和 Active Object 模式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Preserve Whole Objec
- 下一篇: Redis系列一、redis介绍与安装