I'm a fairly-new developer in the process of writing a Java application that needs to create objects dynamically. These objects are unique and need to be uniquely referenced, but are all part of the same generic data class with the same attributes. Each of these attributes is a String
, and they are:
- the
value
of the data - the
source
of the data - a reference
interfaceCode
that enables me to place the data in the correct location in an interfacing system - a reference
parent
that ties the data object to a parent object representing a data classification used in the interfacing system
And getters/setters for each. Values for each of those attributes is set using setters called in the public constructor with two arguments passed in. Here is a code sample:
class DataObject {
private String value, source, interfaceCode, parent;
public DataObject(String arg1, String arg2) {
setValue(arg1);
setSource(arg2);
setInterfaceCode(arg2);
setParent(arg2);
}
//setters/getters here
/
/
/
}
The issue I'm running into is determining how to create these objects uniquely and dynamically - I have input code that reads a data source document and pulls out data I want. If it finds data of a certain source type (which occur once per document), it needs to create a new Data Object
and assign the values to that. This reference is intended to be stored in a HashMap<String,DataObject
with the String
describing the data source set as a key and the DataObject
reference being the value assigned. This is all run in a different Class, as you can see below:
class DataParser {
private String value, source;
public DataObject DataParser() {
if (value != null && source != null) {
DataObject newData = new DataObject(value, source);
}
return newData;
}
//data setter/getter here to grab data from data source and provide access
/
/
/
}
So, I've read a whole bunch of StackOverflow posts/questions on this and I'm still coming up short. From many of the answers, I thought this problem might require reflection. However, I'm a little overwhelmed by the topic and not really sure if this is the correct route.
My concern is that, with my original code, that DataObject
reference from newData
will simply be overwritten each time I instantiate a new DataObject
. Ideally, I would have it instantiate a new Object with a variable name equivalent to the value of source
, but this is entirely unnecessary - what matters is the reference.
Can someone point me in the right direction regarding this problem?
Aucun commentaire:
Enregistrer un commentaire