A public method calls a private method with the same name - what is this template?

Consider this code from the 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 fairly common approach. The public method delegates the call to a private method with the same name, but with additional parameters. Is there a name for this template?

+4
source share
2 answers

, . , . , , . , SplitByCharacterType(final String str, final boolean camelCase) , splitByCharacterType(final String str).

Encapsulation. , , /, .

+1

. API, , , () :

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

. Apache StringUtils, , .

, ++, :

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

, , varargs. , javascript :

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

    // ...
}

, null undefined. , javascript :

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

. :

ls

, , :

ls -l

, .

+1

All Articles