简体   繁体   中英

Implement builder method with arguments

I have the following code:

ApiRequest request = ApiRequest.builder("test 55").clientName(SERVICE).build();

I implemented this code:

@Builder
@Getter
@Setter
public class ApiRequest {

    private String clientName;

    public static class builder{

        private String clientName;

        public builder(String bucketKey) {
            this.bucketKey = bucketKey;
        }

        public builder(String clientName, ......) {
            this.clientName= clientName;
            ....
        }

        public ApiRequest build(){
            return new ApiRequest (clientName, .....);
        }

    }
}

For build I get error Expected 0 arguments but found 1 Do you know what is the proper way to implement this code?

You can implement a builder in two different ways.

Plain java

In that case, you don't add any annotations and write all code with your hands:

public class ApiRequest {

    private String clientName;
    private String serviceName;

    public ApiRequest(final String clientName, final String serviceName) {
        this.clientName = clientName;
        this.serviceName = serviceName;
    }

    public static void main(String[] args) {
        String SERVICE = "Some service name";
        final String clientName = "test 55";
        ApiRequest request = ApiRequest.builder(bb).setServiceName(SERVICE).build();
    }

    private static Builder builder(final String clientName) {
        return new Builder(clientName);
    }

    private static class Builder {

        private final String clientName;
        private String serviceName;

        public Builder(final String clientName) {
            this.clientName = clientName;
        }

        public Builder setServiceName(final String serviceName) {
            this.serviceName = serviceName;
            return this;
        }

        public ApiRequest build() {
            return new ApiRequest(clientName, serviceName);
        }
    }
}

Moreover, it's better to set clientName using separate setter (not in the constructor).

Lombok

Since you are using Lombok, it becomes much easier, just use @Builder annotation without any other code:

@Builder
public class ApiRequest {

    private String clientName;
    private String serviceName;

    public ApiRequest(final String clientName, final String serviceName) {
        this.clientName = clientName;
        this.serviceName = serviceName;
    }

    public static void main(String[] args) {
        String SERVICE = "Some service name";
        final String clientName = "test 55";
        ApiRequest request = ApiRequest.builder().clientName(clientName).serviceName(SERVICE).build();
    }
}

Lombock provide you builder method without parameters. If you want additional methods you can add

public static Builder builder(String bucketKey) { return builder().setBucketKey(bucketKey); }

Also you don't need builder inner class. It isn't used anyway. Lombock generate Builder inner class for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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