I have an interface like this:
public interface Foobar
{
default void foo( Bar bar )
{
foo( bar, 1 );
}
default void foo( Bar bar, int n )
{
for ( int i = 0; i < n; i++ )
{
foo( bar );
}
}
}
In the beginning I thought it'd be fine just as:
default void foo( byte[] b )
{
foo( b, 0, b.length );
}
void foo( byte[] b, int off, int len );
My problem is that I want to execute foo
either once or n
times.
Any implementing class may override either one or both of them. (The second method exists for batching purposes in a performance critical system)
But it seems that my solution using default
isn't good style as it is possible to override none of them and calling either leads to an infinite loop (and eventually to a StackOverflow).
So, my question is: What would be good OOP style compromise?