简体   繁体   English

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

[英]Is this a well known design pattern? What is its name?

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. 我有一个带有2个参数的方法,它调用一个有3个参数的重载方法,并故意将第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
}

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. 我这样做的原因是不重复代码,并允许现有的调用者仍然调用只有2个参数的方法。

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 ) 有关更具权威性的报价,请参阅Effective Java 2nd Edition的摘录, 第2项:在面对许多构造函数参数时考虑构建器模式在线摘录

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 eg 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. 同样,通常在构造函数的上下文中讨论伸缩模式(例如,2-arg构造函数将有一行this(arg1, arg2, ARG3_DEFAULT);调用3-arg构造函数等),但我不这样做看看为什么它也不能推广到其他方法。


Another authoritative quote, unfortunately with no definition of the pattern: Sun Developer Network: How to Write Doc Comments for the Javadoc Tool : 另一个权威性的引用,遗憾的是没有定义模式: Sun Developer Network:如何为Javadoc工具编写Doc注释

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. 注意方法和构造函数是“伸缩”顺序,这意味着首先是“no arg”形式,然后是“1 arg”形式,然后是“2 arg”形式,依此类推。


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. 所以你创建了一些具有相同名称和更少参数的方法,这些方法调用了“master”方法。

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 它的名称是重载,它不是一个设计模式,而是一个OOP功能

http://en.wikipedia.org/wiki/Method_overloading 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 在像ruby这样的语言中,你可以做类似的事情如下

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

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. Sheesh人,不在四人帮书中并不能阻止它成为一种模式。 In the specific case where there helper is public and the helped method is private it's an example of encapsulation. 在特定情况下,帮助器是公共的并且help方法是私有的,这是封装的一个例子。 Possibly a little too much encapsulation in this case. 在这种情况下可能有点过多的封装。

I'd say this is pretty much ac# < 4 work around for lack of default arguments. 我会说这是因为缺少默认参数而非常适合#4。 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. 就公共API而言,您可以将所有私有方法代码移动到公共方法中,并为“默认”值设置局部变量。 or are both methods also called from other places in the class? 或者这两种方法也是从班级中的其他地方调用的?

I'm guessing either your DoWork methods should be called CreateContact or your call to CreateContact should be DoWork ... 我猜你的DoWork方法应该叫做CreateContact或者你对CreateContact的调用应该是DoWork ...

But this isn't really a pattern. 但这不是一种模式。 It's just a common use of method overloading . 它只是方法重载的常见用法

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. 它不是一种模式,它是一种基本的OOP概念。

I agree with @polygenelubricants, but would like to point out that the telescopic pattern can be used apart from overloading. 我同意@polygenelubricants,但我想指出,伸缩模式可以用于超载。 An example is in Objective-C, where method selectors (signatures) must be unique and can't be overloaded. 一个例子是Objective-C,其中方法选择器(签名)必须是唯一的并且不能重载。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM