Adapter
An adapter is a construct whcih adapts an existing interface to conform to another required interface. This could be a factory which takes one object and transforms it into another, or it can be a wrapper around the object that performs the operations on the fly.
python
from unittest import TestCase
class Square:
def __init__(self, side=0):
self.side = side
def calculate_area(rc):
return rc.width * rc.height
class SquareToRectangleAdapter:
def __init__(self, square):
self.square = square
@property
def width(self):
return self.square.side
@property
def height(self):
return self.square.side
class Evaluate(TestCase):
def test_exercise(self):
sq = Square(11)
adapter = SquareToRectangleAdapter(sq)
self.assertEqual(121, calculate_area(adapter))
sq.side = 10
self.assertEqual(100, calculate_area(adapter))