简体   繁体   中英

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.

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


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

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.

I'd say this is pretty much ac# < 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?

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 .

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.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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