ActiveObject模式:

ActiveObject模式和Command模式的配合使用是实现多线程控制的一项古老的技术,该模式有多种使用方式,为许多工业系统提供了一个简单的多任务核心。

// 活动对象的工具类,封装添加命令的方法以及运行方法

class ActiveObjectEngine
{
ArrayList itsCommands = new ArrayList();
// 添加命令 //
public void AddCommand(Command c)
{
itsCommands.Add(c);
}
// 运行命令,并且移除 //
public void Run()
{
while (itsCommands.Count > )
{
Command c = (Command)itsCommands[];
itsCommands.RemoveAt();
c.Execute();
}
}
}

Run()函数只是遍历列表,执行并移除每个命令。如此,似乎也没给人很深刻的印象,但是发挥一下想象,如果链表中的command对象会克隆自己并把克隆对象放到链表的尾部,会是什么情况?可见这个链表不会为空,Run()永远不会返回。

// 唤醒命令,类似于undo()函数 //
class WakeUpCommand : Command
{
public bool executed = false;
public void Execute()
{
executed = true;
}
}
// 睡眠命令 //
class SleepCommand : Command
{
private Command wakeupCommand = null;
private ActiveObjectEngine engine = null;
private long sleepTime = ;
private System.DateTime startTime;
private bool started = false; public SleepCommand(long milliseconds, ActiveObjectEngine e,Command wakeupCommand)
{
sleepTime = milliseconds;
engine = e;
this.wakeupCommand = wakeupCommand;
}
public void Execute()
{
System.DateTime current = DateTime.Now;
if (!started)
{
Debug.Log("one..................add this");
started = true;
startTime = current;
engine.AddCommand(this);
}
else
{
TimeSpan elapsedTime = current - startTime;
Debug.Log("__" + elapsedTime.TotalMilliseconds);
if (elapsedTime.TotalMilliseconds < sleepTime)
{
engine.AddCommand(this);
Debug.Log("..................add this");
}
else
{
engine.AddCommand(wakeupCommand);
Debug.Log("..................add wakeup");
}
}
}
}

可见它的构造方法有三个参数,第一个是延迟时间,第二个是活动对象的工具类,第三个是唤醒命令类,分析代码可以看出,当前时间小于延迟时间的时候,会重复的调用自身(sleepCommand)的excute(),当大于延迟时间,就调用wakeupCommand,表示任务处理结束。

然后调用以上代码:

// 这里测试环境是unity3d,所以会看到Start()函数
void Start () {
WakeUpCommand wakeup = new WakeUpCommand();
ActiveObjectEngine e = new ActiveObjectEngine();
SleepCommand c = new SleepCommand(, e, wakeup);
e.AddCommand(c);
DateTime start = DateTime.Now;
e.Run(); DateTime stop = DateTime.Now;
TimeSpan sleepTime = stop - start; print("command end:" + wakeup.executed + " run time:" + sleepTime.TotalMilliseconds);
}

打印的结果,wakeup.executed : true, 运行时间是一个大于900毫秒的数。

我们可以将该程序和等待一个事件的多线程程序做一个对比,多线程程序等待的时候,通常是使用操作系统调用来阻塞自己直到事件发生。

而上面的代码并没有阻塞,采用该技术的变体实现多线程是一个很优秀的方式,这种类型的线程称为run-to-completion任务(RTC),它的优点就是共享同一个运行时栈。这在需要大量线程的内存受限系统中是一个强大的优势。

以下代码是一个多线程的实例

class DelayedTyper : Command
{
private long itsDelay;
private char itsChar;
private static bool stop = false;
private static ActiveObjectEngine engine = new ActiveObjectEngine(); class StopCommand : Command
{
public void Execute()
{
DelayedTyper.stop = true;
}
}
public DelayedTyper(long delay, char c)
{
itsDelay = delay;
itsChar = c;
} public void Execute()
{
Debug.Log(itsChar);
if (!stop)
{
DelayAndRepeat();
}
}
public void DelayAndRepeat()
{
engine.AddCommand(new SleepCommand(itsDelay, engine, this));
} public static void Go()
{
engine.AddCommand(new DelayedTyper(, ''));
engine.AddCommand(new DelayedTyper(, ''));
engine.AddCommand(new DelayedTyper(, ''));
engine.AddCommand(new DelayedTyper(, '')); Command stopCommand = new StopCommand();
engine.AddCommand(new SleepCommand(, engine, stopCommand));
engine.Run();
}
}

在Start函数添加:

DelayedTyper.Go();

打印的结果可以看出,每次的都会不一样,这是因为CPU的时钟和实时时钟没有完美的同步,这种不确定的行为不就是多线程系统的特点么。

