Skip to content

Instantly share code, notes, and snippets.

@alecmunro
Created September 13, 2011 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alecmunro/1214536 to your computer and use it in GitHub Desktop.
Save alecmunro/1214536 to your computer and use it in GitHub Desktop.
Adapter Pattern example
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