3

I was reading Python's requests library's code to find out how it works. Since this library has a simple usage interface, it creates a more complex object beyond. For instance:

requests.get(...)

Is a shortcut to something like:

s = Session()
r = s.request("get", ...)
return r

I do like this approach but I'd like to know whether it is a well-known design pattern or not. It's important to say that we are able to use Session class (it isn't a private/internal class).

We can see a similar (little bit different I guess) technique in Javascript.

Date.now = function () { return new Date().getTime ....

Is there a name to this approach? After spending some minutes looking for it, I wasn't able to find a suitable explanation. Should I consider it as a Proxy or a Facade?

  • The link to the Requests library doesn't seem to have the code you cited. Could you provide a more specific reference? The function defined on lines 59-70 is just a wrapper. – Joel Harmon Sep 13 '16 at 02:48
  • I've just updated the link. This behavior could be found at line 56, inside a context manager. – Jonathan Simon Prates Sep 13 '16 at 03:44

2 Answers2

3

I think there is many way to classify the idiom.

Wrapper of course, but it can lead to many patterns associate with it.

book Implementation Patterns mentions some patterns:

LIBRARY CLASS in Chapter 5:

Where do you put functionality that doesn’t fit into any object? One solution is to create static methods on an otherwise-empty class. No one is expected to ever create instances of this class. It is just there as a holder for the functions in the library.

also METHOD OBJECT in Chapter 7:

This is one of my favorite patterns, probably because I use it so infrequently but the results are spectacular when I do. Creating method object can help you turn a tangled mass of code packed into an impossible method into readable, clear code that gradually reveals details to readers. This is a pattern I apply after I have some code working, and the more complex the method the better.

Also I think it is a simple Adapter/Facade pattern as @Joel Harmon already mention.

I think The purpose of the idiom in your case:

  1. Requests library:
    1. reveals details to readers/users
    2. keep api easy to use
    3. Layers Architecture, High Level Low Level divide, low level can change, high level api won't
  2. Date.now:

    I look on https://developer.mozilla.org

Description

Because now() is a static method of Date, you always use it as Date.now().

Polyfill

This method was standardized in ECMA-262 5th edition. Engines which have not been updated to support this method can work around the absence of this method using the following shim:

if (!Date.now) {
  Date.now = function now() {
    return new Date().getTime();
  };
}
  1. "using the following shim" so it is a adapter
  2. "static method of Date", yes LIBRARY CLASS as the book Implementation Patterns said.
Gohan
  • 176
  • 4
1

This idiom is simply a wrapper.

The function you call provides a somewhat cleaner interface to another function that would otherwise appear more awkward to use. I called it an idiom, rather than a pattern, because it's three lines of code. As I understand it, this is used extensively in Functional Programming as well as any language that supports functions as first class objects.

If, however, you're looking for a term referring to using wrappers to create a new interface for existing functionality, I'd consider that the Adapter pattern. If it was working with more than one backing object (in this case, the session.request function), it would be the Facade pattern.

Joel Harmon
  • 1,063
  • 7
  • 10