构建器模式和大量强制参数 [英] The builder pattern and a large number of mandatory parameters

查看:131
本文介绍了构建器模式和大量强制参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我使用了以下实现构建器模式(与此处中所述的实现相反):

To date I use the following implementation of the builder pattern (as opposed to the implementation described here):

public class Widget {
    public static class Builder {
        public Builder(String name, double price) { ... }
        public Widget build() { ... }
        public Builder manufacturer(String value) { ... }
        public Builder serialNumber(String value) { ... }
        public Builder model(String value) { ... }
    }

    private Widget(Builder builder) { ... }
}

对于我遇到的大多数情况,我需要使用各种必需的/强制的和可选的参数构建一个复杂的对象,这样做很好。但是,最近我一直在努力了解所有参数是强制性的(或至少绝大多数)是如何得到的。

This works well for most situations I've encountered where I need to build up a complex object with a variety of required/mandatory and optional parameters. However, I've been struggling lately to understand how the pattern is of any benefit when all your parameters are mandatory (or at least the vast majority are).

解决这个问题是将传入的参数逻辑分组到自己的类中,以减少传递给构建器构造函数的参数数量。

One means of getting around this has been to logically group the parameters being passed in to their own classes to reduce the number of parameters being passed to the builder constructor.

例如,而不是:

Widget example = new Widget.Builder(req1, req2, req3,req4,req5,req6,req7,req8)
                           .addOptional(opt9)
                           .build();

分组如下:

Object1 group1 = new Object1(req1, req2, req3, req4);
Object2 group2 = new Object2(req5, req6);

Widget example2 = new Widget.Builder(group1, group2, req7, req8)
                            .addOptional(opt9)
                            .build();

虽然拥有单独的对象简化了很多事情,但是如果一个不熟悉代码。我考虑的一件事是将所有参数移动到自己的 addParam(param)方法中,然后对 build()方法。

While having separate objects simplifies things quite a bit, it also makes things a little difficult to follow if one is not familiar with the code. One thing I considered was moving all parameters into their own addParam(param) methods and then performing validation on required parameters in the build() method.

最好的做法是什么,还有一个更好的方法,我没有考虑过?

What is best practice and is there perhaps a better approach to this that I haven't considered?

推荐答案


但是,最近我一直在努力了解当所有参数是强制性(或至少)时,该模式有什么好处绝大多数是)。

However, I've been struggling lately to understand how the pattern is of any benefit when all your parameters are mandatory (or at least the vast majority are).

流利的构建器模式仍然是有益的:

The fluent builder pattern is still beneficial:

1)它更可读 - 它有效地允许命名参数,以便调用不仅仅是一个长命名的参数列表

1) Its more readable - it effectively allows named parameters so that the call isn't just a long list of unnamed arguments

2)它的无序 - 这让你将组参数合并成逻辑组,作为单个构建器调用程序调用的一部分,或者简单地通过让您使用自然顺序来调用构建器设置器方法最有意义的是这个特定的实例。

2) Its unordered - this lets you group arguments together into logical groups, either as part of a single builder setter call or simply by letting you use a natural order to calling the builder setter methods that make the most sense of this particular instantiation.

===


Widget example = new Widget.Builder(req1, req2, req3,req4,req5,req6,req7,req8)
                               .addOptional(opt9)
                               .build();

分组如下:

Object1 group1 = new Object1(req1, req2, req3, req4);
Object2 group2 = new Object2(req5, req6);

    Widget example2 = new Widget.Builder(group1, group2, req7, req8)
                            .addOptional(opt9)
                            .build();

虽然拥有单独的对象简化了很多事情,但是如果一个不熟悉代码。我考虑的一件事是将所有参数移动到自己的addParam(param)方法中,然后在build()方法中对所需参数执行验证。

While having separate objects simplifies things quite a bit, it also makes things a little difficult to follow if one is not familiar with the code. One thing I considered was moving all parameters into their own addParam(param) methods and then performing validation on required parameters in the build() method.



我喜欢混合物在适当或自然的情况下。每个参数都有自己的addParam方法,它不一定都是在构造函数中,而是。 Builder可以灵活地执行一个,另一个,中间或组合:

I would favor a hybrid when appropriate or natural. It doesn't have to be all in constructor or each param has its own addParam method. Builder gives you flexibility to do one, the other, in-between, or a combo:


Widget.Builder builder = new Widget.Builder(Widget.BUTTON);
builder.withWidgetBackingService(url, resource, id);
builder.withWidgetStyle(bgColor, lineWidth, fontStyle);
builder.withMouseover("Not required");
Widget example = builder.build();


这篇关于构建器模式和大量强制参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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