这是一个众所周知的设计模式?它叫什么名字? [英] Is this a well known design pattern? What is its name?

查看:116
本文介绍了这是一个众所周知的设计模式?它叫什么名字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到这个经常在code,但是当我提起它,我不知道这样的模式的名称

I have seen this often in code, but when I speak of it I don't know the name for such 'pattern'

我有2个参数的方法调用,拥有3个参数和故意的重载方法将3一个空字符串。

I have a method with 2 arguments that calls an overloaded method that has 3 arguments and intentionally sets the 3rd one to empty string.

public void DoWork(string name, string phoneNumber)
{
    DoWork(name, phoneNumber, string.Empty)
}

private void DoWork(string name, string phoneNumber, string emailAddress)
{
    //do the work
}

我这样做的原因是为了不重复code,并允许现有的来电者还自称是只有2个参数的方法。

The reason I'm doing this is to not duplicate code, and to allow existing callers to still call the method that has only 2 parameters.

这是一个模式,它有名字吗?

Is this a pattern, and does it have a name?

推荐答案

它实际上不仅仅是方法重载更多的(其中的一般的同样的方法名称有不同的观点的类型的)这个特定的图案 - 其中,过载基本上是相同的方法,和较短的调用与默认值的较长的一个模拟可选参数 - 被称为伸缩/伸缩模式,通常看到的构造,但当然可推广到任何方法。

It's actually more than just method overloading (where usually the same method name has different argument types), this specific pattern -- where overloads are basically the same method, and the shorter one invokes the longer one with default value to emulate optional parameters -- is called the telescopic/telescoping pattern, usually seen on constructors, but of course generalizable to any method.

有关更权威的报价,这里是从的有效的Java第二版第2项的摘录:当面临许多构造函数的参数考虑一个生成器模式(适用的节选在线

For a more authoritative quote, here's an excerpt from Effective Java 2nd Edition, Item 2: Consider a builder pattern when faced with many constructor parameters (excerpt online)

传统地,程序员已经使用的伸缩构造的图案,在其中提供一个构造仅与所需的参数,另一与单个可选参数,第三个具有两个可选参数,依此类推。 ..

Traditionally, programmers have used the telescoping constructor pattern, in which you provide a constructor with only the required parameters, another with a single optional parameters, a third with two optional parameters, and so on...

再次平时伸缩模式构造的情况下(其中例如,一个2参数的构造将有一个行内讨论这(ARG1,ARG2,ARG3_DEFAULT); 调用3参数的构造函数,等等),但我不明白为什么不能推广到其他的方法为好。

Again, usually the telescopic pattern is discussed within the context of constructors (where e.g. a 2-arg constructor would have one line this(arg1, arg2, ARG3_DEFAULT); to invoke the 3-arg constructor, etc), but I don't see why it can't be generalized to other methods as well.

另外一个权威的报价,遗憾的是没有图案的定义: Sun开发者网络:如何编写适用于Javadoc文档注释工具

Another authoritative quote, unfortunately with no definition of the pattern: Sun Developer Network: How to Write Doc Comments for the Javadoc Tool:

注意方法和构造是在伸缩命令,其中第一装置无ARG的形式,那么1 ARG的形式,那么2 ARG的形式,等等。

Notice the methods and constructors are in "telescoping" order, which means the "no arg" form first, then the "1 arg" form, then the "2 arg" form, and so forth.

和另一个随机报价,使用该模式的一个更明确的定义:<一href=\"http://guidewiredevelopment.word$p$pss.com/2009/05/22/i-am-hate-method-overloading-and-so-can-you/\">I我恨方法重载(你也能做!):


And another random quote, with a more explicit definition of the pattern: I Am Hate Method Overloading (And So Can You!):

伸缩方法

您可能有一个函数,它的参数一定数目。最后几个参数可能不是那么重要,大多数用户将是不得不弄清楚如何传递到他们恼火。所以你创建了几个方法具有相同的名称和更少的参数,它通过对大师的方法调用。

You may have a function that takes some number of arguments. The last few arguments may not be all that important, and most users would be annoyed in having to figure out what to pass into them. So you create a few more methods with the same name and fewer arguments, which call through to the "master" method.

这最后一个引号直接提出了默认参数的语言支持是一个更好的选择。

This last quote directly proposes that language support for default arguments is a much better alternative.

这篇关于这是一个众所周知的设计模式?它叫什么名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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