I have an object containing the results of a computation. This computation is performed in a function which accepts an input object and returns the result object.

The result object has a print method. This print method must print out the results, but in order to perform this operation I need the original input object. I cannot pass the input object at printing because it would violate the signature of the print function. One solution I am using right now is to have the result object hold a pointer to the original input object, but I don't like this dependency between the two, because the input object is mutable.

How would you design for such case ?

1 Answer 1


In the calculation and/or result object, take the input object and extract all the information you'll need to do the print. Bundle that all up into some kind of immutable structure and save that in your result object. Then you've taken a snapshot of the data you need without keeping a reference to the mutable object.

  • In short, your result object is lacking data. Refactor and include that data when the object is generated (while the input object is still easily accessible).
    – gablin
    Mar 18, 2011 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.