Split or not split classification (in Java)
I have a sentence that is analyzed at different stages First, I get some attributes (such as 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, such as "XYZ", and then I create the following classes:
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 is as follows:
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 can have only one class to hold all the information and fill in the attributes when extracting, which may lead to some empty results It's like this:
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,Z } public void setXYZ(...) {...} // getters,setters }
My question is: which design is more popular and why? If there's a better way to do what I'm trying to do here, I'd like to hear it
Solution
You need to consider whether analyzedsensence and finalsensence are unique enough to be split or merged in your problem domain
Obviously, they are processing similar data and working closely to achieve their goals
For me, analysis and ultimately just say that sentences can enter, although this is based on my limited knowledge of the problem you are studying, so I hope to combine them in some way
Based on more information, I think I will design as follows:
The sense class encapsulates the original sentence, tag and extracted category (or any content you are extracting, I assume it is a category based on your description), as well as the operations of setting, obtaining and extracting information
The sense class stores taglist, which contains all tags, original strings, and extracted categories It also encapsulates data extraction by creating a Extractor, and passes it to TagList when data is needed to extract it (I put it in the constructor, but it can be put in a method, and calling it depends on what data you need to extract).
Therefore, in this way, everything needed to manipulate the original sentence is in the sense class Of course, you may know something I don't do, which makes this method inappropriate, but here are some codes to illustrate what I mean:
public class Sentence { private TagList tags private String category; private String sentence public Sentence(String newSentence) { sentence = newSentence; Extractor<TagList> e = new Extractor<TagList>() tags = e.extractTags(sentence); category = new Category(tags); } public String getXYZ() { } public void setXYZ(...) {...} private extractTags(String s){ ...} // getters,setters } public class TagList{ private List<String> tags; .... //rest of class deFinition }