Relationships and dispatch
What is delegation?
On this page
Delegation forwards responsibility for an operation to a collaborator. An audited store can log a request and delegate the read to its wrapped store. Decide whether failures are logged before exposing the wrapper. Callers otherwise see the same method with a surprising difference in error behaviour.
Relationship diagram
Worked example
Pythonclass AuditedStore:
def __init__(self, store):
self.store = store
self.reads = []
def read(self, key):
self.reads.append(key)
return self.store[key]
store = AuditedStore({"version": 4})
assert store.read("version") == 4
assert store.reads == ["version"]Explore class relationships in the OOP Playground