1

Let's say I have a system. In this system I have a number of operations I can do, but all of these operations have to happen as a batch at a certain time, while calls to activate and deactivate these operations can come in at any time. No matter how many times the doOperation1() method/function is called the operation is done only once in the batch. To implement this, I could use flags like doOperation1 and doOperation2 but this seems like it would become difficult to maintain. Is there a design pattern, or something similar, that addresses this situation?

2

You may want to look at the Visitor Pattern. It allows you to decouple operations from the objects they work on. If you extend this pattern, to place those operations into a queue, and then process the queue as a batch, this may work.

  • Thanks chris, I'll look into it! I'd vote up if I could. – Mikayla Maki May 27 '14 at 19:41
0
class A {
    void foo() { ... }
}

class DoFooOnce {
    bool activated = false;

    void activateFoo (A obj) {
        if (!activated) {
            activated = true;
            obj.foo();
        }
  }
}


main () {
    A obj1;
    B obj2;
    C obj3;

    DoFooOnce guard;

    guard.activateFoo(obj1);
    guard.activateFoo(obj2);
    guard.activateFoo(obj3);
}
  • The problem with this implementation is that it will quickly become unmanageable. Imagine having a dozen of these; it would smell awful and it implies the need for a more interesting approach. This also happens to be the way I solved this problem in my code. – Mikayla Maki May 30 '14 at 2:45
  • FWIW, if you have a language with strong template capabilities (e.g. C++), only one such class would need to be created that could handle various object types with various methods. – Thomas Eding May 30 '14 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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