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?
0
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.
1
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);
}
2