Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have some code that needs to be executed at specific user-defined times of day (e.g. at 11:45:30 am). My code looks like this:

void OnTimer(UINT nTimerID)
{
    if(nTimerID == somethingTimerId_)
    {
        presenter_.DoSomething();
    }
    else if(nTimerID == updateClockTimerId_)
    {
        // this timer goes off once per second.
        UpdateTimeDisplay();
        presenter_.ExecuteTaskIfExecuteTime();
        presenter_.ExecuteOtherTaskIfExecuteTime();
    }
}

ExecuteTaskIfExecuteTime() checks the curent time and checks a list for times to execute it's task; if the current time is one of those times, it then executes the task.

ExecuteOtherTaskIfExecuteTime() does the same thing but has it's own list of times to execute and it's own task.

Now I need to add a third task type and I'm feeling like it's time to generalize this and make it more flexible.

So I'm wondering if a command pattern would be appropriate here. What I'm thinking is making a command for each type of task, I'll add a command object with a time to execute to a list of commands and iterate over that list in once per second. For each command object it will compare the command time against the current time and if they are equal (or within a small threshold) execute the command object. So my code would become:

else if(nTimerID == updateClockTimerId_)
{
    // this timer goes off once per second.
    UpdateTimeDisplay();
    presenter_.ProcessCommands();
}

Is this a correct use of the command pattern? I'm open to different ideas as well.

share|improve this question
Looks legit. But this question would probably get better traction at codereview stackexchange site. – Spoike Apr 18 '12 at 10:37
To me it's more of conceptual question which I thought was perfect for programmers.stackexchange but ultimately I want some guidance so wherever I can get that best works for me. – User Apr 18 '12 at 14:58

migrated from programmers.stackexchange.com Apr 18 '12 at 12:04

1 Answer

up vote 1 down vote accepted

Congratulations, the command pattern is the right choice. The abstract Command defines the interface to execute an operation. For every task type implement a concrete subclass of Command. Override the execute operation to execute the specific task. The timer does not need to know about the different kinds of tasks. It simply executes the scheduled Command instances at the right time. This solution is easy to extend if you are confronted with a fourth and fifth kind of task to execute.

share|improve this answer
1  
If you have many commands, it's also common to store them in a (priority) queue ordered by time. Then iteration can stop as soon as you reach the first command which isn't due yet. – Useless Apr 18 '12 at 12:17
@Useless: I like the sound of the priority queue. How would that be implemented? Would I have time fields on the command objects themselves or some kind of holder struct/class that has a slot for the time and slot for the command object? – User Apr 18 '12 at 15:31
I'd probably keep them seperate, unless there's a particular reason the action and the scheduling behaviour need to be tightly coupled. Recurring actions, or actions which may or may not need to be re-scheduled to run again, require a bit more thought. – Useless Apr 18 '12 at 16:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.