Skip to content

Week 9 - Adapter & Facade Design Patterns

The Adapter and Facade Design Patterns

Classification

  • Structural Design Pattern

Pattern Definition

Adapter Design Pattern

  • Hard Definition: Converts the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.
  • Easy Definition: Allow two unrelated interfaces work together.

Facade Design Pattern

  • Hard Definition: Provide a unified interface to a set of interfaces in a subsystem. Facade Pattern defines a higher-level interface that makes the subsystem easier to use.
  • Easy Definition: A facade “wraps” a set of objects to simplify interacting with them.

Representations

Mermaid Graph - Object Adapter

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
classDiagram
    Client --> Target
    Target <|.. Adapter
    Adaptee --* Adapter
    class Client {

    }
    class Target {
        <<interface>>
        +request()
    }
    class Adapter {
        +request()
    }
    class Adaptee {
        +specificRequest()
    }

Mermaid Graph - Class Adapter

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
classDiagram
    Client --> Target
    Target <|.. Adapter
    Adaptee <|-- Adapter
    class Client {

    }
    class Target {
        <<interface>>
        +request()
    }
    class Adapter {
        +request()
    }
    class Adaptee {
        +specificRequest()
    }

Mermaid Graph - Facade

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
classDiagram
    Client --> Facade
    Facade *-- ComplexSubClass1
    Facade *-- ComplexSubClass2
    Facade *-- ComplexSubClass3
    ComplexSubClass1 --> ComplexSubClass2
    ComplexSubClass1 --> ComplexSubClass3
    ComplexSubClass2 --> ComplexSubClass3
    ComplexSubClass3 --> ComplexSubClass1
    class Client {

    }
    class Facade {
        +SimplerActions()
    }
    class ComplexSubClass1 {
    }
    class ComplexSubClass2 {
    }
    class ComplexSubClass3 {
    }   

UML

Object Adapter
Class Adapter
Facade

Real World Usages

Adapter

  • Converting Enumerators to Iterators and vice versa.
  • java.io classes InputStreamReader and OutputStreamWriter adapts an InputStream into a Reader and adapts an OutputStream into a Writer
  • java.util classes such as array with methods like asList converts an Array into a List.

Facade

  • JDBC Driver Manager

Java Code Example

Main Take-Aways from the textbook

  • When you need to use an existing class and its interface is not the one you need, use an adapter.
  • When you need to simplify and unify a large interface or complex set of interfaces, use a facade.
  • An adapter changes an interface into one a client expects.
  • A facade decouples a client from a complex subsystem.
  • Implementing an adapter may require little work or a great deal of work depending on the size and complexity of the target interface.
  • Implementing a facade requires that we compose the facade with its subsystem and use delegation to perform the work of the facade.
  • There are two forms of the Adapter Pattern: object and class adapters.
  • You can implement more than one facade for a subsystem.
  • An adapter wraps an object to change its interface, a decorator wraps an object to add new behaviors and responsibilities, and a facade “wraps” a set of objects to simplify.
  • Facade design pattern can be applied at any point of development, usually when the number of interfaces grow and system gets complex.
  • Facade design pattern should be applied for similar kind of interfaces, its purpose is to provide a single interface rather than multiple interfaces that does the similar kind of jobs.

Main OO Principles of Adapter Pattern

Main OO Principles of Facade Pattern