【问题标题】:How to pass the various input parameters to a method in a class efficiently?如何有效地将各种输入参数传递给类中的方法?
【发布时间】:2014-01-10 06:48:06
【问题描述】:

下面是我的界面 -

public interface IClient {
    public String executeSync(ClientInput input);
}

这是我的接口实现 -

public class TestingClient implements IClient {

    @Override
    public String executeSync(ClientInput input) {

    }
}

现在我有一个工厂,它可以像这样获取TestingClient 的实例 -

IClient client = TestingClientFactory.getInstance();

现在客户将调用我的TestingClientexecuteSync 方法,该方法接受ClientInput 参数,下面是ClientInput 的类。

public final class ClientInput {

    private Long userid;
    private Long clientid;
    private Long timeout = 20L;
    private boolean debug;
    private Map<String, String> parameterMap;

    public ClientInput(Long userid, Long clientid, Map<String, String> parameterMap, Long timeout, boolean debug) {
        this.userid = userid;
        this.clientid = clientid;
        this.parameterMap = parameterMap;
        this.timeout = timeout;
        this.debug = debug;
    }

 ... //getters here
}    

所以当客户调用我们TestingClientexecuteSync方法时,他们会像这样创建ClientInput参数,然后使用工厂获取TestingClient的Instance,然后相应地调用executeSync方法.

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

ClientInput input = new ClientInput(109739281L, 20L, paramMap, 1000L, true);

IClient client = TestingClientFactory.getInstance();
client.executeSync(input);

问题陈述:-

  1. 如上所示,这是制作ClientInput参数并传递给executeSync方法的正确方法吗?
  2. 我的 ClientInput 中已经有三个 Long 类型的参数,其他开发人员可能不清楚哪个位置对应哪个字段(尤其是在那些漫长的夜晚......)。任何想法如何避免这种情况?
  3. 如果需要更多的输入,它会使构造函数声明更长。我该如何克服这种情况?

【问题讨论】:

  • 你为什么不使用setter??将来你必须让你的构造函数总是改变

标签: java constructor input-parameters


【解决方案1】:

如果您有更多参数,请使用构建器模式。这使代码干净。 (参见this 示例)

例如,你可以有

clientinput.userid("userid)
           .clientid("clientid")

如果没有指定,一些参数可以是可选的。就像没有设置 timeout 和 debug 一样,它们可以采用默认值

【讨论】:

  • @Shiva:谢谢..您能否根据我的场景提供一个基于此的示例以及如何使用它?对于我的用例,这看起来更有希望..
  • 检查我给出的链接。如果你用谷歌搜索“Builder Pattern”,你会在网上找到很多例子。祝你好运
【解决方案2】:

对于这种情况,最好使用 Setters 和 Getters 所有这些字段而不是 Constructor

private Long userid;
private Long clientid;
private Long timeout = 20L;
private boolean debug;
private Map<String, String> parameterMap;

【讨论】:

  • 这样只能让构造函数变苗条
  • 我认为使用构造函数而不是 setter 更好,因为使用 setter 你可以有未初始化的字段。所以你应该检查它是否为 NULL。
【解决方案3】:

我建议你使用 setter 和 getter 因为将来您的 Object 字段(状态)可能会增加,这将始终要求您在构造函数中进行更改。

【讨论】:

    【解决方案4】:

    1 听起来不错,就像你做的那样。方法将包含所有信息的包装对象作为参数 -> 良好的设计,好像对象中的信息发生了变化,方法不需要改变。显然比采用 5 个不同参数的方法要好。

    2 我认为调用该方法的通常方式如下:

    Map<String, String> paramMap = new HashMap<String, String>();
    paramMap.put("attribute", "segmentation");
    
    ClientInput input = new ClientInput(user.getUserId(), client.getClientId(), 20L, paramMap, 1000L, true);
    
    IClient client = TestingClientFactory.getInstance();
    client.executeSync(input);
    

    所以要足够清楚,可以避免错误

    3 .如果可能出现更多参数,请尝试将它们分组到其他一些包装类中,然后这些包装类将用于创建您的调用对象(组合)。

    【讨论】:

      猜你喜欢
      • 2020-12-07
      • 2017-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-07
      • 2012-08-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多