如果未传递默认值,如何在构建器模式中使用默认值,并使线程安全? [英] How to use default value in the builder pattern if that value is not passed and also make thread safe?

查看:61
本文介绍了如果未传递默认值,如何在构建器模式中使用默认值,并使线程安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为班级使用Builder模式.

I am trying to use Builder Pattern for my class..

下面是我按照约书亚·布洛赫(Joshua Bloch)在有效Java(第二版)中显示的版本构建的Builder类.我们的客户通常总是总是通过 userId clientId ,但是其他字段是可选的,他们可能会也可能不会通过.这里的Preference是一个ENUM类,其中有四个字段.

Below is my Builder class which I have built by following Joshua Bloch version as he showed in Effective Java, 2nd Edition. Our customer will mostly pass userId, clientId always but other fields are optional and they may or may not pass it. Here Preference is an ENUM class having four fields in it.

public final class InputKeys {

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

    private InputKeys(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 {
        protected final long userId;
        protected final long clientId;
        protected long timeout;
        protected Preference preference;
        protected boolean debug;
        protected Map<String, String> parameterMap;

        public Builder(long userId, long clientId) {
            this.userId = userId;
            this.clientId = clientId;
        }

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

        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 InputKeys build() {
            return new InputKeys(this);
        }
    }
}

下面是我的ENUM课程-

Below is my ENUM class -

public enum Preference {
    PRIMARY,
    SECONDARY
}    

我正在这样调用以构造 InputKeys 参数-

I am making a call like this to construct the InputKeys parameter -

InputKeys keys = new InputKeys.Builder(12000L, 33L).build();    
System.out.println(keys);

我在这里看到的唯一问题是,如果客户未传递任何超时值,则我需要将默认超时值始终设置为500,但是如果他们传递任何超时值,则它应覆盖我的默认超时值.而且这对我不起作用,因为当我在调试模式下看到 keys 时,它的超时值始终为0.

The only problem here I am seeing is, if the customers 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. And this is not working for me as when I see my keys in the debug mode, it always has timeout value as 0.

我有什么想念的吗?并且我也在尝试使此类不可变且线程安全..这是Builder的线程安全且不变的版本,还是我错过了某些事情?

Is there anything I am missing? And also I am trying to make this class immutable and thread safe.. Is this a thread safe and immutable version of Builder or I have missed something?

更新:-

人们将要使用的特定场景是我们有一个工厂,客户将使用该工厂来调用我们的代码.我们有一个简单的接口,我们的一个类正在实现它,然后有一个工厂,通过它我们将调用该实现的某个方法,该方法接受此 keys 参数.

Specific scenarios when people will be using this is we have a factory which customers are going to use to make a call to our code. We have a simple interface which one of our class is implementing it and then we have a factory by which we will call a certain method of that implementation which accepts this keys parameter.

因此,他们将在该应用程序中使用以下代码进行调用,并且有可能在多线程环境中在该应用程序中运行.

So they will be using the below code in there application to make a call and it might be possible they will be running there application in a multithreaded environment.

InputKeys keys = new InputKeys.Builder(12000L, 33L).build();    

IClient client = ClientFactory.getInstance();
client.getData(keys);

然后可以多次使用InputKeys Builder来构造键,而不是一次.无论他们获得什么userId和clientId,都将再次构造InputKeys.

And then InputKeys Builder can be used multiple times to construct the keys, it's not one time. Whatever userId and clientId they will be getting they will construct the InputKeys again.

推荐答案

只需在 Builder 中将超时初始化为500.如果调用此方法, timeout 方法将覆盖此值.

Just initialize the timeout to 500 in the Builder. The timeout method will overwrite this value if it's called.

protected long timeout = 500L;

这篇关于如果未传递默认值,如何在构建器模式中使用默认值,并使线程安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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