0

Suppose you are working with some data, which comes from a JSON file. You read the file, parse it, make sure all needed variables are there and provide accessor methods to retrieve what's needed. You reader/ parser .class name is A.

Once you're done with all the above, you need to do something with the data. To handle this, there is additional class, lets call it B.

At this point do you:

a) Have B retrieve all that it needs from A and do whatever it needs with it

b) Have A create a clean bean C, which will have nothing to do with JSON and will only contain getters and setters for data, B will need to work with it. Once C is created, it will be passed to B, which will handle it.

I can make arguments both for and against each (a) and (b). Please let me know what approach is preferred from the height of your experience.

Assume:

  • Performance is not a concern
  • Clarity and maintenability is top priority
  • Tomorrow, data may come from a different source. It will continue to remain in JSON format

1 Answer 1

1

It's more of a design decision.

(b) IMO. A is then a Factory pattern and can/should be somewhat agnostic of the data in JSON and you can use it on different files to produce beans with the right fields. You could even generate custom bean classes for different data sets.

(a) solution is only viable if your data is very small and rather static, as far as the fields are concerned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.