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

问题描述

我有一个客户端库,其中我对我的休息服务进行http远程调用,然后我返回 List< DataResponse> 回呼给我们的客户库与响应我从我的REST服务得到,以及任何错误,如果有任何包裹 DataResponse 对象。

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 class我有很多字段所以基础上,我有一个非常长的构造函数,每当我做一个 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 对象,然后从我的库中返回列表< DataResponse>

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

推荐答案

不要立即使用。 不适用于具有必需字段的类型。

建构者所需的属性是通过构造函数指定的。您不必强制使用方法定义值,这使得这些值是可选的。 这会让你的对象只有部分构造的潜力

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.

有了这个说法,你应该您的类型。我不确定 lmd ctime 是,或真的是一个 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.

isLink maskInfo idType 可以分解为 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

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

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

感觉你的构造函数需要太多静态吗?分解更多!

Feel your constructor requires too much still? Decompose more!

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

09-16 00:30