Command模式

来源:互联网 发布:汉语词典软件 编辑:程序博客网 时间:2024/05/16 15:03
Command模式是一个简单而又非常有用的模式。Command模式解除了系统逻辑和实际执行命令部分之间的耦合。Command模式常见的用法是创建和执行事务,比如一般的增删改查等动作。
Active Object模式是实现多线程控制的一个古老的技术。考虑一下以下的代码:
ActiveObjectEngine类维护了一个Command对象的链表。
    public class ActiveObjectEngine
    {
        ArrayList itsCommands = new ArrayList();


        public void AddCommand(Command c)
        {
            itsCommands.Add(c);
        }


        public void Run()
        {
            while (itsCommands.Count > 0)
            {
                Command c = (Command)itsCommands[0];
                itsCommands.RemoveAt(0);
                c.Execute();
            }
        }
    }
SleepCommand检查自己以前是否已经执行过,如果没有,就记录下开始时间。如果没有延迟时间,就把自己再加到ActiveObjectEngine中。如果过了延迟时间,就把weekup命令对象加到ActiveObjectEngine中。
    public class SleepCommand : Command
    {
        private Command wakeupCommand = null;
        private ActiveObjectEngine engine = null;
        private long sleepTime = 0;
        private DateTime startTime;
        private bool started = false;


        public SleepCommand(long milliseconds, ActiveObjectEngine e, Command wakeupCommand)
        {
            sleepTime = milliseconds;
            engine = e;
            this.wakeupCommand = wakeupCommand;
        }


        public void Execute()
        {
            DateTime currentTime = DateTime.Now;
            if (!started)
            {
                started = true;
                startTime = currentTime;
                engine.AddCommand(this);
            }
            else
            {
                TimeSpan elapsedTime = currentTime - startTime;
                if (elapsedTime.TotalMilliseconds < sleepTime)
                {
                    engine.AddCommand(this);
                }
                else
                {
                    engine.AddCommand(wakeupCommand);
                }
            }
        }
    }
DelayedTyper的行为是维持一个循环,在循环中重复打印一个指定的字符并等待一定的延迟。当stop标志被设置时,就退出循环。
   public class DelayedTyper : Command
    {
        private long itsDelay;
        private char itsChar;
        private static bool stop = false;
        private static ActiveObjectEngine engine =
        new ActiveObjectEngine();


        private class StopCommand : Command
        {
            public void Execute()
            {
                DelayedTyper.stop = true;
            }
        }


        public static void Main(string[] args)
        {
            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 StopCommand();
            engine.AddCommand(
            new SleepCommand(20000, engine, stopCommand));
            engine.Run();
        }


        public DelayedTyper(long delay, char c)
        {
            itsDelay = delay;
            itsChar = c;
        }


        public void Execute()
        {
            Console.Write(itsChar);
            if (!stop)
                DelayAndRepeat();
        }


        private void DelayAndRepeat()
        {
            engine.AddCommand(
            new SleepCommand(itsDelay, engine, this));
        }


    }
这样我们就实现了一个简单的多线程的过程。