Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Steakeye/634f42d81f4489f317b6761b14f9435c to your computer and use it in GitHub Desktop.
Save Steakeye/634f42d81f4489f317b6761b14f9435c to your computer and use it in GitHub Desktop.

Revisions

  1. @Integralist Integralist revised this gist Oct 20, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Design Patterns: Adapter vs Facade vs Bridge.md
    Original file line number Diff line number Diff line change
    @@ -36,7 +36,7 @@ Most DSL's are a facade of some form. The popular jQuery library consists of mul

    The primary function of a Bridge is to decouple an abstraction from its implementation.

    > Adapter makes things work _after_ they're designed
    > Adapter makes things work _after_ they're designed
    > Bridge makes them work _before_ they are
    Imagine you have a function that abstracts the implementation detail of making an HTTP request to an external API endpoint. In a language like JavaScript you might tightly couple the abstraction with the consumer code.
  2. @Integralist Integralist revised this gist Oct 20, 2016. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions Design Patterns: Adapter vs Facade vs Bridge.md
    Original file line number Diff line number Diff line change
    @@ -36,6 +36,9 @@ Most DSL's are a facade of some form. The popular jQuery library consists of mul

    The primary function of a Bridge is to decouple an abstraction from its implementation.

    > Adapter makes things work _after_ they're designed
    > Bridge makes them work _before_ they are
    Imagine you have a function that abstracts the implementation detail of making an HTTP request to an external API endpoint. In a language like JavaScript you might tightly couple the abstraction with the consumer code.

    For example:
  3. @Integralist Integralist revised this gist Sep 26, 2014. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions Design Patterns: Adapter vs Facade vs Bridge.md
    Original file line number Diff line number Diff line change
    @@ -18,6 +18,12 @@ The `B` function cannot be changed and it is dependant on the interface that was

    An adapter can solve this by creating a new function `A2C` which contains the relevant logic for handling the interaction between `B` and `A`.

    ```
    Current | Future
    ----------------
    B(C) | B(A2C(A))
    ```

    ## Facade

    The primary function of a Facade is to simplify the interaction between a consumer and an interface.
  4. @Integralist Integralist revised this gist Sep 26, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Design Patterns: Adapter vs Facade vs Bridge.md
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ Current | Future
    B(C) | B(A)
    ```

    The `B` function cannot be changed and it is dependant on the interface that was originally provided by `C`, but now we are passing in `A` which is has an incompatible interface.
    The `B` function cannot be changed and it is dependant on the interface that was originally provided by `C`, but now we are passing in `A` which has an incompatible interface.

    An adapter can solve this by creating a new function `A2C` which contains the relevant logic for handling the interaction between `B` and `A`.

  5. @Integralist Integralist created this gist Sep 26, 2014.
    65 changes: 65 additions & 0 deletions Design Patterns: Adapter vs Facade vs Bridge.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    The three design patterns (Adapter, Facade and Bridge) all produce the result of a clean public API. The difference between the patterns are usually due to a subtle context shift (and in some cases, a behavioural requirement).

    ## Adapter

    The primary function of an Adapter is to produce a *unified* interface for a number of underlying and unrelated objects.

    You will notice this pattern being utilised in many applications. For example, ActiveRecord (the popular Ruby ORM; object-relational mapping) creates a unified interface as part of its API but the code underneath the interface is able to communicate with many different types of databases. Allowing the consumer of the API to not have to worry about specific database implementation details.

    The principle structure of this pattern is:

    ```
    Current | Future
    ----------------
    B(C) | B(A)
    ```

    The `B` function cannot be changed and it is dependant on the interface that was originally provided by `C`, but now we are passing in `A` which is has an incompatible interface.

    An adapter can solve this by creating a new function `A2C` which contains the relevant logic for handling the interaction between `B` and `A`.

    ## Facade

    The primary function of a Facade is to simplify the interaction between a consumer and an interface.

    Most DSL's are a facade of some form. The popular jQuery library consists of multiple facades (one for each type of feature). For example, the jQuery `ajax` method makes it very easy to make an XHR (`XMLHttpRequest`).

    > The difference between a Facade and an Adapter is that the Facade makes a simple abstraction, where as an Adapter will handle complex interactions by taking incoming data and constructing it to work with the underlying objects.
    ## Bridge

    The primary function of a Bridge is to decouple an abstraction from its implementation.

    Imagine you have a function that abstracts the implementation detail of making an HTTP request to an external API endpoint. In a language like JavaScript you might tightly couple the abstraction with the consumer code.

    For example:

    ```js
    function get(e) {
    return asyncRequest('foo?bar=' + this.id, function(response) {
    console.log(response)
    })
    }

    myTrigger.addEventListener('click', get, false)
    ```

    The above abstraction (i.e. the `get` function) will only ever work within the context of a web browser. The abstraction has been tightly coupled to the consumer.

    Utilising a bridge will allow us to decouple this code:

    ```js
    function get(id, callback) {
    return asyncRequest('foo?bar=' + id, function(response) {
    callback(response)
    })
    }

    function getBridge(e) {
    get(this.id, function(response) {
    console.log(response)
    })
    }

    myTrigger.addEventListener('click', getBridge, false)
    ```