日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Command 和 Active Object 模式

發(fā)布時(shí)間:2024/7/23 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Command 和 Active Object 模式 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Command 和 Active Object 模式

Command 模式是封裝了一個(gè)沒有任何變量的函數(shù)。

public interface Command {public void do(); }

簡單的Command

打印機(jī)工作流

開啟/關(guān)閉繼電器—RelayOnCommand、RelayOffCommand;

開啟/關(guān)閉發(fā)動機(jī)—MotorOnCommand、MotorOffCommand;

開啟/關(guān)閉離合器—ClutchOnCommand、ClutchOffCommand;

事務(wù)操作

? ? ? ? 另一個(gè)Command 模式的常見用法是創(chuàng)建和執(zhí)行事務(wù)操作(Transactions)。當(dāng)用戶決定增加一個(gè)新雇員時(shí),該用戶必須詳細(xì)指明成功創(chuàng)建一條雇員記錄所需的所有信息。在使用這些信息前,系統(tǒng)需要驗(yàn)證這些信息語法和語義上的正確性。Command 對象存儲了還未驗(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)證過的數(shù)據(jù)去更新數(shù)據(jù)庫。

?

Active Object 模式

? ? ? ? Active Object 模式是使用Command 模式的地方之一。這是實(shí)現(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;/*** 開始時(shí)間.*/private long startTime = 0l;/*** 是否已開始.*/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è)事件的多線程程序類比。當(dāng)多線程程序中的一個(gè)線程等待一個(gè)事件時(shí),它通常使用一些操作系統(tǒng)調(diào)用來阻塞自己直到事件發(fā)生。這里并沒有阻塞,如果所等待的((currentTime - startTime) < sleepTime) 這個(gè)事件沒有發(fā)生,它只是把自己放回到ActiveObjectEngine 中。

? ? ? ? 采用該技術(shù)的變體去構(gòu)建多線程系統(tǒng)已經(jīng)是一個(gè)很常見的實(shí)踐。這種類型的線程被稱為run-to-completion 任務(wù)(RTC),因?yàn)槊總€(gè)Command 實(shí)例在下一個(gè)Command 實(shí)例可以運(yùn)行之前就運(yùn)行完成了。RTC 意味著Command 實(shí)例不會阻塞。

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 模式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。