An expensive RealService already exists. Every call to its fetch counts against a call counter. Think of a network round-trip or a slow database read. We are not allowed to change it, but we want to wrap it.
The Proxy pattern puts a stand-in object in front of the real one. The stand-in exposes the same interface, so callers written against the service work unchanged against the proxy. The simplest form, a virtual proxy: just forwards every call to the real object.
You are given RealService. Build a Proxy that holds a RealService instance and whose fetch simply delegates to it.
class RealService:
def __init__(self, backend):
self.backend = dict(backend)
self.calls = 0
def fetch(self, key):
self.calls += 1
return self.backend.get(key)
class Proxy:
def __init__(self, real):
self.real = real
def fetch(self, key):
"""Delegate to the wrapped real service and return its result."""
# TODO: return self.real.fetch(key)
pass
The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.
An expensive RealService already exists. Every call to its fetch counts against a call counter. Think of a network round-trip or a slow database read. We are not allowed to change it, but we want to wrap it.
The Proxy pattern puts a stand-in object in front of the real one. The stand-in exposes the same interface, so callers written against the service work unchanged against the proxy. The simplest form, a virtual proxy: just forwards every call to the real object.
You are given RealService. Build a Proxy that holds a RealService instance and whose fetch simply delegates to it.
Press Run Tests or ββ© to check your solution.