Source and Decorator base

Step 1: Source and Decorator base

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.

  • Source already holds some text and returns it from read(). Nothing to do there.
  • Decorator wraps another object (its wrapped attribute) and must expose the same read() interface. The base decorator is transparent: it simply forwards the call to the thing it wraps and returns the result unchanged.

Implement Decorator.read so a bare Decorator behaves identically to the source it wraps. This delegation is the backbone every later transformer builds on.

Hints
Starter code
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
Write it on a laptop

The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.