AlgoViz

Adapter pattern

easy 50 min

The Adapter pattern makes an existing class fit the interface that its client expects, without changing either of them.

Watch lesson4:39

Where it shows up

The same pattern appears across everyday software.

  • Java: Arrays.asList

    Gives an array the interface of a List. Its documentation calls it a bridge between array-based and collection-based APIs. The list writes through to the array, and its size is fixed, so add is refused: an adaptee that can not keep every promise.

  • Java: InputStreamReader

    Described as a bridge from byte streams to character streams: it reads bytes and decodes them with a charset. The charset is the part that must be translated, not only the method names.

  • C++: std::stack and std::queue

    The standard library calls them container adaptors. std::stack offers push and pop and does the work with another container, a deque by default.

  • Python: io.TextIOWrapper

    A buffered text stream on top of a buffered binary stream. It adapts bytes to text, with encoding, error handling and newline translation as settings.

  • Node.js: util.promisify

    Takes a function that follows the error-first callback style and returns one that returns promises. A function adapter for the way of waiting.

  • Spring MVC: HandlerAdapter

    Helps the DispatcherServlet to call a handler regardless of how the handler is invoked. The reference says that its main purpose is to shield the servlet from such details.

  • Android: RecyclerView.Adapter

    Provides a binding from an app's data set to the views shown in a list. The list asks for a count and for a view at a position, the data can have any shape.

  • Requests: transport adapters

    The Python library defines the way of talking to a service in objects that are mounted for an address prefix. The default one adapts the library urllib3.

  • Anti-corruption layer

    The Azure architecture guide describes a facade or adapter layer between subsystems that do not share the same semantics, for example during the migration of an old system.