In the context of the design pattern state. What are the pros and cons of embedding the StateContext
as additional method argument in State
?
To make it clearer:
public void handle(Object obj);
vs. public void handle(StateContext context, Object obj);
Refactoring the context into the parameter list, instead of keeping it as class member, would increase low coupling and enhance the reuse capabilities of the object. At the same time, having the context as member would reduce the size of the parameter list and also benefit to having high cohesion.
But having a decoupled context introduces a new class of errors, where global state inconsistencies can occurr, when multiple context instances are used.
I began thinking about this problem, as I thought about a clean way to switch states.