I'm trying to refactor some code, and one of the major changes is to remove the (ab)use of static classes to give global state.
I've tried to split up some of the 'global state' functionality with POJOs that can be passed around.
But the further down the rabbit hole I go the more and more I feel like this is an Anti-Pattern.
Is holding state in POJOs and then passing them around bad form? It means the outline of my methods often look like this:
public MyPojo makeReport(int myInt, String pathToInput, MyPojo myPojo){
...
if(myPojo.whatKindOfReport()==Reports.reportTypes){
...
}
...
myPojo.isWritingReportToFileSuccessful(true);
....
return myPojo;
}
Where the POJO 'state' is set from the GUI that the user interacts with. For instance with tickboxes for report types etc.
But this blocks other return types, and seems overly complicated.
I've taken this approach because the application has several 'paths' (see whatAmIDoing
) the user can take in running it, each of which has several stages. What options the user has depends heavily on what path they pick and then from there, the result of that choice (normally, success or failure, see myPojo.isSomethingSuccessful(true)
). I'm trying to encode that information in a POJO rather than a Global State Class.
Is this an anti-pattern? How can I get round this?
Edit: I think what I'm after is something like Chain of Responsibility. After looking at this answer I'm not sure.
myPojo.whatKindOfReport()==Reports.reportTypes
=>myPojo.isKindOf(Reports.reportTypes)
ormyPojo.hasKindOf(Reports.reportTypes)