The Decorator pattern lets you wrap an object in another object that has the same interface, adding behavior on the way through. Real stream libraries do exactly this: a GzipStream wraps a FileStream, a Base64Stream wraps that, and to the caller they all just look like something with a read() method.
We start with two building blocks.
Implement Decorator.read so a bare Decorator behaves identically to the source it wraps. This delegation is the backbone every later transformer builds on.
class Source:
def __init__(self, text):
self.text = text
def read(self):
return self.text
class Decorator:
def __init__(self, wrapped):
self.wrapped = wrapped
def read(self):
"""Delegate to the wrapped source, returning its read() unchanged."""
# TODO:
# return self.wrapped.read()
pass
The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.
The Decorator pattern lets you wrap an object in another object that has the same interface, adding behavior on the way through. Real stream libraries do exactly this: a GzipStream wraps a FileStream, a Base64Stream wraps that, and to the caller they all just look like something with a read() method.
We start with two building blocks.
Implement Decorator.read so a bare Decorator behaves identically to the source it wraps. This delegation is the backbone every later transformer builds on.
Press Run Tests or ββ© to check your solution.