开发者

Working with data .. Is separate bean needed to handle data retrieved from json file?

开发者 https://www.devze.com 2023-03-11 08:27 出处:网络
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

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 rea开发者_如何学Goder/ 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消