Author - StudySection Post Views - 170 views
Adapter Design

Adapter Design Pattern in Python

Many times code for classes was written, and we didn’t have any option to modify the code or change the interface according to needs. This generally happens when the method we are calling and the method from which we are calling are on different systems.

So here come the Adapters, In simple words, an Adapter is a design pattern that connects two incompatible interfaces or systems. This type of design pattern is also referred to as a structural pattern.

Adapters solve problems like:

  • How can a class be reused that does not have an interface that is required?
  • How can two incompatible classes or systems work together?
  • How can an alternative interface or system be provided to a class?

Adapters provide a single class that can combine two independent interfaces. A real-life example of an adapter is an earphone. Earphones are able to take audio from a phone or tablet and put it in our ears. You can plug the earphone jack into the phone and put earphones in your ears, so you can hear audio.

In two objects Adapter acts as a wrapper. The adapter catches calls from one object or function and changes them according to the requirements of another object.

Basically, two types of adapter patterns exist:

  • Object Adapter Pattern
  • Class Adapter Pattern

Object Adapter Pattern: It works on object implementation.
Class Adapter Pattern: It works using multiple inheritances. It can also refer to an alternative method to implement the adapter pattern.

Adapters are very commonly used in Python. Adapters generally work in systems where old computer source code is used. Adapters are able to convert these legacy code systems to modern code and let them interact with each other.

Note: Adapters are also a kind of decorator that can change the functionality of a function or object.

Adapter patterns are generally composed of:

  • Which classes do adapters have?
  • What functionalities do those classes have?
  • How are patterns related?

Example

class aim:
def request(self) -> str:
return "aim: default aim’s behavior."
class accepter:
def specific_request(self) -> str:
return "accepter: accepter."
class accept:
def __init__(self, accepter: Accepter) -> None:
self.accepter = accepter
def request(self) -> str:
return f"accept: (TRANSLATED) {self.accepter.specific_request()[::-1]}"
def client(aim: Target) -> None:
print(aim.request(), end="")
if __name__ == "__main__":
print("client: interface of aim object work:")
aim = aim()
client_code(aim)
print("\n")
accepter = accepter()
print("client: accepter class has a weird interface. See, I don't understand it:")
print(f"accepter: {accepter.specific_request()}", end="\n\n")
print("client: using accepter things can work:")
adapter = accept(accepter)
client_code(adapter)

Output
client: interface of aim object work:
aim: The default target’s behavior.
client: accepter class has a weird interface. See, I don’t understand it:
accepter: accepter.
client: using accepter things can work:
accept: (TRANSLATED) Special behavior of the accepter.

The code includes an accept interface with various parameters and attributes. It includes an accepter along with an aim interface that implements all the attributes and displays the output as visible.

Diagram

accepter-digram

Summary

  • You can use an adapter if you want to use an existing class, but its interface is not what you want.
  • The adapter modifies the interface of its parent class for situations when it is not possible to modify the parent class that needs to interact with another class.
  • An adapter can provide an alternative interface to an existing object without actually changing it.
  • Adapter act as decorators for existing objects and classes.
  • An adapter can also be referred to as a bridge that acts as a connector between two interfaces and objects or systems.

StudySection gives an opportunity to beginners and experts in .NET framework to go through StudySection’s .NET Certification Exam and get a .NET certification for enhancement of career in programming. If you have knowledge of the .NET framework then you can get a certificate through an online exam at StudySection.

Leave a Reply

Your email address will not be published.