Jay Taylor's notesback to listing index
In OOP, what is forwarding and how is it different from delegation?[web search]
We started with Q&A. Technical documentation is next, and we need your help.
Whether you're a beginner or an experienced developer, you can contribute.
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 sure I really understand.
Forwarding is sort of like "inheritance via containment", or "implementation inheritance the hard way".
Typical implementation inheritance:
Now, an instance of Derived has a baseFn() method. This is a way of sharing implementation between different classes.
Forwarding looks like this:
You could have also implemented that with private inheritance.
Delegation is a special case of forwarding, where at the "thing to forward" to is an interface itself.
Now, you can swap out the implementation of delegate at runtime, whereas in forwarding you cannot (and you may not want to, which is why you would do it).
If that answers the wrong question, let me know in a comment and I'll remove the answer.
Let's first define two terms:
The difference between forwarding and delegation is that in forwarding
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.
They're similar ideas in that one object relies on another for help. Here's how I think of the two ideas given my strong Objective-C bias:
delegation: A decision needs to be made, but I don't want to make it. I'll let my delegate handle that.
In Cocoa, for example, NSTableView uses a delegate to customize the behavior of the table. Delegation provides a way to customize one object by letting another object, the delegate, provide the customization. Continuing with the example, a table view's delegate implements an NSTableViewDelegate interface that the table uses to talk to its delegate.
forwarding: Someone just sent me a message that I don't understand, but I know of another object that might implement it. I'll pass the invocation of that message on to that object.
In Cocoa, again, any class can implement the -forwardInvocation: method. If a message is sent to an object that doesn't implement it, that object's -forwardInvocation: method is called, and the object can decide to pass the invocation on to another object. That object could be its delegate, or it could be some system-wide error handler, or whatever. NSProxy uses this to appear to implement all methods -- it just passes the invocation on to its master object.
Note that with forwarding, there's not a defined delegate interface; the message is just passed on to another object. Another place you see what I'd call forwarding is when one object contains another object that it uses to implement some interface. Any messages to that interface are just forwarded to the contained object, which does all the work.
Not the answer you're looking for? Browse other questions tagged c++ oop class delegation forwarding or ask your own question.
4 years ago
Hot Network Questions
- Additional Turns and Emrakul, the Promised End
- What is the word for a property no more necessary but still present in a object?
- SELECT * FROM tablename WHERE 1
- How common are self-funded PhDs in the STEM fields in U.S. universities?
- How many pokemon did Ash catch?
- Does it make sense for a cake icing recipe to call for vinegar but not baking soda?
- Could a candle theoretically melt iron?
- Credit Card Testing (Paypal Payflow API)
- Word or phrase to describe slacking because you think you already know everything
- Serpentine resistor - Number of bends effect
- Adult talking in Peanuts
- find + how delete the files on specific date
- Why won't the Import plugin accept this JSON string?
- What is QuadRooter that 900 million of Android devices are vulnerable for?
- Can you outgolf me? (Cops section)
- Finding a short story called "Shards"
- Earth overshoot day: is it a sound concept?
- CheckRecursion class not working
- What is the meaning for a Dark Blue Pokestop?
- Where did the phrase "Gotta catch 'em all" come from?
- What does "Drawing over other apps" mean?
- Formal Definition of Pushdown Automata
- Is there a guide for breast feeding etiquette around the world?
- Convert xxd output to shellcode
|Technology||Life / Arts||Culture / Recreation||Science||Other|