1、示例代码
import lombok.Data;
@Data
public class UrlConfig {
private String host;private Integer port;private String path;public static final class Builder {
private final UrlConfig urlConfig;private Builder() {
urlConfig = new UrlConfig();}public static Builder builder() {
return new Builder();}public Builder withHost(String host) {
urlConfig.setHost(host);return this;}public Builder withPort(Integer port) {
urlConfig.setPort(port);return this;}public Builder withPath(String path) {
urlConfig.setPath(path);return this;}public Builder but() {
return builder().withHost(urlConfig.getHost()).withPort(urlConfig.getPort()).withPath(urlConfig.getPath());}public UrlConfig build() {
return urlConfig;}}
}
2、使用方式
UrlConfig.Builder.builder().setHost(AGENT_HOST).build()