Integrating XML read/write design question

I have two designs for reading/writing my classes to XML. Which do you think is better:

This is my structure (Attributes are single class members, Elements are std::vector< T > members
1
2
3
4
5
6
7
8
9
10
Structure: 
+ Calibrator
  + Attrib: readLabel (string)
  + Attrib: saveLabel (string)
  + Element: Device
    + Attrib: sourceLabel (string)
    + Attrib: destLabel (string)
    + Element: CalPoint
      + Attrib: sourceVal (double)
      + Attrib: destVal (double)



1. Design 1: Have an independent class to handle everything and "friend" it with the users.
Pro: Only 1 interface needed and we can switch interface easily.
Con: There is a lot of inter-class data which destroys encapsulation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class CalibratorXML;

class Device{
  string sourceLabel;
  string destLabel;
  vector<CalPoint> calPoints;

  friend class CalibratorXML;
};

class Calibrator {
  string readLabel;
  string saveLabel;
  std::vector<Device> devices;

  friend class CalibratorXML;
};

class CalibratorXML {
public: 
  static void Read( Calibrator* cal );
  static void Write( Calibrator* cal );
private:
  static void ReadCalibrator( Calibrator* cal, TiXmlElement* calibrator );
  static void ReadDevice( Calibrator* cal, TiXmlElement* device );
  static void ReadCalPoint( Device* dev, TiXmlElement* calPoint );
  // ... same for write
};


2. Encapsulate XML into the main classes. I think it's more organized, but makes it tougher to swap out for another interface and the class becomes less modular/generic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Device{
  string sourceLabel;
  string destLabel;
  vector<CalPoint> calPoints;

#ifdef TINY_XML
  TiXMLElement* ToXMLElement();
  void FromXMLElement(TiXMLElement* elem);
#endif
};

class Calibrator {
  string readLabel;
  string saveLabel;
  std::vector<Device> devices;

#ifdef TINY_XML
  TiXmlElement* ToXMLElement();
  void FromXMLElement(TiXmlElement* elem);
  void WriteXML();
  void ReadXML();
#endif
};
Last edited on
Topic archived. No new replies allowed.