Mojtaba
Mojtaba

Reputation: 1240

what is good pattern to de serialize object?

I have a class imagine Entity, Entity class wants to provide deserialize method this method accept a xml string and it will rebuild the object. which one looks better? 1- having a constructor which accept xmlString parameter and there deSerilaize Entity. 2- having a constructor without parameter and then call deserialize method with xmlString. 3- having a static method in Entity accept xmString as parameter and return a deserialized Entity as a function call. or any other solution?

Upvotes: 0

Views: 1120

Answers (1)

Justin Niessner
Justin Niessner

Reputation: 245399

Neither. An object shouldn't be concerned with Serializing/Deserializing themselves at all. Those operations should be handled by an object that is dedicated to handling the serialization logic.

Not only does this make the Entity class simpler to work with, but it also opens your architecture to be flexible and handle different serialization methods in the future. Remember, decoupling behavior in your application is good.

Upvotes: 3

Related Questions