开发者

Java Class turns invalid after a method call

开发者 https://www.devze.com 2023-03-10 02:18 出处:网络
I have a class that interfaces with an RPGLE program. The client will hold a refer开发者_开发问答ence to the class and perform certain operations.Calling methods etc.

I have a class that interfaces with an RPGLE program.

The client will hold a refer开发者_开发问答ence 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

0

精彩评论

暂无评论...
验证码 换一张
取 消