I have a class that interfaces with an RPGLE program.
The client will hold a reference to the class and perform certain operations. Calling methods etc.
After a call to a particular method – endDocument(), the class is then is an invalid state because I call destroy() on a number of objects (which is required by the API).
Other than having a boolean member flag “illigalState” and at the top of every method checking it, throwing an IllegalStateException(), how might I handle this problem?
I don’t want the user to be able to use the class after it is in this illegal state, but they have a reference to it.
A more OO way would be to use the State pattern. You could have two object states; valid (which contains the logic) and invalid which throws IllegalStateExceptions. Object state would need to be changed in the endDocument method