Created
September 13, 2011 18:06
-
-
Save alecmunro/1214536 to your computer and use it in GitHub Desktop.
Adapter Pattern example
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
class IPaintable(Interface): | |
def get_color(): | |
pass | |
def paint(color): | |
pass | |
class BicyclePainter(object): | |
"""Adapts a Bicycle to the IPaintable interface.""" | |
def __init__(self, context): | |
self.context = context | |
def paint(self, color): | |
self.context.frame.color = color | |
def get_color(self): | |
return self.context.frame.color | |
class Bicycle(Vehicle): | |
def __init__(self): | |
self.frame = BicycleFrame() | |
self.tires = [BicycleTire(), BicycleTire()] | |
bike = Bicycle() | |
#If we had adapter lookup functionality, we could do this: | |
#painter = IPaintable(bike) | |
#But we don't, so... | |
painter = BicyclePainter(bike) | |
painter.paint("RED") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment