Python @ DjangoSpin

PyPro #88 Adapter Design Pattern

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon
Reading Time: 1 minutes

Adapter Design Pattern in Python

Adapter Design Pattern in Python

Write a Python program to implement Adapter Design Pattern.

# Example #1: Language Translator
# Our client, a French-only speaking person, wishes to have a two-way conversation in French. He expects the other person to be able to speak French.
# Our existing class, an English-only speaking person cannot speak French.
# Our Adapter, is in the form of a translator who translates English responses to French so that our French-only speaking client can have his two-way conversation in French.
# Existing Class: a.k.a. Adaptee: Incompatible interface # 1
class EnglishSpeaker:
        def responseToGreeting(self):
                return "Hello to you too!"
        def responseToFarewell(self):
                return "Goodbye my friend."
# Adapter Class, which takes functionality provided by EnglishSpeaker, morphs it into functionality expected by the FrenchSpeaker.
class Translator:
        '''Accespts an english speaker, translates his responses to French.'''
        _englishSpeaker = None
        _englishToFrenchPhrases = {
                "Hello to you too!": "Bonjour à vous aussi",
                "Goodbye my friend.": "Au revoir mon ami"
        def __init__(self, englishSpeaker):
                self._englishSpeaker = englishSpeaker
# Client: Incompatible interface # 2
class FrenchSpeaker:
        '''Accepts an English-To-French Speaker as argument.'''
        _englishToFrenchTranslator = None
        def __init__(self, englishToFrenchTranslator):
                self._englishToFrenchTranslator = englishToFrenchTranslator
        def exchangeGreetings(self):
                print( self._englishToFrenchTranslator._englishToFrenchPhrases[  self._englishToFrenchTranslator._englishSpeaker.responseToGreeting()  ] )
        def exchangeFarewell(self):
                print("Au revoir!")
                print( self._englishToFrenchTranslator._englishToFrenchPhrases[  self._englishToFrenchTranslator._englishSpeaker.responseToFarewell()  ] )
# Create an English Speaking person
englishSpeaker = EnglishSpeaker()
# Create a translator with popular english phrases
englishToFrenchTranslator = Translator(englishSpeaker)
# The French Speaking Person can now get responses in French
frenchSpeaker = FrenchSpeaker(englishToFrenchTranslator)
# Two-way conversation in French
Bonjour à vous aussi
Au revoir!
Au revoir mon ami
# Example #2: Square-pin to round-pin adapter
# Consider two incompatible interfaces: round-pin Socket & square-pin Electric Kettle.
# The Electric Kettle expects a square-pin connection for it to work.
# The Adapter takes square-pin of Electric Kettle, and connects to round-pin socket.
# Existing Class: a.k.a. Adaptee: Incompatible interface # 1
class Socket:
    _pinType = "Round"
# The Adapter: acts as an interface between two incompatible interfaces
class Adapter:
    _socket = None
    _pinType = "SquareToRound"
    def __init__(self, socket):
        self._socket = socket
# Client: Incompatible interface # 2
class ElectricKettle:
    _adapter = None
    _pinType = "Square"
    def __init__(self, adapter):
        self._adapter = adapter
    def makeTea(self):
        if self._adapter._pinType == (self._pinType + "To" + self._adapter._socket._pinType):       # "SquareToRound" == "Square" + "To" + "Round"
            print("Boiling water....")
            print("Adding ingredients...")
            print("Tea brewing...")
            print("Tea is ready!")
            print("No power. Can't function.")
# Create a socket
roundPinSocket  = Socket()
# Connect adapter and socket
squareToRoundAdapter = Adapter(roundPinSocket)
# Connect Kettle and socket via adapter.
kettle  = ElectricKettle(squareToRoundAdapter)
Boiling water....
Adding ingredients...
Tea brewing...
Tea is ready!

See also:

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon

Leave a Reply