我们假设存在以下类:
class MySession {
public:
run(); // can only be invoked once, starts a child _run() process as an async process
suspend();
resume();
private:
// basic implementation
run_() {
while(condition) {
runMyDefaultOperation();
// either waits for interval, or if suspended, waits until resume is called
waitFor(interval);
}
}
};
这非常好用,但它在可扩展性方面有所限制。这种实现限制我暂停"块"。为了解决这个问题,我需要检查is_suspended
状态是否存在任意较小的代码块。
这种设计模式是否可以接受,还是我应该使用另一种设计模式代替这样的任务?
答案 0 :(得分:0)
我认为这种设计模式是可以接受的,也是可扩展的:这里的可伸缩性方法是将Session的作业分成更小的块,因此runMyDefaultOperation()每次都会执行一小段代码。
您可以在某种程度上使用command pattern来帮助构建工作。
作为旁注,实施suspend
,resume
和wait
的合适机制是condition variable。