Design Patterns Adapter in Python

Jigisha Sata

a year ago

Design Patterns Adapter In Python
Design Patterns Adapter In Python
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.
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–
Object Adapter Pattern
Object Adapter Pattern design pattern relies on object implementation. Hence, it is known as the Object Adapter Pattern.
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
Output for Design Patterns Adapter In Python
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

We're Online!

Chat now for any query