本文介绍了如何避免制作长构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个客户端库,我在其中对我的休息服务进行 http 远程调用,然后我将 List 返回给正在调用我们的库的客户如果 DataResponse 对象周围有任何包裹,则从我的 REST 服务以及任何错误中获取.

I have a client library in which I am making http remote calls to my rest service and then I am returning List<DataResponse> back to the customer who is calling our library with the response I am getting from my REST service along with any errors if there are any wrapped around DataResponse object.

public class DataResponse {

    private final String response;
    private final boolean isLink;
    private final TypeOfId idType;
    private final long ctime;
    private final long lmd;
    private final String maskInfo;

    // below are for error stuff
    private final ErrorCode error;
    private final StatusCode status;

    // constructors and getters here

}

这是我的 ErrorCode 枚举类:

public enum ErrorCode {

    // enum values

    private final int code;
    private final String status;
    private final String description;

    // constructors and getters

}

这是我的 StatusCode 枚举类:

public enum StatusCode {
    SUCCESS, FAILURE;
}

正如你在我的 DataResponse 类中看到的,我有很多字段,因此我有一个很长的构造函数,每次当我创建一个 DataResponse 对象时,我都有new DataResponse(.......) 一行.将来我可能会有更多的领域,但现在我只有这些领域.

As you can see in my DataResponse class I have lot of fields so basis on that I have a very long constructor and everytime when I make a DataResponse object I have a big line with new DataResponse(.......). In future I might have more fields but for now I only have these fields.

有什么更好的方法可以用来制作 DataResponse 对象,然后从我的库中返回 List 吗?

Is there any better way I can use to make a DataResponse object and then return back List<DataResponse> from my library?

推荐答案

请勿使用 builder 模式.它不适用于具有大量必填字段的类型.它适用于具有大量可选字段的类型.

Do not use the builder pattern right away. It is not for types with tons of required fields. It's for types with tons of optional fields.

构建器所需的属性是通过构造函数指定的.您不必使用方法来定义值,这使得这些值是可选的.

Builders' required properties are specified via the constructor. You are not forced to define values using methods, which makes those values optional.

这使得您的对象可能仅被部分构建.为此使用构建器将是对设计的滥用.

This leaves potential for your object being only partially constructed. Using a builder for this would be abuse of the design.

话虽如此,您应该分解您的类型.我不确定 lmdctime 是什么,或者 DataResponse 应该代表什么,所以我不能告诉你以哪种方式你应该分解.但我可以告诉你,凝聚力决定了这一点.

With that said, you should decompose your type. I'm not sure what lmd or ctime is, or really what a DataResponse is supposed to represent, so I cannot tell you in which way you should decompose. But I can tell you cohesion is what determines such.

isLinkmaskInfoidType 可能被分解成一个 DataResponseDetails 对象:

isLink, maskInfo and idType could possibly be decomposed into a DataResponseDetails object:

class DataResponseDetails {
    private boolean isLink;
    private String maskInfo;
    private TypeOfId idType;

    public DataResponseDetails(boolean isLink, String maskInfo, TypeOfId idType) {
        //...
    }
}

现在你的 DataResponse 可以由 DataResponseDetails 组成:

Now your DataResponse could be composed of DataResponseDetails:

class DataResponse {
    private DataResponseDetails details;
    private String response;
    //...

    public DataResponse(DataResponseDetails details, String response, ...) {
        //...
    }
}

还是觉得你的构造函数要求太多?分解更多!

Feel your constructor requires too much still? Decompose more!

这篇关于如何避免制作长构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 00:30