Note that there are some explanatory texts on larger screens.

plurals
  1. POTo split or not to split a class (in Java)
    primarykey
    data
    text
    <p>I have a sentence which is analyzed in different phases. First, I get some attributes (say, X, Y, Z):</p> <pre><code>public class AnalyzedSentence { private String X; private String Y; private String Z; public AnalyzedSentence(String sentence) { extractX(); extractY(); extractZ(); } // getters, setters } </code></pre> <p>Then, I use these attributes to further analyze the sentence to get another attribute, say, "XYZ", after which I create the following class:</p> <pre><code>public class FinalSentence { private AnalyzedSentence data; private String XYZ; public FinalSentence(String XYZ, AnalyzedSentence data) { this.data = data; this.XYZ = XYZ; } // getters, setters } </code></pre> <p>The workflow goes like this:</p> <pre><code>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); } } </code></pre> <p>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:</p> <pre><code>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 } </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COcould you give a concrete example of what these classes do? E.g. what values would the be given and what would they return?
      singulars
    2. COYes, I have a pipeline system, in which the first module of the pipeline is the SentenceAnalyzer, whose goal is to return an object that can be passed to the other components of the pipeline. This object is the FinalSentence/Sentence. In the first case, the attributes X, Y, and Z, represent data that can be independently extracted from the sentence, such as POS tags (nouns, verbs, articles), person names, etc. The XYZ attribute however, cannot be independently extracted, and requires a special class to do it. It represents the "category" of the sentence, such as "sports".
      singulars
    3. COMoreover, the class that is used to extract XYZ, requires me to pass a single object containing everything that I need for the extraction, hence the need to pass the "AnalyzedSentence" to it. This interface for the class is: public class Extractor<E> { String extract(E); } So, I declare an Extractor<AnalyzedSentence>, so that I can obtain the analyzed sentence data in the extract method. This is one the reasons why I think the classes should be split, because the extractor shouldn't receive a class that has an extra field (XYZ), which corresponds to what it is extracting.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload