I have long experience in procedural world and been programming/maintaining OO code in various languages too. Now getting into OO design and feeling the pains of identifying objects from scratch. Been reading several books, articles, etc. on OO. Often times they stop with Person, Employee examples. Identifying and doing it the right way in a real world projects raises several questions.
This is sort of a follow up question from my earlier design review question. Say, I have an object like below:
public class AddressRecord {
protected long RecordID;
protected String AddressLine1;
protected String AddressLine2;
protected String City;
protected String State;
protected String PostalCode;
protected String Country;
protected String FirstName;
protected String LastName;
protected String FullName;
protected String PhoneNumber;
protected String EmailAddress;
protected String CompanyName;
..
/* related to my question below */
public String extractXml() {}
public HashMap extractHash() {}
public String extractJson() {}
...
}
Now, I want to extract the field/values into a hashtable or a XML (proprietary format) or JSON structure without resorting to any libraries like JAXB. I will write some methods (for e.g., extractXml, extrachHash, extractJson) to extract specific fields into the target format.
My question is where should such extraction/conversion methods be? Based on some exposure to OO design books and advice, I was assuming I could have these methods right inside this class.
@Vladislav mentioned that they should actually be outside, like in the controller. I kind of get it, but I am curious to know why? Also, if I want to isolate this extraction logic from the controller code, can I create a class doing just that? Like,
public class AddressRecordXml {
public static String extractXml(AddressRecord rec)
{
}
...
}
and then use it in the controller as,
...
AddressRecord rec = new AddressRecord();
String xmlStr = AddressRecordXml(rec);
...
String resp = transport.process(xmlStr);
...
Note: I know there are libraries like JAXB etc to help serialize objects to XML etc. I cannot use those, yet. I am working with Java 1.4 inside an old Sybase EAServer (main programming language being PowerBuilder) and it puts a lots of restrictions. Same reason for HashMap, no Generics etc.
toXxx
.