看了我好久才看懂的代码。
ActiveObjectEngine.java
- package active;
- /**
- *
- * @author Administrator
- */
- import java.util.LinkedList;
- public class ActiveObjectEngine {
- LinkedList itsCommands = new LinkedList();
- public void addCommand(Command c) {
- itsCommands.add(c);
- }
- public void run() {
- while (!itsCommands.isEmpty()) {
- Command c = (Command) itsCommands.getFirst();
- itsCommands.removeFirst();
- try {
- c.execute();
- } catch (Exception e) {
- }
- }
- }
- }
Command.java
- package active;
- /**
- *
- * @author Administrator
- */
- public interface Command {
- public void execute() throws Exception;
- }
DelayedTyper.java
- package active;
- /**
- *
- * @author Administrator
- */
- public class DelayedTyper implements Command{
- private long itsDelay;
- private char itsChar;
- private static ActiveObjectEngine engine=new ActiveObjectEngine();
- private static boolean stop=false;
- public static void main(String args[]) throws Exception{
- engine.addCommand(new DelayedTyper(100,'1'));
- engine.addCommand(new DelayedTyper(300,'3'));
- engine.addCommand(new DelayedTyper(500,'5'));
- engine.addCommand(new DelayedTyper(700,'7'));
- Command stopCommand=new Command(){
- public void execute(){
- stop=true;
- }
- };
- engine.addCommand(new SleepCommand(20000,engine,stopCommand));
- engine.run();
- }
- public DelayedTyper(long delay,char c){
- itsDelay=delay;
- itsChar=c;
- }
- public void execute() throws Exception{
- System.out.print(itsChar);
- if(!stop){
- delayAndRepeat();
- }
- }
- public void delayAndRepeat() throws CloneNotSupportedException{
- engine.addCommand(new SleepCommand(itsDelay,engine,this));
- }
- }
SleepCommand.java
- package active;
- /**
- *
- * @author Administrator
- */
- public class SleepCommand implements Command{
- private Command wakeupCommand=null;
- private ActiveObjectEngine engine=null;
- private long sleepTime=0;
- private long startTime=0;
- private boolean started=false;
- public SleepCommand(long milliseconds,ActiveObjectEngine e,Command wakeupCommand){
- sleepTime=milliseconds;
- engine=e;
- this.wakeupCommand=wakeupCommand;
- }
- public void execute() throws Exception{
- long currentTime=System.currentTimeMillis();
- if(!started){
- started=true;
- startTime=currentTime;
- engine.addCommand(this);
- }else if((currentTime-startTime)
- engine.addCommand(this);
- }else{
- engine.addCommand(wakeupCommand);
- }
- }
- }
SleepCommandTest.java
- package active;
- import org.junit.After;
- import org.junit.AfterClass;
- import org.junit.Before;
- import org.junit.BeforeClass;
- import org.junit.Test;
- import static org.junit.Assert.*;
- /**
- *
- * @author Administrator
- */
- public class SleepCommandTest {
- public SleepCommandTest() {
- }
- @BeforeClass
- public static void setUpClass() throws Exception {
- }
- @AfterClass
- public static void tearDownClass() throws Exception {
- }
- @Before
- public void setUp() throws Exception {
- }
- @After
- public void tearDown() throws Exception {
- }
- public boolean commandExecuted=false;
- @Test
- public void execute() throws Exception {
- Command wakeup=new Command(){
- public void execute(){commandExecuted=true;}
- };
- ActiveObjectEngine e=new ActiveObjectEngine();
- SleepCommand c=new SleepCommand(1000,e,wakeup);
- e.addCommand(c);
- long start=System.currentTimeMillis();
- e.run();
- long stop=System.currentTimeMillis();
- long sleepTime=(stop-start);
- System.out.println(sleepTime);
- assert(sleepTime>1000);
- assert(sleepTime>1000);
- assert(commandExecuted);
- } /* Test of execute method, of class SleepCommand. */
- }