Should an object write itself out to a file, or should another object act on it to perform I/O?

后端 未结 7 1683
予麋鹿
予麋鹿 2020-12-23 02:06

NOTE: Sorry for the long question!

I\'m trying to understand some key areas behind object orientation and I couldn\'t decide one way or another about my par

相关标签:
7条回答
  • 2020-12-23 02:23

    The correct approach, in general, is your Case 1. This maintains a single responsibility for the class (whatever it does) without coupling it to a specific persistence mechanism (a disk).

    You're looking at a specific case of a more generalized problem: Serialization. It's good and OK for an object to have some means to indicate how it should be serialized-- it's the only entity that knows what's necessary to deserialize it, after all. But if you make the object save itself to disk, you've tightly coupled that object to a specific implementation.

    Instead, consider creating an interface that a generalized "writer" can use to "serialize" the object to whatever that writer serializes to. This way, you'll be able to serialize to disk, to the network, to memory, to whatever you actually need to serialize to. :)

    0 讨论(0)
  • 2020-12-23 02:25

    This is an example of where the Strategy Design Pattern could be used. Your myBob object could have an instance of the class that will write it out. You may want the writer to implement an interface or derive from an abstract class so that the save routine can be easily changed.
    Today you are saving to xml, but you might need to eventually persist the object to a database as well. This pattern will allow you to change the save routine easily. You would even have the option to change how you save at runtime.

    0 讨论(0)
  • 2020-12-23 02:35

    I would make Bob know how to serialize itself since it has private data. Another object (such as your Writer) would take that and put it on disk. Bob knows how to deal with its data best but it need not care about how or where that is stored. Your Writer knows how to save data best but it need not care about how that data is created.

    0 讨论(0)
  • 2020-12-23 02:35

    Do this:

    public interface Writable {
        public void Save(Writer w);
    }
    
    public interface Writer {
        public void WriteTag(String tag, String cdata);
    }
    
    public class Bob : Writable {
        private String ssn = "123-23-1234";
        public void Save(Writer w) {
            w.WriteTag("ssn", ssn);
        }
    }
    
    public class XmlWriter : Writer {
        public XmlWriter(Sting filename) {...}
        public void WriteTag(String tag, Sting cdata) {...}
    }
    

    Obviously this isn't a complete solution but you should get the general idea.

    0 讨论(0)
  • 2020-12-23 02:44

    One other method is to use the visitor pattern. Have your object contain an Accept method that goes through the members you want to process/serialize, and have a visitor be your serializer. Whenever you update or change your serialization (plain text to xml to binary to whatever), you don't need to update the object.

    We've had good experiences at work doing it this way. It's pretty powerful.

    0 讨论(0)
  • 2020-12-23 02:46

    I used to prefer option 2; however, as I have started really trying to understand and model the domains I am working on, I prefer option 1.

    Imagine if your modelling Vehicles. Why would a vehicle know how to persist itself? It might know how to move, how to start and how to stop, but what is Save within the context of a vehicle.

    0 讨论(0)
提交回复
热议问题