I have an object to populate with xml. I think that I might use a contructor which accept this xml to build the object. But, is there a better design pattern to do it ? Is it to the object to handle all the xml parsing ?
Thank you !
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No you should not send the xml to the constructor because you are coupling the model with deserilizing logic. You should create a class that handles the deserlization for you and return an instance. Why this is important might be unclear. But imagine in the future you may have the data coming from a database, you don’t want to pass the database connection to the constructor, do you? Instead you can create a different class that uses databases to generate an instance of your class.
Here are some good read:
http://en.wikipedia.org/wiki/Builder_pattern
http://en.wikipedia.org/wiki/Immutable_object