Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 787
GenEric35 Avatar asked Apr 14 '10 12:04

GenEric35


People also ask

What is pattern name in design pattern?

A design pattern systematically names, motivates, and explains a general design that addresses a recurring design problem in object-oriented systems. It describes the problem, the solution, when to apply the solution, and its consequences. It also gives implementation hints and examples.

What is an example of a design pattern?

Design patterns provide a standard terminology and are specific to particular scenario. For example, a singleton design pattern signifies use of single object so all developers familiar with single design pattern will make use of single object and they can tell each other that program is following a singleton pattern.


4 Answers

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.

like image 151
polygenelubricants Avatar answered Sep 18 '22 01:09

polygenelubricants


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

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

like image 32
Claudio Redi Avatar answered Sep 19 '22 01:09

Claudio Redi


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
like image 32
Daniel Avatar answered Sep 21 '22 01:09

Daniel


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.

like image 25
Steve Avatar answered Sep 21 '22 01:09

Steve