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

后端 未结 7 1736
予麋鹿
予麋鹿 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: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.

提交回复
热议问题