A Delegation Framework for Responding to Events
There are four events in the BookSet API that
allow modification: adding, editing, and deleting transactions, and
renaming accounts. For each of these, you should provide a hook
called before the event that gives the user a chance to modify the
data or cancel the action altogether and another hook called after
the event that allows the user to update other variables elsewhere.
Rather than have the user write numerous disconnected functions or
subclass the entire BookSet, you can use a pattern
known as a
delegate
.
A delegate is a helper object a class informs of various events: it
delegates certain responsibilities. The delegation mechanism was a
cornerstone of the almost legendary NeXTStep development environment
and is widely used behind the scenes in
Delphi,
where each component delegates the task of responding to events to
its parent form. A delegate is typically much simpler than the class
it’s supporting. Users will find it far less work to write
their own delegates to achieve a task than to rewrite or subclass the
entire BookSet.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access