I was developing a simple class, called Simulator
, that applies a list of Simulation
s to a certain input. For each input the simulations can produce an output or not, based on some conditions that the input has to fulfill with respect to each simulation. The result produced by the Simulator
is a list of output.
Here is the code.
class Simulator {
final List<Simulation> simulations;
// Some initialization code...
List<Ouput> execute(Input input) {
return simulations
.stream()
.filter(s -> s.processable(input))
.map(s -> s.prepareOutput(input))
.collect(Collectors.toList());
}
}
As you can see, first I verify if an input is processable by a Simulation
, filtering the simulations for which it is not, and then applying these simulations to the input.
From a object oriented point of view, I am exposing internals of Simulation
class. The check operation done by the processable
method should be hide inside the prepareOutput
method.
However, having processable
visible to Simulator
, I can apply a more functional approach, which is very convenient.
Which approach is better? Is there any other solutions I am missing?