I have some code where I believe the SLA (single level of abstraction) principle is broken in a way that the low-level code (if-statements) is outside and the high level code is nested inside the cases. A simplified version of the code is:
public void GenerateReport(int effort, bool delayed)
{
if (effort < 5)
{
GenerateSimpleReport();
}
else if (effort < 20)
{
GenerateNormalReport();
}
else if (delayed)
{
GenerateDangerReport();
}
else
{
GenerateAwesomeReport();
}
}
I would like to have the individual conditions for the different kind of reports at the abstraction level of those different report types, not at the abstraction level of the caller. I still don't want to move the condition tests into the Generate...Report()
methods directly.
Which approach (maybe a design pattern) would be helpful here to refactor this code so that the SLA principle would be obeyed? If available, I'd like to use a well-known, documented pattern, but I am also open for other ideas.
Regarding the suggested duplicate: If I apply the Specification pattern, the code probably looks like
if (simpleSpecification.IsSatisfiedBy(effort, delayed))
{
GenerateSimpleReport();
}
if (normalSpecification.IsSatisfiedBy(effort, delayed))
{
GenerateNormalReport();
}
if (dangerSpecification.IsSatisfiedBy(effort, delayed))
{
GenerateDangerReport();
}
if (awesomeSpecification.IsSatisfiedBy(effort, delayed))
{
GenerateAwesomeReport();
}
and I don't see how this solves the problem, the conditions are still separated from the GenerateXxx()
methods.