开发设计模式(二) ActiveObject模式的更多相关文章

  1. 游戏开发设计模式之状态模式 & 有限状态机 & c#委托事件(unity3d 示例实现)

    命令模式:游戏开发设计模式之命令模式(unity3d 示例实现) 对象池模式:游戏开发设计模式之对象池模式(unity3d 示例实现) 原型模式:游戏开发设计模式之原型模式 & unity3d ...

  2. 游戏开发设计模式之原型模式 & unity3d JSON的使用(unity3d 示例实现)

    命令模式:游戏开发设计模式之命令模式(unity3d 示例实现) 对象池模式:游戏开发设计模式之对象池模式(unity3d 示例实现) 实现原型模式 原型模式带来的好处就是,想要构建生成任意独特对象的 ...

  3. 游戏开发设计模式之命令模式(unity3d 示例实现)

    博主才学尚浅,难免会有错误,尤其是设计模式这种极富禅意且需要大量经验的东西,如果哪里书写错误或有遗漏,还请各位前辈指正. 打 算写设计模式的目的就是,首先自己可以理清思路,还有就是国内的设计模式资料很 ...

  4. 游戏开发设计模式之对象池模式(unity3d 示例实现)

    前篇:游戏开发设计模式之命令模式(unity3d 示例实现) 博主才学尚浅,难免会有错误,尤其是设计模式这种极富禅意且需要大量经验的东西,如果哪里书写错误或有遗漏,还请各位前辈指正. 原理:从一个固定 ...

  5. 项目开发设计模式理解之MVC模式

    项目开发设计模式之MVC模式: M model 模型层 V view 视图层 C control 控制器 MVC模式在B/S架构下使用很广泛的软件设计模式,分成三个相对独立的模块构成,model+vi ...

  6. C#设计模式总结 C#设计模式(22)——访问者模式(Vistor Pattern) C#设计模式总结 .NET Core launch.json 简介 利用Bootstrap Paginator插件和knockout.js完成分页功能 图片在线裁剪和图片上传总结 循序渐进学.Net Core Web Api开发系列【2】:利用Swagger调试WebApi

    C#设计模式总结 一. 设计原则 使用设计模式的根本原因是适应变化,提高代码复用率,使软件更具有可维护性和可扩展性.并且,在进行设计的时候,也需要遵循以下几个原则:单一职责原则.开放封闭原则.里氏代替 ...

  7. 设计模式 ( 二十 ) 访问者模式Visitor(对象行为型)

    设计模式 ( 二十 ) 访问者模式Visitor(对象行为型) 1.概述 在软件开发过程中,对于系统中的某些对象,它们存储在同一个集合collection中,且具有不同的类型,而且对于该集合中的对象, ...

  8. PHP微信支付开发之扫描支付(模式二)后如何回调

    其实在写这篇文章的时候感觉自己已经落伍了,不过笔者在百度上搜索"微信支付开发之扫描支付(模式二)后如何回调"寻找答案时,发现依旧有很多朋友没有解决这个问题,所以就把自己的解决思路分 ...

  9. C#设计模式之二十二访问者模式(Visitor Pattern)【行为型】

    一.引言   今天我们开始讲"行为型"设计模式的第九个模式,该模式是[访问者模式],英文名称是:Visitor Pattern.如果按老规矩,先从名称上来看看这个模式,我根本不能获 ...

随机推荐

  1. 去掉Eclipse中的Validating

    去掉Eclipse中的Validating 最近我的Eclipse一直经常效验javascript,我疯了校验了一个多小时还是在验.我只能在项目的.project文件中: 去掉.project文件中的 ...

  2. windows7怎么共享文件夹

    http://jingyan.baidu.com/article/d45ad148f06fef69552b80e6.html

  3. iOS开发几年了,你清楚OC中的这些东西么1

    前言 几年前笔者是使用Objective-C进行iOS开发, 不过在两年前Apple发布swift的时候,就开始了swift的学习, 在swift1.2发布后就正式并且一直都使用了swift进行iOS ...

  4. ExtJs 继承 和 插件 示例

    Ext.ns('Ext.ux'); function btn(){ alert(this.id); }; var panel_plugs = {//定义插件 init : function(panel ...

  5. c/c++指针基础使用

    #include <iostream> #include <string> using namespace std; int main() { "; "; ...

  6. supervisor tornado 多进程多端口配置

    base: nginx tornado 目标: tornado 实现多端口多进程运行 pip install supervisor echo_supervisord_conf > /etc/su ...

  7. sed示例

    引用:http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856901.html [root@www ~]# sed [-nefr] [动作] 选 ...

  8. List集合即其遍历

    1. 首先List<E>集合继承与Collection<E>,是一个接口. ①  Collection (集合框架是JDK1.2版本出现的) ②   list:是有序的,元素可 ...

  9. cocos2d-x实战 C++卷 学习笔记--第7章 动作、特效(一)

    前言: 介绍cocos2d-x中的动作.特效. 动作: 动作(action)包括基本动作和基本动作的组合,这些基本动作有缩放.移动.旋转等,而且这些动作变化的速度也可以设定. 动作类是 Action. ...

  10. cocos2d-x实战 C++卷 学习笔记--第4章 使用标签

    前言: 介绍cocos2d-x中 标签类. cocos2d-x中 标签类 主要有三种:LabelTTF, LabelAtlas, 和 LabelBMFont.此外,在Cocos2d-x 3.x之后推出 ...