6

Consider this code from Apache Commons StringUtils:

public static String[] splitByCharacterType(final String str) {
    return splitByCharacterType(str, false);
}

private static String[] splitByCharacterType(final String str, final boolean camelCase) {
    // Some code...
}

This is a pretty common approach - public method delegates a call to private method with the same name but with additional parameters. Is there any name for this pattern?

Kao
  • 7,225
  • 9
  • 41
  • 65

2 Answers2

3

Its more likely to be the Facade design pattern. Is more known to provide a unified interface to a set of interfaces in a subsystem. But in this case I think is used to define a higher-level implementation that makes the subsystem easier to use. As you can see the parameters are two in SplitByCharacterType(final String str, final boolean camelCase), but only one is exposed to the outer world via splitByCharacterType(final String str).

Hiding the implementation details is also a concept of Encapsulation. So the other users are being provided with the things they need to know/use and the actual processing is left to the one responsible for it.

Christopher Moore
  • 3,071
  • 4
  • 30
  • 46
ekostadinov
  • 6,880
  • 3
  • 29
  • 47
1

This is an implementation of optional arguments. It is used to provide an API where if you call it without the optional arguments it will proceed with sane (preferably) defaults:

String.splitByCharacterType(text);            // splits the normal way
String.splitByCharacterType(text,CAMEL_CASE); // splits the alternative way

note: I've never used Apache StringUtils so my example above may be wrong but it's only to illustrate the use-case.

In some languages such as C++, the language directly provides syntax to support this use case:

char*[] splitByCharacterType(char* text, bool camelCase = 0) {
    // ...
}

In other languages that have neither function overloading nor optional arguments the same use-case can be implemented using varargs. For example, in javascript you could do this:

function splitByCharacterType (text) {
    var camelCase = false;
    if (arguments.length > 1 && arguments[1] == true) camelCase = true;

    // ...
}

In some languages, you're allowed to call a function with less than the expected number of arguments and the unspecified arguments will simply be given the value of null or undefined. For example, in javascript you can also do this:

function splitByCharacterType (text, camelCase) {
    if (camelCase != undefined) {
        // ..
    }
    else {
        // ..
    }
}

The idea of optional arguments is essentially similar to command line parameters for console applications. For example:

ls

the above invocation generates output you most commonly want but you can also do:

ls -l

for those times when you need more information.

slebetman
  • 109,858
  • 19
  • 140
  • 171
  • You cannot use `splitByCharacterType()` as you have shown, because the second implementation of this method is `private`. – Kao Oct 01 '14 at 09:02
  • Ah. Didn't notice that detail. Perhaps it's to allow derived classes to have access to the underlying method so that the alternate behavior can be invoked? – slebetman Oct 01 '14 at 09:18
  • I don't think so. You cannot access private methods from derived classes. – Kao Oct 01 '14 at 09:26