Delegation
“delegation ”即“委托、授权”。
wikipedia Delegation (computing)
NOTE: 这篇对delegation的总结是比较全面的
Delegation (object-oriented programming), evaluating a member of one object (the receiver) in the context of another, original object (the sender).
- Delegation pattern, a design pattern implementing this feature.
- Forwarding (object-oriented programming)
- Object aggregation
wikipedia Delegation (object-oriented programming)
In object-oriented programming, delegation refers to evaluating a member (property or method) of one object (the receiver) in the context of another original object (the sender). Delegation can be done explicitly, by passing the sending object to the receiving object, which can be done in any object-oriented language; or implicitly, by the member lookup rules of the language, which requires language support for the feature. Implicit delegation is the fundamental method for behavior reuse in prototype-based programming, corresponding to inheritance in class-based programming. The best-known languages that support delegation at the language level are Self, which incorporates(合并) the notion of delegation through its notion of mutable parent slots that are used upon method lookup on self calls, and JavaScript; see JavaScript delegation.
NOTE: 上面是使用类比方法来描述delegation的:
prototype-based programming class-based programming behavior reuse delegation inheritance sender、receiver parent、child 在class-based programming中,可以通过delegation pattern](https://en.wikipedia.org/wiki/Delegation_pattern) 来实现delegation;
The term delegation is also used loosely for various other relationships between objects; see delegation (programming) for more. Frequently confused concepts are simply using another object, more precisely referred to as consultation or aggregation; and evaluating a member on one object by evaluating the corresponding member on another object, notably in the context of the receiving object, which is more precisely referred to as forwarding (when a wrapper object doesn't pass itself to the wrapped object).[1][2][a] The delegation pattern is a software design pattern for implementing delegation, though this term is also used loosely for consultation or forwarding.
NOTE: 关于delegation pattern,参见
Theory\Design-pattern\OOP-design-pattern\Structural-pattern\Delegation-pattern
。
Delegation VS inheritance
这其实就是composition VS inheritance,需要遵循"composition over inheritance";
Example
1、strategy pattern,参见 Theory\Programming-paradigm\Object-oriented-programming\Design-pattern\Behavioral-pattern\Strategy-pattern
2、composition over inheritance,参见 Theory\Programming-paradigm\Object-oriented-programming\Design-pattern\Principle\Composition-over-inheritance
3、Dependency injection就是典型的使用delegation的
C++ delegating constructor
C++11引入了delegating constructor特性,参见cppreference Constructors and member initializer lists。
TODO
geeksforgeeks Delegation vs Inheritance in Java
stackoverflow What is a C++ delegate?
wikipedia Delegation pattern