Middle
Can you tell me something about the Adapter pattern?
sobes.tech AI
Answer from AI
The Adapter pattern is used to convert the interface of one class into an interface expected by the client. This allows classes with incompatible interfaces to work together.
Example in Python:
class OldSystem:
def specific_request(self):
return "Data in old format"
class Adapter:
def __init__(self, old_system):
self.old_system = old_system
def request(self):
# convert call to old interface
data = self.old_system.specific_request()
return f"Adapted data: {data}"
old = OldSystem()
adapter = Adapter(old)
print(adapter.request()) # Adapted data: Data in old format
Thus, the Adapter encapsulates calls to the old interface and provides a new one expected by the client.