There is a third-party api for receiving data (read only). Since the data comes in the form of json / xml, I would like to somehow convert them into an object for convenient work.

What design pattern is used for this purpose?

  • I do not know the pattern or who. Serialization / deserialization, marshaling / unmarshaling. Just export / import without a hitch :) - Sergey
  • one
    Are you going to design something for the sake of 1-2 lines of code? The people are completely crazy with this OOP - DaemonHK

1 answer 1

Use an abstract factory

The receipt code - you will always be the same, but if the data changes the format, or you want to get additional data from other sources that may have a different format - then updating the script will not be difficult by adding 2 classes of new format to this template.

PySy, wrote about 1-2 lines of code in the comments, especially when the templates can be copied and pasted in the same way and the time for their implementation will not be much, if you understand what is at stake, this can be useful.

For example, I literally last week decided to import from 1c through an abstract factory - the data came to CSV, respectively, I simply had CSVReader and CSVItem in this template, then the client asked to completely copy the goods database from the combat site, on the one hand I could do export from the combat to CSV and also accept data as from 1c, but I didn’t want to copy files every time, so I just wrote a simple Rest API that outputs the data in JSON format, in the end the choice of the initial template helped me, since I used same creak Just added 2 new classes JSONReader and JSONItem