简体   繁体   中英

Public method calls private method with the same name - what is this pattern?

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?

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.

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.

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