Skip to content

Adapter Design Pattern

Devrath edited this page Mar 5, 2024 · 2 revisions

usb-charger-adapter-mobile-power-charger-smartphone-banner-blue-background-flat-lay-top-view-copy-space-photo_526934-3056

Gangs of Four(GOF) - Definition

Provides an interface between two unrelated entities so that they can work together.

General reference scenario

  • Consider there is a new device we want to connect to our computer via Bluetooth
  • Computer has a Bluetooth device which connects to external devices via `Bluetooth
  • The software of the new device that has to be connected to the computer is different.
  • Similarly the software of the computer Bluetooth is different.
  • Our goal is to link them but both are different and can't be linked directly.
  • We use the adapter pattern to solve this problem.

Examples in Android

Untitled Diagram drawio

  • One good example in Android programming is the usage of an adapter in populating the list.
  • There will be a data source in the activity/fragment and we need to display it in a certain way in the list.
  • The adapter will form a mediation between the data source and how it should be displayed.

Programming Example