CoursesGalaxy Courses Subscription Knowledge Hub
Knowledge Hub
Design Patterns
    Creational Patterns
    Structural Patterns
        Adapter
        Bridge
        Composite
        Decorator
        Facade
        Flyweight
        Proxy
    Behavioral Patterns


Adapter Design Pattern


Adapter design pattern is used to provide a way for reusing an existing class.


Intent
  • Convert the interface of a class into another interface clients expect. Adapter lets classes work together that could not otherwise because of incompatible interfaces.
The intent is to convert the interface of existing class to the one which client is expecting.

Problem
  • Want to use existing system/component but the current system used by client does not have interfaces that are compatible with existing one.
The client has the interface that he wants to use, we have an existing class which can be reused but it has different interface. So either a new class is required or we have to find a way to use the existing one.

Solution
  • Come up with a class which adapts the interface of existing system/component to the new one which client expects.
The system will adapt the existing interface and will provide it in the way client expects.

Where it is applicable?
  • There is an existing class which we want to use in a system and may require to provide interface in the way applicable for the system. Or we can say we want to reuse the class but we don't have compatible interfaces.
Structure

Composition

Composition structure also called as object adapter.

Adapter Design Pattern - Composition

Participant Classes-
  • Target class provides interfaces to clients and has abstract method Request().
  • Adapter class is derived from Target class and implements the method Request().
  • Adaptee has the functionality we want to reuse.
How they work together?
  • The Adapter class has instance of Adaptee and it adapts the methods of Adaptee class. The method Request() of Adapter class in turn uses the method SpecificRequest() of Adaptee. The client is required to use the abstract method Request() only.
  • Suppose we want to use another method which is not available in Adaptee then we can just provide that method in Adapter class itself.
  • We can also have multiple adaptees and adapter can have their instance and adapts the functionality it requires.
Inheritance

Inheritance structure is also called as class adapter.

Adapter Design Pattern - Inheritance

Participant Classes-
  • Target class provides interfaces to clients and has abstract method Request().
  • Adapter class is derived from Target and Adaptee. It implements the method Request() and inherits the functionality of Adaptee.
  • Adaptee has the functionality we want to reuse.
How they work together?
  • Adapter class is derived from Adaptee, so that it can adapt the Adaptee methods. The method SpecificRequest() is called in method Request() of class Adapter which is provided as interface to client.
Example

We want to reuse already available class for HP System operations.

Adapter Design Pattern Example - Composition

OperationManager class provides the interface ShutDown() to client. Concrete class HPOperationManager implements the method ShutDown(). The class HPOPSystem already exists and provides the functionality through its method PowerOff(). HPOperationManager has instance of HPOPSystem and uses it in ShutDown() method to call the method PowerOff().

Implementation Code

C++   Adapter Pattern in C++   Adapter Pattern C++ Example
C#   Adapter Pattern in C#   Example in C#
Java   Adapter Pattern in Java   Example in Java
Python   Adapter Pattern in Python   Example in Python
JavaScript   Adapter Pattern in JavaScript   Example in JavaScript
PHP   Adapter Pattern in PHP   Example in PHP
Ruby   Adapter Pattern in Ruby   Example in Ruby
Swift   Adapter Pattern in Swift   Example in Swift
Objective-C   Adapter Pattern in Objective-C   Example in Objective-C



Suresh Kumar Srivastava is founder of online learning site coursegalaxy.com and author of popular books "C In Depth", "Data Structures Through C In Depth". He has 18+ years experience in industry and worked on architecture and design of multiple products. This article is from his courses on design patterns.







Yearly Subscription


Design Patterns in PHP


Advanced Data Structures in C#

Explore

Courses

Subscription

Knowledge Hub

Design Patterns

Tech Articles

About Us

Overview

Contact Us

CourseGalaxy

CourseGalaxy is focused on transformation of Engineering graduate to Software Engineer to fill the gap between education and industry and provide the training on specific problems of industry to enhance the skills of Engineers in industry to do their job appropriately.

Learn From Us

2018 CourseGalaxy