委譲
時折、コンポジションと転送の組み合わせがおおざっぱに委譲(delegation)と呼ばれることがあります。技術的には、ラッパーオブジェクトがラップしているオブジェクトへ自分自身を渡さない限り、委譲ではありません。
(『Effective Java』第3版、p.93)
参考
プログラマーを惑わせる3種類の委譲(委譲・Delegation/転送・Forwarding/.NET Delegates) - Qiita
https://ja.wikipedia.org/wiki/委譲
hr.icon
Kotlinでは言語仕様の中にdelegationがあるんですが、個人的に結構きもいというか、うーん、という感じがある。koma.icon
code:kotlin
interface Base {
fun print()
}
class BaseImpl(val x: Int) : Base {
override fun print() { print(x) }
}
class Derived(b: Base) : Base by b
fun main() {
val b = BaseImpl(10)
Derived(b).print()
}
https://kotlinlang.org/docs/delegation.html#overriding-a-member-of-an-interface-implemented-by-delegation
Derivedの公開APIとしてBaseの公開APIがまるまる公開されるわけです。delegated propertiesというのもあるのですが、いずれにせよ、委譲先の公開APIを委譲元の公開APIとするという機能なんですよね。これが欲しくなる場面がないわけではないとは思うのですが、委譲ってそんなんだっけ・・・という気持ちになります。koma.icon