In OOP, what is forwarding and how is it different from delegation?

后端 未结 3 700
终归单人心
终归单人心 2021-02-02 11:21

Would someone please explain the difference between forwarding and delegation? They seem similar, but I haven\'t been able to find a good definition of forwarding, so I\'m not s

3条回答
  •  借酒劲吻你
    2021-02-02 11:38

    Let's first define two terms:

    • sender : the object that sends a message/task to another object(the receiver)
    • receiver: the object that receives a message/task from the sender

    The difference between forwarding and delegation is that in forwarding the receiver acts in its own context whereas in delegation the receiver acts on the behalf of the sender.

    Here is a great metaphor from this blog post:

    Delegation and forwarding are both very similar. One metaphor that might help distinguish them is to think of receiving an email asking you to donate some money to a worthy charity.

    • If you forward the email to a friend, and the friend donates money, the friend is donating their own money and getting their own tax receipt.
    • If you delegate responding to your accountant, the accountant donates your money to the charity and you receive the tax receipt.

提交回复
热议问题