0

I'm stuck on a lightly philosophical question for me. I have three classes and I'm not sure how to make them related to each other. I have few competing approaches all of them standing on equal ground for me and I'm not sure which one has more cons and pros.

For example I'm designing system where I can command a device (and its subdevices) through Communicator:

  • Device (e.g. Computer)
  • Subdevice (e.g. Mouse, keyboard, screen)
  • Communicator (e.g. network)

Device creates and holds onto Subdevice objects. And Device creates Communicator. Now let's say there is JSON comming into Communicator and it needs to be passed on to each of Device's Subdevice (ex parseJSON() method)

Do I:

  1. Create Device::GetSubdevices() and for each Subdevice call parseJSON inside Communicator or
  2. Create Device::onParsedJson called by Communicator, where I iterate and route to Subdevice
  3. Do something else

First choice seems to bring Device's responsibility to Communicator which is not good. But second choice makes Device (+) handle routing responsibly (-) pollutes interface (even though it is interfaced)

edin-m
  • 133

1 Answers1

4

JSON is a data transfer syntax, and as such should be captured by the external-facing interface (Communicator, in this context) and immediately turned into some sort of classes/set of classes that the rest of the application can understand, rather than passing around "dirty" (and it's from an external source, it is dirty) data in a subpar format.

Thus, it seems to me that the Communicator should have a parseJson method which parses the data into a set of classes appropriate to represent it, and those classes are then passed to the rest of the system for however they will be ultimately consumed.

(this may not apply quite so much if you're using JSON as the core data format for the entire application and all internal classes are set up to deal with it, but frankly that's an odd choice... and even so, the data coming from an external place should always be scrubbed/validated into a known format/schema before being moved along to internal classes anyway, so the stance of having the Communicator (or an appropriate adapter within that context) handle it remains, IMO)

jleach
  • 2,682
  • Parsing of JSON needs to be done by each Subdevice (since it is specific to it - or at least part of json). Prior to parsing step Devices and Subdevices are already created - so they will only update theirs state. JSON is not used application wide but it is not relevant. I'm undecided on a matter of path of JSON to subdevices. – edin-m Mar 01 '16 at 03:17
  • @EdinM WRONG. You should not pass JSON into subdevices. PERIOD. Now, try to think of design that allows you to parse the JSON in Communication layer and then pass the properly typed data into the device and subdevices. – Euphoric Mar 01 '16 at 07:15
  • this could be realized with the use of some adapter that is given to your communicator class and managed through an interface. It separates the details of the parsing from the communication layer and as such the adapter can come from anywhere, and it means that the json data can be turned into a real object at the point of introduction, thus cleaning up the design of the subdevices (which really should never see json) – jleach Mar 01 '16 at 11:51