How to use Builder pattern as described by Joshua Bloch's version in my ModelInput class?

后端 未结 4 1674
孤街浪徒
孤街浪徒 2021-01-23 10:32

I am trying to use Builder Pattern for my below class.. Initially I was using constructor of my class to set all the parameters but accidentally I came across Builder pattern an

4条回答
  •  北恋
    北恋 (楼主)
    2021-01-23 10:41

    try this

    public final class ModelInput {
        ...
    
        public static class Builder {
            private long userid;
            ...
    
            public Builder setUserId(long userId) {
                this.userId = userId;
            }
            ...
    
            public ModelInput build() {
                return new ModelInput(userId,...
            }
        }
    }  
    

提交回复
热议问题