🧰 Developer’s toolbox:👩💼 👉 👷🔨 Delegation Design Pattern
Delegation is a design pattern that enables a class to hand off (or “delegate”) some of its responsibilities to an instance of another class.
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
TypeScript
Output
P.S. We would be happy to see comments according to mistakes & typos.