Skip to content

Instantly share code, notes, and snippets.

@jdavis
Created April 17, 2013 12:03
Show Gist options
  • Save jdavis/5403713 to your computer and use it in GitHub Desktop.
Save jdavis/5403713 to your computer and use it in GitHub Desktop.
Example of the Adapter Design Pattern in glorious Python code.
"""
Example of the Adapter Design Pattern
"""
class RocketShip(object):
def turnOn(self):
raise NotImplementedError()
def turnOff(self):
raise NotImplementedError()
def blastOff(self):
raise NotImplementedError()
def fly(self):
raise NotImplementedError()
class NASAShip(RocketShip):
def turnOn(self):
print('NASAShip turning on')
def turnOff(self):
print('NASAShip turning off')
def blastOff(self):
print('NASAShip blasting off')
def fly(self):
print('NASAShip flying!')
class SpaceXShip(object):
def ignition(self):
print('Starting SpaceXShip ignition')
def on(self):
print('Turning on SpaceXShip')
def off(self):
print('Turning off SpaceXShip')
def launch(self):
print('Launching SpaceXShip')
def fly(self):
print('SpaceXShip flying!')
class SpaceXAdapter(RocketShip):
def __init__(self):
self.ship = SpaceXShip()
def turnOn(self):
self.ship.on()
self.ship.ignition()
def turnOff(self):
self.ship.off()
def blastOff(self):
self.ship.launch()
def fly(self):
self.ship.fly()
@jdavis
Copy link
Author

jdavis commented Apr 18, 2013

I just realized that this code isn't PEP8 compliant. AHHHHHHHHHH

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment