现在的位置: 首页 > 综合 > 正文

Consider Consolidating Method Parameters

2012年07月04日 ⁄ 综合 ⁄ 共 3100字 ⁄ 字号 评论关闭
Consider Consolidating Method Parameters

完善方法的参数

Sometimes it's a good idea to encapsulate multiple parameters to a method into a single object. This may enhance readability and simplify calling code. Consider a method signature like this:

有时候对一个方法封装多个参数是很好的主意。这可以提高可读性和简化调用流程。考虑一个像这样的方法声明:

public void setOptions(Font f, int lineSpacing, int linesPerPage,
int tabSize);

We could simplify this signature by rolling the multiple parameters into a single object, like this:

我们能够简化这个声明用包装这些参数到一个单个的对象,像这样:

public void setOptions(Options options);

The main advantage is flexibility. We don't need to break signatures to add further parameters: we can add additional properties to the parameter object. This means that we don't have to break code in existing callers that aren't interested in the added parameters.

这个主要的优点是可扩展性。我们不需要破坏声明增加更多的参数:我们对参数对象增加参数属性。这意味者我们不得不破坏代码对于调用者增加参数。

As Java, unlike C++, doesn't offer default parameter values, this can be a good way to enable clients to simplify calls. Let's suppose that all (or most) or the parameters have default values. In C++ we could code the default values in the method signature, enabling callers to omit some of them, like this:

正如java,不想c++,不提供默认参数值,这是一个好的方式简化客户端。让我们假设所有的(或者大多数)参数有默认值。在c++中我们能编码默认值在方法声明中,使得调用者去遗漏他们中的一些,像那样:

void SomeClass::setOptions(Font f, int lineSpacing = 1, int linesPerPage = 25,
int tabSize = 4);

This isn't possible in Java, but we can populate the object with default values, allowing subclasses to use syntax like this:

在java中这是不可能的,但是我们能用默认值构建这个对象,允许子类去用语法:

Options o = new Options();
o.setLineSpacing(2);
configurable.setOptions(o);

Here, the Options object's constructor sets all fields to default values, so we need modify only to those that vary from the default. If necessary, we can even make the parameter object an interface, to allow more flexible implementation.

这里,这个options对象的构造方法被设置默认值对所有的域,所以我们需要修改这些不同于默认的值。如果必要,我们甚至能使参数对象成一个接口,允许更多的灵活性实现。

This approach works particularly well with constructors. It's indicated when a class has many constructors, and subclasses may face excessive work just preserving superclass constructor permutations. Instead, subclasses can use a subclass of the superclass constructor's parameter object.

这种方式在构造方法中工作的特别好。它暗示了当一个类有许多构造方法时,子类可以面临过度的工作仅仅保留超类构造函数的排列。代替的是,子类能用一个父类的构造参数对象。

The Command design pattern uses this approach: a command is effectively a consolidated set of parameters, which are much easier to work with together than individually.

命令模式通过这种方式:一个命令是有效的合并了参数集,相比于个体更容易在一起工作。

The disadvantage of parameter consolidation is the potential creation of many objects, which increases memory usage and the need for garbage collection. Objects consume heap space; primitives don't. Whether this matters depends on how often the method will be called.

参数合并的缺点是潜在的创建了许多对象,增长了内存的利用率和需要更多的垃圾回收。对象花费堆内存;原始类型不会。这个问题依赖与是否这个方法经常被调用。

Note

注意

Consolidating method parameters in a single object can occasionally cause performance degradation in J2EE applications if the method call is potentially remote (a call on the remote interface of an EJB), as marshaling and unmarshaling several primitive parameters will always be faster than marshaling and unmarshaling an object. However, this isn't a concern unless the method is invoked particularly often (which might indicate poor application partitioning – we don't want to make frequent remote calls if we can avoid it).

巩固方法参数在单个的对象能偶然的引起性能退化在j2ee应用中如果方法调用是远程的(调用在ejb远程接口上),作为封送和解封几个原始参数将总是更快的比封送和解封对象。然而,这不是问题除非方法别调用的特别的多(可能表明可怜的应用分区--我们不应该去做频繁的远程调用如果我们避免它)。

抱歉!评论已关闭.