The Adapter Method is a design pattern that allows objects with incompatible interfaces to collaborate. It depicts the representation of a bridge between two different incompatible interfaces. It is a type of Structural design pattern; it involves a single class to which this class is responsible for joining the functionalities of both independent interfaces.
From our phone charger, you will notice there’s an adapter attached with a USB cord that connects to the socket. This Adapter is for connecting or interfacing different brands of phone fibres.
For example, you are to implement how you connect your pressing iron or your electric kettle to the wall socket; there are some things like a socket, voltage, etc. So the Adapter method is going to be implemented in python using the Pressing Iron and Electric Kettle scenario.
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 # 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 1 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") # Client 2 class ElectricIron: __power = None def __init__(self, power): self.__power = power def press(self): if self.__power.voltage() > 110: print("Your Iron on fire") else: if self.__power.live() == 1 and self.__power.neutral() == -1: print("Pressing time") else: print("No power") def main(): # Plug in socket = Socket() adapter = Adapter(socket) kettle = ElectricKettle(adapter) iron = ElectricIron(adapter) # Make coffee kettle.boil() # press cloth iron.press() return 0 if __name__ == '__main__': main()
ProBook:~/InfosecAddicts/python design pattern$ python adapter.py Coffee time Pressing time
The code includes the adapter interface with various attributes and parameters along with the target interface, which implements all the features and displays the output as visible.
Types of Adapter Method
The adapter method is categorized into two types, and they are:
- Object Adapter Method
- The object method relies on object implementation
- Class Adapter Method
- The class method is an alternative way to implement the adapter method and also implements multiple inheritances.
Pros and Cons
- it helps to achieve a certain level of flexibility of the code
- another client could reuse the code without any flaws.
- Open/Closed Principle
- The new adapter classes added to the code without violating the Open/Closed principle
- Single responsibility principle
- the single responsibility principle is achieved because it helps separate the concrete code from the primary logic of the client.
- Code Complexity
- The new class(object and interfaces) introduced was responsible for the rises in the complexity of the code.