Source code for Structural.Adapter.python.adapter

from typing import Protocol

[docs] class Target(Protocol): """ Defines the domain-specific interface used by the client. """
[docs] def request(self) -> str: """ Handles the request in a format expected by the client. :return: A string response. """ pass
[docs] class Adaptee: """ Defines the existing interface with specific functionality that needs adaptation. """
[docs] def specific_request(self) -> str: """ Provides functionality that needs to be adapted. :return: A specific string response. """ return "Adaptee: Specific request called."
[docs] class Adapter(Target): """ Adapts the Adaptee to the Target interface. """
[docs] def __init__(self, adaptee: Adaptee): """ Initializes the Adapter with an Adaptee instance. :param adaptee: The Adaptee instance to be adapted. Must not be None. :raises ValueError: If the provided Adaptee instance is None. """ if not adaptee: raise ValueError("Adaptee cannot be None") self._adaptee = adaptee
[docs] def request(self) -> str: """ Translates the Target's request to the Adaptee's specific_request. :return: A string response translated by the Adapter. """ return f"Adapter: Translating request to Adaptee's specific_request.\n{self._adaptee.specific_request()}"