java 处理超过 7 个参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4272756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:38:33  来源:igfitidea点击:

Handling more than 7 Parameters

java

提问by sarahTheButterFly

I have a public class, which needs 7 parameters to be passed down. At the moment, I am able to make 3 of them being passed to constructor and another 4 to a public method in the class . Like this:

我有一个公共类,需要传递 7 个参数。目前,我可以将其中 3 个传递给构造函数,将另外 4 个传递给类中的公共方法。像这样:


Public Class AClass{
    private XClass axClass;
    private String par4;
    private String par5; 
    private String par6;
    private String par7;

    public AClass(String par1, String par2, String par3){
       aXClass = new XClass(par1,par2,par3);
    }

    public execute(String par4,String par5, String par6, String par7){
       //this is needed because they are used in other private methods in this class
       this.par4 = par4;
       this.par5 = par5;
       this.par6 = par6;
       this.par7 = par7;

       //call other private methods within this class. 
       //about 7 lines here
    }

}


My question is, is this the right way to ask client of the class to passing in paramters?

我的问题是,这是要求班级客户传递参数的正确方法吗?

回答by Sean Patrick Floyd

I'd go for the Builder Patterninstead of many constructor parameters as suggested by

我会使用Builder Pattern而不是建议的许多构造函数参数

Effective JavaItem 2: Consider a builder when faced with many constructor parameters

Effective JavaItem 2:当面对许多构造函数参数时考虑构建器

Here's a simple class to illustrate:

这是一个简单的类来说明:

public class Dummy {

    private final String    foo;
    private final String    bar;
    private final boolean   baz;
    private final int       phleem;

    protected Dummy(final Builder builder) {
        this.foo = builder.foo;
        this.bar = builder.bar;
        this.baz = builder.baz;
        this.phleem = builder.phleem;
    }

    public String getBar() {
        return this.bar;
    }

    public String getFoo() {
        return this.foo;
    }

    public int getPhleem() {
        return this.phleem;
    }

    public boolean isBaz() {
        return this.baz;
    }

    public static class Builder {
        private String  foo;
        private String  bar;
        private boolean baz;
        private int     phleem;

        public Dummy build() {
            return new Dummy(this);
        }

        public Builder withBar(final String bar) {
            this.bar = bar;
            return this;
        }

        public Builder withBaz(final boolean baz) {
            this.baz = baz;
            return this;
        }

        public Builder withFoo(final String foo) {
            this.foo = foo;
            return this;
        }

        public Builder withPhleem(final int phleem) {
            this.phleem = phleem;
            return this;
        }

    }

}

You would instantiate it like this:

你会像这样实例化它:

Dummy dummy = new Dummy.Builder()
                    .withFoo("abc")
                    .withBar("def")
                    .withBaz(true)
                    .withPhleem(123)
                    .build();

The nice part: you get all the benefits of constructor parameters (e.g. immutability if you want it), but you get readable code too.

好的部分:您获得了构造函数参数的所有好处(例如,如果您想要它,则是不变性),但您也获得了可读的代码。

回答by Marvo

There shouldn't be anything stopping you from passing 7 parameters to a constructor, if that's what you want. I don't know if there's a maximum number of parameters that can be passed to a method in Java, but it's certainly higher than 7 if there is a max.

如果这是您想要的,应该没有任何东西阻止您将 7 个参数传递给构造函数。我不知道Java中可以传递给方法的参数的最大数量是否存在,但是如果有最大值,它肯定会高于7。

When you create a class and its public methods, you're creating an interface on how to use and access that class. So technically what you've done so far is correct. Is it the "right way" to ask the client of a class to pass in arguments? That's up to you, the designer of the interface.

当您创建一个类及其公共方法时,您正在创建一个关于如何使用和访问该类的接口。所以从技术上讲,到目前为止你所做的都是正确的。要求类的客户传递参数是“正确的方法”吗?这取决于你,界面的设计者。

My first instinct when I saw 7 parameters being passed was to silently ask "Is there some relationship between some or all of these parameters that might mean they'd go together well in a class of their own?" That might be something you address as you look at your code. But that's a question of design, not one of correctness.

当我看到 7 个参数被传递时,我的第一反应是默默地问“这些参数的部分或全部之间是否存在某种关系,这可能意味着它们在自己的类中可以很好地结合在一起?” 这可能是您在查看代码时解决的问题。但这是一个设计问题,而不是正确性问题。

回答by Mark Baijens

Can't you just make a class/hashmap that stores these parameters and pass this to the function?

你不能制作一个存储这些参数的类/哈希图并将其传递给函数吗?

