Forwarding (object-oriented programming) explained

In object-oriented programming, forwarding means that using a member of an object (either a property or a method) results in actually using the corresponding member of a different object: the use is forwarded to another object. Forwarding is used in a number of design patterns, where some members are forwarded to another object, while others are handled by the directly used object. The forwarding object is frequently called a wrapper object, and explicit forwarding members are called wrapper functions.

Delegation

Forwarding is often confused with delegation; formally, they are complementary concepts. In both cases, there are two objects, and the first (sending, wrapper) object uses the second (receiving, wrappee) object, for example to call a method. They differ in what [[self (computer science)|self]] refers to on the receiving object (formally, in the evaluation environment of the method on the receiving object): in delegation it refers to the sending object, while in forwarding it refers to the receiving object. Note that self is often used implicitly as part of dynamic dispatch (method resolution: which function a method name refers to).

For example, given the following code:// Sendervoid n

// Receivervoid m

void n Under delegation, m will output m2, n1 because n is evaluated in the context of the original (sending) object, while under forwarding, it will output m2, n2 because n is evaluated in the context of the receiving object.

In casual use, forwarding is often referred to as "delegation", or considered a form of delegation, but in careful usage they are clearly distinguished by what self refers to. While delegation is analogous to inheritance, allowing behavioral reuse (and concretely code reuse) without changing evaluation context, forwarding is analogous to composition, as execution depends only on the receiving (member) object, not the (original) sending object. In both cases, reuse is dynamic, meaning determined at run time (based on the object to which use is delegated or forwarded), rather than static, meaning determined at compile/link time (based on the class which is inherited from). Like inheritance, delegation allows the sending object to modify the original behavior, but is susceptible to problems analogous to the fragile base class; while forwarding provides stronger encapsulation and avoids these problems; see composition over inheritance.

Examples

A simple example of explicit forwarding in Java: an instance of B forwards calls to the foo method of its a field:

class B Note that when executing a.foo, the this object is a (a subtype of A), not the original object (an instance of B). Further, a need not be an instance of A: it may be an instance of a subtype. Indeed, A need not even be a class: it may be an interface/protocol.

Contrast with inheritance, in which foo is defined in a superclass A (which must be a class, not an interface), and when called on an instance of a subclass B, it uses the code defined in A, but the this object is still an instance of B:class A

class B extends A

In this Python example, class B forwards the foo method and the x property to the object in its a field: using these on b (an instance of B) is the same as using them on b.a (the instance of A to which these are forwarded).

class A: def __init__(self, x) -> None: self.x = x

def foo(self): print(self.x)

class B: def __init__(self, a) -> None: self.a = a

def foo(self): self.a.foo

@property def x(self): return self.a.x

@x.setter def x(self, x): self.a.x = x

@x.deleter def x(self): del self.a.x

a = A(42)b = B(a)b.foo # Prints '42'.b.x # Has value '42'b.x = 17 # b.a.x now has value 17del b.x # Deletes b.a.x.

Simple

In this Java example, the class has a method. This print method, rather than performing the print itself, forwards to an object of class . To the outside world it appears that the object is doing the print, but the object is the one actually doing the work.

Forwarding is simply passing a duty off to someone/something else. Here is a simple example:

class RealPrinter

class Printer public class Main

Complex

The more complex case is a Decorator Pattern that by using interfaces, forwarding can be made more flexible and typesafe. "Flexibility" here means that need not refer to or in any way, as the switching of forwarding is abstracted from . In this example, class can forward to any class that implements an interface . Class has a method to switch to another forwarder. Including the clauses improves type safety, because each class must implement the methods in the interface. The main tradeoff is more code.

interface I class A implements I class B implements I // changing the implementing object in run-time (normally done in compile time)class C implements I public class Main

Applications

Forwarding is used in many design patterns.[1] Forwarding is used directly in several patterns:

Forwarding may be used in other patterns, but often use is modified; for example, a method call on one object results in several different methods being called on another:

Notes and References

  1. Book: Gamma , Erich . Erich Gamma

    . Erich Gamma . Richard Helm . Richard . Helm . Ralph Johnson (computer scientist) . Ralph . Johnson . John Vlissides . John . Vlissides . 1995 . Design Patterns: Elements of Reusable Object-Oriented Software . . 978-0-201-63361-0. Design Patterns (book) . 1995dper.book.....G .