如何在ModelInput类中使用Joshua Bloch版本描述的Builder模式? [英] How to use Builder pattern as described by Joshua Bloch's version in my ModelInput class?

查看:94
本文介绍了如何在ModelInput类中使用Joshua Bloch版本描述的Builder模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在下面的类中使用Builder模式。最初,我使用类的构造函数来设置所有参数,但是偶然地我碰到了Builder模式,这对我的用例来说很好。

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 and it is looking good for my use case.

下面是我的课程,在该课程中,人们通常会通过 userId clientId parameterMap 总是,但是其他字段是可选的,它们可能会也可能不会通过。而且,如果他们没有传递任何超时值,我需要将默认超时值始终设置为500,但是如果他们传递任何超时值,则它应该覆盖我的默认超时值。这里的Preference是一个具有四个字段的ENUM。

Below is my class in which people will mostly pass userId, clientId and parameterMap always but other fields are optional and they may or may not pass it. And also if they are not passing any timeout value, I need to have default timeout value set as 500 always but if they are passing any timeout value then it should override my default timeout value. Here Preference is an ENUM having four fields.

public final class ModelInput {

    private long userid;
    private long clientid;
    private long timeout = 500L;
    private Preference pref;
    private boolean debug;
    private Map<String, String> parameterMap;

    public ModelInput(long userid, long clientid, Preference pref, Map<String, String> parameterMap, long timeout, boolean debug) {
        this.userid = userid;
        this.clientid = clientid;
        this.pref = pref;
        this.parameterMap = parameterMap;
        this.timeout = timeout;
        this.debug = debug;
    }

 ... //getters here
}    

下面是一个示例,我最初是如何通过将参数传递给构造函数来构造 ModelInput 对象的。最初,我传递了所有参数,但客户端通常会传递 userId clientId parameterMap <

Below is an example how I was using initially to construct ModelInput object by passing parameters to constructor. Initially I was passing all the parameters but clients will mostly pass userId, clientId and parameterMap always and other fields are optional..

Map<String, String> paramMap = new HashMap<String, String>();
paramMap.put("attribute", "segmentation");

ModelInput input = new ModelInput(109739281L, 20L, Preference.SECONDARY, paramMap, 1000L, true);

我如何将上述代码转换为开始使用Builder模式,如Bloch在Effective Java中所说的那样。

How do I convert my above code to start using Builder pattern as told by Bloch's in Effective Java which is also thread safe and immutable as well?

又如何使用Builder模式对此进行验证检查?

And how can I do the validation check as well on this using Builder pattern? It might be possible people can pass userId zero or negative numbers same with client id and timeout and same with map as well..

推荐答案

Builder构造函数必须具有必需的参数。因此,在您的情况下,如果userId,clientId和parameterMap是必需的,我们将具有以下内容:

Builder constructor must have the mandatory parameters. So, in your case, if userId, clientId and parameterMap are mandatory, we will have something like that:

public final class ModelInput {

    private long userid;
    private long clientid;
    private long timeout = 500L;
    private Preference pref;
    private boolean debug;
    private Map<String, String> parameterMap;

    public ModelInput(Builder builder) {
        this.userid = builder.userId;
        this.clientid = builder.clientId;
        this.pref = builder.preference;
        this.parameterMap = builder.parameterMap;
        this.timeout = builder.timeout;
        this.debug = builder.debug;
    }

    public static class Builder {
        private long userId;
        private long clientId;
        private Preference preference;
        private boolean debug;
        private Map<String, String> parameterMap;

        public Builder(long userId, long clientId, Map<String, String> parameterMap) {
            this.userId = userId;
            this.clientId = clientId;
            this.parameterMap = parameterMap;
        }

        public Builder preference(Preference preference) {
            this.preference = preference;
            return this;
        }

        public Builder debug(boolean debug) {
            this.debug = debug;
            return this;
        }

        public Builder timeout(long timeout) {
            this.timeout = timeout;
            return this;
        }

        ...

        public ModelInput build() {
            return ModelInput(this);
        }
    }

    // ModelInput getters / setters
}

这是如何使用您的构建器类:

And this is how to use your builder class:

String paramMap = new HashMap<String, String>();
paramMap.put("attribute", "segmentation");

ModelInput.Builder builder = new ModelInput.Builder(109739281L, 20L, paramMap);
builder.preference(Preference.SECONDARY).timeout(1000L).debug(true);

ModelInput modelInput = builder.build();

希望这会有所帮助:)

这篇关于如何在ModelInput类中使用Joshua Bloch版本描述的Builder模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