public excute(Storageclass storageClass){
       //this is needed because they are used in other private methods in this class
       this.par4 = storageClass.getPar4();
       this.par5 = storageClass.getPar5();
       this.par6 = storageClass.getPar6();
       this.par7 = storageClass.getPar7();

       //or
       this.storageClass = storageClass;
    }

回答by OscarRyz

I don't really see the problem with that.

我真的看不出这有什么问题。

In any case you could create a "Request" object or something like this:

在任何情况下,您都可以创建一个“请求”对象或类似的东西:

class SomeClass { 
     private String a;
     private String b;
     ....
     public SomeClass( Request r ) { 
        this.a = r.get("a");
        this.b = r.get("b");
        ...
     }

     public void execute( Request other ) { 
         this.d = other.get("d");
         this.e = other.get("d");
         ...
     }
}

See also: http://c2.com/cgi/wiki?TooManyParameters

另见:http: //c2.com/cgi/wiki?TooManyParameters

回答by KevinDTimm

Without knowing the use of the child class, I can say that there is nothing inherently wrong with what you have done.

在不知道子类的用途的情况下,我可以说你所做的没有任何本质上的错误。

Note though that you have to declare

请注意,您必须声明

private XClass axClass;

private XClass axClass;

in the variables of your AClass.

在你的 AClass 的变量中。

However, you say 'I am able to make....' Does this mean there is some problem with declaring this another way?

但是,您说“我能够制作....”这是否意味着以另一种方式声明有问题?

回答by duffymo

I don't care for it much, because an object should be 100% ready to be used after its constructor is called. It's not as written in your example.

我不太关心它,因为一个对象在它的构造函数被调用后应该是 100% 准备好使用的。它不像你的例子中那样写。

If the parameters passed into the execute method can simply be consumed, and that's the method of interest for clients, I see no reason for them to be data members in the class.

如果传递给 execute 方法的参数可以简单地使用,并且这是客户端感兴趣的方法,那么我认为它们没有理由成为类中的数据成员。

Without knowing more about your ultimate aims it's hard to tell. But I would re-think this implementation.

如果不了解更多有关您的最终目标的信息,就很难说清楚。但我会重新考虑这个实现。

回答by HostileFork says dont trust SE

If you're planning on introducing an AClass.someMethod()that needs to know par4-7without requiring you to have called AClass.excute(), then clearly you should be passing the parameters in the constructor.

如果您打算在不需要调用的情况下引入AClass.someMethod()需要知道的,那么显然您应该在构造函数中传递参数。par4-7AClass.excute()

On the other hand: if you can construct an instance of this object with only par1-3and do something meaningful with it besidescall excute()then it makes sense to allow the object to be constructed with fewer than the full seven parameters.

另一方面:如果你可以只用构造这个对象的一个​​实例par1-3并用它做一些除了调用之外有意义的事情,excute()那么允许用少于完整的七个参数构造对象是有意义的。

Yet my own aesthetic is to try and limit the number of "modes" that an object can be in which make certain methods work and others fail. So ideally, a fully-constructed object is ready to run any method the programmer might call. I'd worry about the design issue more than be too concerned about the sheer number of parameters to the constructor.

然而,我自己的审美是试图限制一个对象可以使用的“模式”的数量,这些“模式”可以使某些方法有效而其他方法失败。因此,理想情况下,一个完整构造的对象已准备好运行程序员可能调用的任何方法。我更担心设计问题,而不是过于担心构造函数的参数数量。

But as others have pointed out, sometimes there is a natural grouping of these parameters which can deserve objects of their own. For instance: in many APIs instead of passing (x, y, width, height) all over the place they use rectangle objects.

但正如其他人指出的那样,有时这些参数会自然分组,值得拥有自己的对象。例如:在许多 API 中,他们使用矩形对象而不是到处传递 (x, y, width, height)。

回答by Zhukikov

As others already wrote, it is technically correct to pass 7 parameters, although not very 'user-friendly', if you can say so.

正如其他人已经写的那样,传递 7 个参数在技术上是正确的,虽然不是很“用户友好”,如果你能这么说的话。

Since you didn't write much about this class, I can suggest one small thing: in constructor you're just creating XClassobject, so it would be sane to create this object before and pass it as a single parameter.

由于你没有写太多关于这个类的内容,我可以建议一件小事:在构造函数中你只是在创建XClass对象,所以之前创建这个对象并将其作为单个参数传递是明智的。

Something like this:

像这样的东西:

...
XClass aXClass = new XClass(par1, par2, par3);
AClass aClass = new AClass(aXClass);
...

And this is the constructor:

这是构造函数:

public AClass(XClass aXClass) {
       this.aXClass = aXClass;
}