What you are looking for here is a Strategy design template.
The purpose of this template is to drop the implementation of the algorithm into a strategy object. Here, your algorithms are the func and crit functions that you want to pass.
So, you will have an interface called TraceStrategy . You will then pass the implementations of this interface to your collection. Then your code would look like
void Trace(TraceStrategy traceStrategy) { item i = firstItem(); while (i != endItem()) { i = nextItem(); traceStrategy.func(i); if (traceStrategy.crit(i)) return; } }
and
interface TraceStrategy { public boolean crit(item i); public void func(item i); }
You probably want to make this generic so that you are not tied to item ... but you understand this idea.
source share