I have a sentence which is analyzed in different phases. First, I get some attributes (say, X, Y, Z):
public class AnalyzedSentence {
private String X;
private String Y;
private String Z;
public AnalyzedSentence(String sentence) {
extractX();
extractY();
extractZ();
}
// getters, setters
}
Then, I use these attributes to further analyze the sentence to get another attribute, say, "XYZ", after which I create the following class:
public class FinalSentence {
private AnalyzedSentence data;
private String XYZ;
public FinalSentence(String XYZ, AnalyzedSentence data) {
this.data = data;
this.XYZ = XYZ;
}
// getters, setters
}
The workflow goes like this:
public class SentenceAnalyzer {
/// ...
public FinalSentence analyze(String sentence) {
AnalyzedSentence as = new AnalyzedSentence(sentence); // every attribute of "as" can be calculated beforehand
String XYZ = SpecialClass.extractXYZ(sentence, as); // extract XYZ (needs a special class), based on as
return new FinalSentence(XYZ, as);
}
}
Alternatively, I could have just a single class holding all the information, filling the attributes as they were extracted, which could result in some null results. It'd be like so:
public class Sentence {
private String X;
private String Y;
private String Z;
private String XYZ;
public Sentence(String sentence) {
extractX();
extractY();
extractZ();
}
public String getXYZ() {
// with this design, this method can be called, even if XYZ was not extracted yet.
// remember that XYZ cannot be extracted as X,Y,Z
}
public void setXYZ(...) {...}
// getters, setters
}
My question is: which design is preferred, and why ? If there's also a better way to accomplish what I'm trying to do here, I'd also like to hear it.