java 拆分类_拆分或不拆分类(用Java)

我有一个句子,在不同的阶段进行分析.首先,我得到一些属性(比如X,Y,Z):

public class AnalyzedSentence {

private String X;

private String Y;

private String Z;

public AnalyzedSentence(String sentence) {

extractX();

extractY();

extractZ();

}

// getters, setters

}

然后,我使用这些属性来进一步分析句子以获得另一个属性,比如“XYZ”,之后我创建了以下类:

public class FinalSentence {

private AnalyzedSentence data;

private String XYZ;

public FinalSentence(String XYZ, AnalyzedSentence data) {

this.data = data;

this.XYZ = XYZ;

}

// getters, setters

}

工作流程如下:

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);

}

}

或者,我可以只有一个类保存所有信息,在提取时填充属性,这可能会导致一些空结果.它是这样的:

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

}

我的问题是:哪种设计更受欢迎,为什么?如果还有更好的方法来完成我在这里尝试做的事情,我也想听听它.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值