A second shape shows why the Bridge scales. We do NOT create VectorSquare or RasterSquare classes. Instead we extend the Renderer interface with a square method and add one Square abstraction.
Implement three things:
class Renderer:
def render_circle(self, radius):
raise NotImplementedError
def render_square(self, side):
raise NotImplementedError
class VectorRenderer(Renderer):
def render_circle(self, radius):
return "vector circle r=" + str(radius)
def render_square(self, side):
"""Return a vector description of a square of the given side."""
# TODO: return a string like 'vector square s=4'
pass
class RasterRenderer(Renderer):
def render_circle(self, radius):
return "raster pixels for circle r=" + str(radius)
def render_square(self, side):
"""Return a raster description of a square of the given side."""
# TODO: return a string like 'raster pixels for square s=4'
pass
class Shape:
def __init__(self, renderer):
self.renderer = renderer
def draw(self):
raise NotImplementedError
class Circle(Shape):
def __init__(self, renderer, radius):
super().__init__(renderer)
self.radius = radius
def draw(self):
return self.renderer.render_circle(self.radius)
class Square(Shape):
def __init__(self, renderer, side):
super().__init__(renderer)
self.side = side
def draw(self):
"""Draw this square by delegating to the bound renderer."""
# TODO: call self.renderer.render_square(self.side) and return it
pass
The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.
A second shape shows why the Bridge scales. We do NOT create VectorSquare or RasterSquare classes. Instead we extend the Renderer interface with a square method and add one Square abstraction.
Implement three things:
Press Run Tests or ββ© to check your solution.