🧰 Developer’s toolbox:👩‍💼 👉 👷🔨 Delegation Design Pattern

Vee Lesyk
Dots and Spaces
Published in
1 min readSep 6, 2019

Delegation is a design pattern that enables a class to hand off (or “delegate”) some of its responsibilities to an instance of another class.

Wikipedia says:

In delegation, an object handles a request by delegating to a second object (the delegate). The delegate is a helper object, but with the original context. With language-level support for delegation, this is done implicitly by having self in the delegate refer to the original (sending) object, not the delegate (receiving object). In the delegate pattern, this is instead accomplished by explicitly passing the original object to the delegate, as an argument to a method. Note that “delegation” is often used loosely to refer to the distinct concept of forwarding, where the sending object simply uses the corresponding member on the receiving object, evaluated in the context of the receiving object, not the original object.

Github repo: link.

Example

Cookie shop should sell cookies, where Bakery should bake cookies.

Swift

Delegation.swift

TypeScript

Delegation.ts

Output

Delegation Output.
Delegation.

P.S. We would be happy to see comments according to mistakes & typos.

--

--

Vee Lesyk
Dots and Spaces

#h+ #livemoredomore Adventurer. Unique experience wizard. Maker of things. Convergence commander. Information warlock. Problem solver. System: ☉; Planet: ♁.