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

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

问题描述

我经常在代码中看到这种情况,但是当我谈到它时,我不知道这种模式"的名称

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
}

我这样做的原因是为了不重复代码,并允许现有调用者仍然调用只有 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.

要获得更权威的引用,请参阅Effective Java 2nd Edition条款 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-arg 构造函数将有一行 this(arg1, arg2, ARG3_DEFAULT); 来调用 3-arg构造函数等),但我不明白为什么它也不能推广到其他方法.

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 Developer Network: How to WriteJavadoc 工具的文档注释:

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

注意方法和构造函数是按伸缩"顺序排列的,这意味着首先是无参数"形式,然后是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.

<小时>

另一个随机引用,对模式有更明确的定义:我讨厌方法重载(你也可以!):

伸缩方法

您可能有一个接受一些参数的函数.最后几个参数可能不是那么重要,大多数用户会因为不得不弄清楚要传递给它们的内容而烦恼.因此,您创建了更多具有相同名称和更少参数的方法,它们会调用主"方法.

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天全站免登陆