这是众所周知的设计模式吗?它的名字是什么?

发布于 2024-08-29 01:04:23 字数 399 浏览 2 评论 0原文

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

我有一个带有 2 个参数的方法,它调用一个具有 3 个参数的重载方法,并故意将第三个参数设置为空细绳。

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 个参数的方法。

这是一种模式吗?它有名字吗?

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

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
}

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

栀子花开つ 2024-09-05 01:04:23

它实际上不仅仅是方法重载(其中通常相同的方法名称具有不同的参数类型),这种特定模式 - 重载基本上是相同的方法,并且更短一种使用默认值调用较长的模式来模拟可选参数——称为伸缩/伸缩模式,通常在构造函数中看到,但当然可以推广到任何方法。


如需更权威的引用,请参阅《Effective Java 第二版》的摘录,第 2 项:面对许多构造函数参数时考虑构建器模式 (在线摘录

传统上,程序员使用伸缩构造函数模式,在该模式中,您提供一个仅具有必需参数的构造函数,另一个具有单个可选参数,第三个具有两个可选参数,依此类推。 ..

...... ,通常在构造函数的上下文中讨论伸缩模式(例如,2-arg 构造函数将有一行 this(arg1, arg2, ARG3_DEFAULT); 来调用 3-arg 构造函数等) ,但我不明白为什么它不能推广到其他方法。


不幸的是,另一个权威引用没有模式的定义: Sun Developer Network: How to Write Doc Comments for Javadoc 工具

请注意,方法和构造函数按“伸缩”顺序排列,这意味着首先是“无参数”形式,然后是“1 arg”形式,然后是“2 arg”形式,依此类推。


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

伸缩方法

您可能有一个需要一定数量参数的函数。最后几个参数可能并不是那么重要,大多数用户会因为必须弄清楚要传递给它们的内容而感到恼火。因此,您创建了更多具有相同名称和更少参数的方法,这些方法调用“主”方法。

最后引用直接表明对默认参数的语言支持是一个更好的选择。

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.


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...

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.


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

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.


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

Telescoping Methods

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.

过去的过去 2024-09-05 01:04:23

它的名称是重载,它不是一种设计模式,而是一种 OOP 功能

http://en.wikipedia.org /wiki/Method_overloading

The name of that is overloading and it's not a design pattern but a OOP feature

http://en.wikipedia.org/wiki/Method_overloading

爺獨霸怡葒院 2024-09-05 01:04:23

不,这不是四种意义上的设计模式,但它在许多不允许默认参数的语言中很常见。

在像 ruby​​ 这样的语言中,您可以执行类似的操作,如下所示

  def dowork(name, phoneNumber, emailAddress = '')
    # code here
  end

No, this is not a design pattern in the gang of four sense, but it is common in many languages that do not allow default parameters.

In a language like ruby you could do something similar as follows

  def dowork(name, phoneNumber, emailAddress = '')
    # code here
  end
分分钟 2024-09-05 01:04:23

这是辅助方法的示例。天哪,没有出现在《四人帮》书中并不能阻止它成为一种模式。在特定情况下,助手是公共的,而被帮助的方法是私有的,这是封装的一个例子。在这种情况下,封装可能有点太多了。

It's an example of a helper method. Sheesh people, not being in the Gang of Four book doesn't stop it being a pattern. In the specific case where there helper is public and the helped method is private it's an example of encapsulation. Possibly a little too much encapsulation in this case.

我想说这几乎是 ac# < 4 解决缺少默认参数的问题。如果你认为“模式缺少语言特征”学派,我想你可以说它是一种模式,尽管不是一种通常被命名的模式。

编辑:好吧,你的更新确实让我困惑,我不明白你试图用调用私有方法的公共方法做什么。就公共 api 而言,您可以将所有私有方法代码移至公共方法中,并为“默认”值设置一个局部变量。或者这两个方法也从类中的其他地方调用?

I'd say this is pretty much a c# < 4 work around for lack of default arguments. if you prescribe to the 'patterns are missing language features' school of thought i guess you could say its a pattern, though not one that commonly gets named.

edit: ok your update has really thrown me, i don't see what your are trying to do with a public method calling a private method. as far as the public api is concerned you could just move all the private methods code into the public method and have a local variable for the 'defaulted' value. or are both methods also called from other places in the class?

沙与沫 2024-09-05 01:04:23

我猜测您的 DoWork 方法应该被称为 CreateContact ,或者您对 CreateContact 的调用应该是 DoWork 。但这

并不是真正的模式。这只是方法重载的常见用法。

I'm guessing either your DoWork methods should be called CreateContact or your call to CreateContact should be DoWork...

But this isn't really a pattern. It's just a common use of method overloading.

<逆流佳人身旁 2024-09-05 01:04:23

它被称为函数重载。在函数重载中,函数的名称相同,但参数类型或参数数量不同。它也被称为假多态性。它不是一种模式,而是一个基本的 OOP 概念。

It is called as Function Overloading.In Function overloading name of a functions are same but they are differ in either type of parameter or number of parameter. It is also called as false polymorphism. It is not a pattern, it is a basic OOP concept.

我做我的改变 2024-09-05 01:04:23

我同意@polygenelubricants,但想指出的是,除了过载之外,还可以使用伸缩模式。 Objective-C 就是一个例子,其中方法选择器(签名)必须是唯一的并且不能重载。

- (id)init {
    return [self initWithParam:0];
}

- (id)initWithParam:(int)param {
    // Do real initialization here!
    return self;
}

I agree with @polygenelubricants, but would like to point out that the telescopic pattern can be used apart from overloading. An example is in Objective-C, where method selectors (signatures) must be unique and can't be overloaded.

- (id)init {
    return [self initWithParam:0];
}

- (id)initWithParam:(int)param {
    // Do real initialization here!
    return self;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文