Home Articles

Adapter Wrapper Design Pattern

Let us discuss how Adapter Wrapper Design Pattern in this article:

Adapter Wrapper Design Pattern

It’s one of the structural design pattern. In daily development, we use adapter pattern at many places but sometimes we are not aware of terminology if adapter word is not specified 😁

Intent

The adapter pattern allows two components with incompatible APIs to work together by introducing an adapter that maps from one component to the other. You can adapt interface-interface, interface-implementation and implementation-implementation

  • Adapter pattern use a single class (the adapter class) to join functionalities of incompatible interfaces/classes.
  • It’s also known as wrapper

What are the benefits?

  • This pattern allows you to integrate components for which you cannot modify the source code into your application. It’s mostly an issue while working with third party code.

When should you use this pattern?

  • Use this pattern only when target component provides similar functionality but uses an incompatible API

When should you avoid this pattern?

  • When target doesn’t matches your requirement completely

Pattern correct implementation check

  • When adapter can connect adaptee to target without modifying component.

Any related patterns

This pattern is often used with factory, composition or decorator patterns.

Structure

Extension Adapter

Design Pattern Structure

User is Codable struct which conforms to Identifiable protocol as well for some requirement. User type already has identifier property but to satisfy protocol requirement it had to create identifier property using type extension, it’s called extension adapter. Extensions are a convenient way to add new methods and computed properties to any type in Swift without modifying its implementation.

Object Adapter

If you extract UITableViewDataSource and UITableViewDelegate into separate class then it’s object adapter. Adaptee is HomeView and target is TableView.

Object Adapter

Let’s go through terminologies:

  • Target — This defines the domain-specific interface that the client uses. It’s table view in our example.
  • Adapter — This adapts the interface from the adaptee to the target interface.
  • Adaptee — This defines an existing interface that needs adapting. It’s home view here.

What problem Adapter design pattern tries to solve?

  • How can a class be reused that does not have an interface that a client requires?
  • How can classes that have incompatible interfaces work together?
  • How can an alternative interface be provided for a class?

This is a free third party commenting service we are using for you, which needs you to sign in to post a comment, but the good bit is you can stay anonymous while commenting.