Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 ?

share|improve this question
up vote 5 down vote accepted

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.

share|improve this answer
+1 for the refactoring concept – guiman Mar 18 '11 at 14:10
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 '11 at 15:40

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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