Created
April 17, 2013 12:03
-
-
Save jdavis/5403713 to your computer and use it in GitHub Desktop.
Example of the Adapter Design Pattern in glorious Python code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I just realized that this code isn't PEP8 compliant. AHHHHHHHHHH