Split or not split class (in Java)

I have a suggestion that is being 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”, and then 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 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 could have only one class containing all the information, filling in the attributes as they are retrieved, which may lead to some null results. It will be 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,Y,Z
    }

    public void setXYZ(...) {...}

    // getters, setters
}

My question is: which design is preferable and why? If there is also a better way to accomplish what I'm trying to do here, I would also like to hear that.

+3
6

, AnalyzedSentence FinalSentence .

, .

, , , , , , - .

Edit , , :

Sentence , ( , , , , ), , , .

Sentence TagList, , . , Extractor TagList, ( , , , ).

, , , Sentence. , -, , , - , , :

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

}
+2

, . . , , .

+3

, 8000 !
FinalSentence , , AnalyzedSentence, .

+1

, , , .

, / , : " ".

, , , , , , : " (, , ..) , .

http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod

+1

, XYZ .

X, Y, Z, , XYZ .

0

, ?

String analyze(String input) {...}

, ( ), , - API - .

0

Source: https://habr.com/ru/post/1712971/


All Articles