我是一名新手程序员,正在学习如何为我的模块设计 API。我想到了 2 种类型的服务类来提供 API 供其他类使用。
第一个例子通过参数处理逻辑,第二个例子通过对象方式处理。
对于我来说,为其他类提供业务方法以供使用,哪种方法/设计更好?
作为一般经验法则,我应该使用哪个?
示例 1 - 服务 A
public class ServiceA {
private SampleDAO dao = new SampleDAO();
private static final String DRAFT_STATUS = "DRAFT";
private static final String APPROVED_STATUS = "APPROVED";
private static final String SUBMITTED_STATUS = "SUBMITTED";
public boolean isDocumentApprove(String documentId) {
Document doc = getDocument(documentId);
return (APPROVED_STATUS.equals(doc.getStatus()));
}
public boolean isDocumentDraft(String documentId) {
Document doc = getDocument(documentId);
return (DRAFT_STATUS.equals(doc.getStatus()));
}
public boolean isDocumentSubmited(String documentId) {
Document doc = getDocument(documentId);
return (SUBMITTED_STATUS.equals(doc.getStatus()));
}
private Document getDocument(String documentId) {
return (dao.getByDocumentId(documentId));
}
}
示例 2 - 服务 B
public class ServiceB {
private SampleDAO dao = new SampleDAO();
private static final String DRAFT_STATUS = "DRAFT";
private static final String APPROVED_STATUS = "APPROVED";
private static final String SUBMITTED_STATUS = "SUBMITTED";
public Document getDocument(String documentId) {
return (dao.getByDocumentId(documentId));
}
public boolean isDocumentApprove(Document doc) {
return (APPROVED_STATUS.equals(doc.getStatus()));
}
public boolean isDocumentDraft(Document doc) {
return (DRAFT_STATUS.equals(doc.getStatus()));
}
public boolean isDocumentSubmited(Document doc) {
return (SUBMITTED_STATUS.equals(doc.getStatus()));
}
}