Question

I currently have code like this to display some objects that meet some criteria in a grid:

// simplified
void MyDialog::OnTimer()
{
    UpdateDisplay();
}

void MyDialog::UpdateDisplay()
{
    std::list<MyClass *> objects;
    someService_->GetObjectsMeetingCriteria(objects, sortField, sortDirection);

    for(std::list<MyClass *>::iterator it = objects.begin();
        it != objects.end() ; it ++)
    {
        AddObjectToGrid(*it);
    }

}   

This works fine. Now the requirement has come in to process these objects when they meet some criteria. The information on the objects can change quickly so it would be most desirable to check if an object meets criteria and then process it immediately and continue on in this fashion.

My question is how to best architect this to handle the display and processing. I could simply add a method to process the object such as:

for(std::list<MyClass *>::iterator it = objects.begin();
    it != objects.end() ; it ++)
{
    ProcessObject(*it);
    AddObjectToGrid(*it);
}

However ideally I'd check if an object meets the criteria immediately before processing it to ensure its acting on the most recent info. In this example, all the objects are checked to see if they match the criteria and then afterwards each one is processed.

Also I'm concerned this is coupling the processing code with the display code although I'm not sure how to separate them or if it's even necessary. I could solve that like this:

void MyDialog::OnTimer()
{
    ProcessObjects();
    UpdateDisplay();
}

But then I'm iterating over the list of objects twice, once to process and once to display.

Finally I could do something like:

// simplified
void MyDialog::OnTimer()
{
    ProcessAndDisplayObjects();
}

void MyDialog::ProcessAndDisplayObjects()
{
    std::list<MyClass *> objects;
    someService_->GetAll(objects, sortField, sortDirection);

    for(std::list<MyClass *>::iterator it = objects.begin();
        it != objects.end() ; it ++)
    {
        if(someService->MeetsCriteria(*it))
        {
          ProcessObject(*it);
          AddObjectToGrid(*it);
        }
    }
}   

Overall what's holding me up is I'm worried about coupling the display and processing code together and the code running efficiently because timely processing is critical. How can I best structure this code?

No correct solution

Licensed under: CC-BY-SA with attribution
scroll top