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));
}
}
这样我们就实现了一个简单的多线程的过程。
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));
}
}
这样我们就实现了一个简单的多线程的过程。
- Command模式
- Command模式
- Command模式
- Command 模式
- Command 模式
- Command模式
- command模式
- Command模式
- Command模式
- Command模式
- Command 模式
- Command 模式
- Command模式
- Command模式
- Command模式
- COMMAND 模式
- Command模式
- Command模式
- TOMCAT加载
- windows 下利用dos命令合并几个txt文件为一个文件
- 查询同一个表中重复的字段
- .NET Framework 4.0下开发的软件,打包后不能在XP SP2下运行
- 在写两次调用service读取dao的函数时出现的错误
- Command模式
- iPhone4/iPhone4S iOS5.1.1完美越狱教程(Absinthe2.0)
- Template Method模式和Strategy模式
- 让数据库变快的10个建议
- 申请Google Map Android API Key
- EAS-BOS查询统计开发步骤
- Facade模式和Mediator模式
- 控件篇 - TextBox的GotFocus事件触发问题
- 解决关于IIS gzip 不能正常启用的问题!