Our application is built somewhat around a Context class with many primitive fields and booleans The Context is passed around in almost all of the flow, and decisions are taken based on the boolean flags. Now in order to implement new functionality, a new boolean was added to the context and it has to be checked in 10 different places. The general flow has structure similar to this example.
public void handle(context) {
if (context.isBig())
drawBigThing(context)
else
drawSmallThing(context)
//more code
...... handleColor(context) //somewhere deeper in the flow/stack
}
private void handleColor(context) {
if (context.isBig())
takeMoreColor(context.getColor())
else
takeLessColor(context.getColor())
}
As you see in different parts of the code we look back at the same flag but take different decisions based on it. Now if i add context.isVeryBig() you can see how this might explode.
What are some ideas (on java 8 tools) to refactor the boolean flag that is queried from methods/classes with different responsibilities but still interested in the same flag?
One idea would be to make the context smarter, not hold boolean flags but a State/Strategy for each of the responsibilities, but this leaks the responsibilities in the context (maybe somehow they can be decoupled?), i will still have the IFs but at least they will be grouped in one place and before the flow starts
Aucun commentaire:
Enregistrer un commentaire