Huge number of get and set method

Hi All,

Application which i am developing has number of get and set method in bean class, so just would like to know in the class where i am fetching data (basically extracted from an xml which is present in DB as blob) should i call these get and set metod depending on the corresponding fields explicitly or is there any way that java detects on the fieldname and does a get/set automatically.

Thanks in advance

[432 byte] By [VenkyJavaa] at [2007-10-2 21:14:10]
# 1
If you want it done, you will need to provide for it.I had an object once that had a large number of sets and gets too, i just wrappered them in an object and did one set or one get that used or populated the data object. It was a lot less confusing to support.
morgalra at 2007-7-14 0:00:42 > top of Java-index,Desktop,Developing for the Desktop...
# 2

Did you try "XPath"?

Try searching articles about "XPath" ...

However, if you have to make your class in a JavaBeans fashion, I think you cannot reduce the getters and setters to one method. It is because JavaBeans currently does not support

"<ValueType> getXXX(String)" and "setXXX(String, <ValueType>)"

where the String argument is used to pass the name of the XML tag you want. The current JavaBeans spec only supports:

"<ValueType> getXXX(int)" and "setXXX(int, <ValueType>)"

which is called "Indexed Properties". One way you can do this is to "digitalize" the XML tags. For example, if you have 3 XML tags <first_name>, <last_name> and <address>, you can create 3 static constants like this:

public static final int TAG_FIRST_NAME = 0;

public static final int TAG_LAST_NAME = 1;

public static final int TAG_ADDRESS = 2;

Besides, create a map to map these numbers to the XML tag names:

public static final Map<Integer, String> MAP_XML_TAGS = createXMLTagsMap();

private static Map<Integer, String> createXMLTagsMap() {

Map<Integer, String> map = new HashMap<Integer, String>();

map.put(0, "first_name");

map.put(1, "last_name");

map.put(2, "address");

return map;

}

Then, you may call the "reduced getter" by getXMLTagValue(TAG_FIRST_NAME). The method could be inplemented like this:

public String getXMLTagValue(int index) {

String XMLTagName = MAP_XML_TAGS.get(index);

String value = null;

// Use XPath to get the value of the tag

return value;

}

However, personally, I don't think this is a good design pattern ....

I think this destroys the original philosophy behind JavaBeans.

Asuka Kenji (UserID = 289)

Duke Dollar Hunting now ...

kennethza at 2007-7-14 0:00:42 > top of Java-index,Desktop,Developing for the Desktop...