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

本文是小编为大家收集整理的关于这是一个众所周知的设计模式吗?它的名字是什么?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

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

我有一个带有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个参数的方法.

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

推荐答案

实际上它不仅仅是方法过载(其中通常相同的方法名称具有不同的参数类型),此特定模式 - 基本上是过载相同的方法,较短的方法调用较长的默认值以模拟可选参数 - 称为望远镜/望远镜模式,通常在构造函数上看到,但当然可以推广到任何方法.

.


对于更具权威的报价,这是有效Java第二版,项目2的摘录:当面对许多构造函数参数时,请考虑一个构建器模式(摘录在线)

传统上,程序员使用了望远镜构造函数模式,其中您仅提供一个只需必需参数的构造函数,另一个带有单个可选参数,第三个带有两个可选参数,等等. ..

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


另一篇权威报价,不幸的是没有对该模式的定义: sun developer "> sun developter "> sun developer network:如何写作Javadoc工具的DOC评论:

请注意,方法和构造函数在"伸缩"顺序中,这意味着首先是" no arg"表单,然后是" 1 arg"表单,然后是" 2 arg"形式,等等.


和另一个随机报价,具有对模式的更明确的定义:我是讨厌的方法过载(您可以!):

望远镜方法

您可能具有一个需要一些参数的函数.最后几个参数可能并不那么重要,大多数用户将不得不弄清楚将什么传递到它们时会感到烦恼.因此,您可以创建一些具有相同名称和更少参数的方法,这些方法调用了"主"方法.

最后一句话直接提出了对默认参数的语言支持是一个更好的选择.

其他推荐答案

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

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

其他推荐答案

否,这不是四个意义上的设计模式,但在许多语言中不允许默认参数.

在像Ruby这样的语言中,您可以做类似的事情

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

本文地址:https://www.itbaoku.cn/post/627639.html

问题描述

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?

推荐答案

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.

其他推荐答案

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

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

其他推荐答案

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