Assuming that I have a processElement
function like this:
class InputProcessor {
public void processElement(T element) {
nextOperator.processElement(element);
}
}
Now I'd like to add an on/off switch to count the elements during the "switch is on" period. However I don't want to add an "if else" like this because it may downgrade the performance.
class InputProcessor {
public void processElement(T element) {
if (on) {
count++;
}
nextOperator.processElement(element);
}
}
Is there any way to help implement this? I have an idea but I'm not sure it works as expected(no performance degrading). I'll create an CountInputProcessor
just like the InputProcessor
except for the count part.
class CountInputProcessor {
public void processElement(T element) {
count++;
nextOperator.processElement(element);
}
}
And when I switch the feature on, I redefine the operator(the InputProcessor
is stateless).
inputProcessor = new CountInputProcessor();
inputProcessor.processElement(element);
And when I switch the feature off, I redefine it as the old InputProcessor
.