NOTE. Sorry for the long question!
I am trying to understand some of the key areas behind object orientation, and I could not somehow solve my specific question.
Say I have an object full of beautiful data. Class bob.
Bob myBob = new Bob("This string is data");
Let's say I want to save the contents of myBob in an xml file (bob.xml)
Do I have to force the object to act on bob to write the content, or do I need myBob to do this?
Case 1: action on an object
Writer myWriter = new Writer(myBob, "C:\\bob.xml");
Case 2: Save Method
myBob.Save("C:\\bob.xml");
Some people support one option, as this means that if the code for writing files has been changed, it does not need to update each Save method; I assume that code reuse will happen again. My problem is getting all the data from objects that may have personal data without access.
The case for the second option is that the method acts only on the data stored in the object, and how it should be. No interference from other objects.
Or is the answer to my question one of those questions that depend on the case? If so, how do you know when one method is preferable to another?
c # oop design-patterns io
Andrew Weir May 23 '09 at 1:47 p.m. 2009-05-23 13:47
source share