All Courses

Design Patterns Adapter in Python

Jigisha Sata

2 years ago

Design Patterns Adapter In Python | insideAIML
Table of Contents
  • Introduction
  • The adapter pattern
             1. Object Adapter Pattern
             2. Class Adapter Pattern
  • Implementation of the adapter pattern
  • Explanation

Introduction

          Adapter pattern builds the bridge between two incompatible interfaces. This design pattern also comes under structural pattern as decorators. Adaptor Design pattern combines the capability of two independent interfaces.
Adapter involves a single class, which is responsible to join functionalities of independent or incompatible interfaces.
Let’s take a real-life example that could help us understand it better.

Adapter Pattern

          The adaptor may be similar to the case of a card reader, which acts as an adapter between the memory card and a laptop. We plugin the memory card into the card reader and the card reader into the laptop so that memory card can be read via the laptop.
The adapter design helps to work for classes together. It changes the interface of one class into others as per the requirement. It includes speciation a polymorphism which try to names into one name with multiple forms. For example, a shape class which can be used as per the requirements gathered.
The adapter pattern is mainly of two types–

1. Object Adapter Pattern

          Object Adapter Pattern design pattern relies on object implementation. Hence, it is known as the Object Adapter Pattern.

2. Class Adapter Pattern

          Class Adapter Pattern is an alternative way to implement the adapter design pattern. This pattern can be implemented using multiple inheritances.

Implementation of the adapter pattern

Let’s take an example and see how the adapter pattern is implemented.
class EuropeanSocketInterface:
   def voltage(self): pass

   def live(self): pass
   def neutral(self): pass
   def earth(self): pass

# Adaptee
class Socket(EuropeanSocketInterface):
   def voltage(self):
      return 230

               def live(self):
      return 1
def neutral(self):
      return -1
   
   def earth(self):
      return 0

# Target interface
class USASocketInterface:
   def voltage(self): pass
   def live(self): pass
   def neutral(self): pass

# The Adapter
class Adapter(USASocketInterface):
   __socket = none
   def __init__(self, socket):
      self.__socket = socket
   
   def voltage(self):
      return 110
   
   def live(self):
      return self.__socket.live()
   def neutral(self):
      return self.__socket.neutral()
# Client
class ElectricKettle:
   __power = none
   
   def __init__(self, power):
                  self.__power = power
   
   def boil(self):
      if self.__power.voltage() > 110:
         print "Kettle on fire!"
      else:
         if self.__power.live() == 1 and \
            self.__power.neutral() == -1:
            print "Coffee time!"
         else:
            print "No power."

def main():
   # Plug in
   socket = Socket()
   adapter = Adapter(socket)
   kettle = ElectricKettle(adapter)
               
   # Make coffee
   kettle.boil()
               
   return 0
               
if __name__ == "__main__":
   main()

Output
The output of the above program is−
Output for Design Patterns Adapter In Python | insideAIML

Explanation

      This code uses adapter interface with various parameters and attributes. It includes Adaptee along with Target interface that implements all the attributes and displays the output as visible.
So, you can see the output what we got above.
I hope you enjoyed reading this article and finally, you came to know about Design Patterns Adapter in Python.
      
For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.
Thanks for reading…
Happy Learning…

Submit Review