I have a complex object which routinely needs to compute a sub-object representing various aspects of the parent's state as a bundle. For example, imagine the object represents information about an aircraft and the sub-object is a summary of various key parameters of the aircraft at some particular time such as its heading, speed, etc. The object needs to store the last computed version of the sub-oject.
Currently the way I do this is that I have a void method in the object that does the computation and then sets a module-level variable to be equal to the newly computed sub-oject. The outside user of this sub-object then retrieves the current sub-oject via a getter. So, for the client methods the operation looks like this:
main_object.computeState();
State new_state = main_object.getState();
To handle possible errors, however, in the computation, which is currently not done, I am thinking of changing to a compute method that returns the state:
State new_state = main_object.computeState( error_msg );
if( new_state == null ){
print( error_msg );
goto failure continuation
}
.... [everything ok, continue normally ]
Is there a better strategy for constructing this pattern?