Decorators can carry their own configuration. A GzipStream might take a compression level; here Repeat takes a times count. Because it needs extra state beyond the wrapped object, it defines its own init that calls the base constructor (to store wrapped) and then saves times.
The init is already written for you. Implement Repeat.read so it returns the wrapped text repeated self.times times. It still stacks with everything else, since it exposes the same read() interface.
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):
return self.wrapped.read()
class UpperCase(Decorator):
def read(self):
return self.wrapped.read().upper()
class Reverse(Decorator):
def read(self):
return self.wrapped.read()[::-1]
class Repeat(Decorator):
def __init__(self, wrapped, times):
super().__init__(wrapped)
self.times = times
def read(self):
"""Return the wrapped text repeated self.times times."""
# TODO:
# return self.wrapped.read() * self.times
pass
The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.
Decorators can carry their own configuration. A GzipStream might take a compression level; here Repeat takes a times count. Because it needs extra state beyond the wrapped object, it defines its own init that calls the base constructor (to store wrapped) and then saves times.
The init is already written for you. Implement Repeat.read so it returns the wrapped text repeated self.times times. It still stacks with everything else, since it exposes the same read() interface.
Press Run Tests or ββ© to check your solution.