简体   繁体   English

服务层的API设计

[英]API Design for Service Layer

I am a novice programmer learning how to design API for my module. 我是一个新手程序员,正在学习如何为我的模块设计API。 I thought of 2 types of service class to provide a API for other classes to use. 我想到了两种类型的服务类来提供供其他类使用的API。

First example handles the logic by parameters, the second example handles by object way. 第一个示例通过参数处理逻辑,第二个示例通过对象方式处理。

Which is a better approach / design for me to provide business methods for other classes to use? 为我提供其他班级使用的业务方法,哪种方法/设计更好?

As a general rule of thumb which should I use? 作为一般经验法则,我应该使用哪个?

Example 1 - Service A 示例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));
    }
}

Example 2 - Service B 示例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()));
    }

}

If I had to choose, i would pick the the second example, because it contains less code duplication. 如果必须选择,我将选择第二个示例,因为它包含较少的代码重复。 In the first one, there is the same Document doc = getDocument(documentId); 在第一个中,有相同的Document doc = getDocument(documentId); statement in every method. 每种方法中的语句。 So, here the general principle is "don't repeat yourself". 因此,这里的一般原则是“不要重复自己”。

Furthermore, in the first version you can pass any garbage as a string to the methods. 此外,在第一个版本中,您可以将任何垃圾作为字符串传递给方法。 Although you can pass null in the second version too, but it is easier to check for null than for invalid ids. 尽管您也可以在第二个版本中传递null,但是检查null而不是检查无效的id更为容易。

Depends on the goal of your API. 取决于您的API的目标。

Does the caller always have a DocumentID and never a Document instance? 调用方是否总是有一个DocumentID不是一个Document实例?

  • ServiceA is the way to go (abstract the Document instance and only return a status). ServiceA是一种处理方式(抽象Document实例,仅返回状态)。

Does the caller sometimes have a DocumentID and sometimes a Document instance? 调用方有时是否具有DocumentID有时甚至具有Document实例?

  • ServiceB is the way to go as you offer the caller the ability to either get a document or get the status. 当您为调用方提供获取文档或获取状态的功能时,ServiceB是一种解决方法。
  • In this case you would be better off modifying your Document class to include a getStatus() method. 在这种情况下,最好将Document类修改为包含getStatus()方法。 That way if the caller has a Document instance they can call document.getStatus() instead of calling your service. 这样,如果调用方具有Document实例,则他们可以调用document.getStatus()而不是调用您的服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM