4 ms·
I bet the alternatives that were considered were: class Parent { void a() {...} void b() {...} void c() {...} void d() {...} .
by ambition 19y ago
I bet the alternatives that were considered were:
class Parent
{
void a() {...}
void b() {...}
void c() {...}
void d() {...}
...
void zzz() {...}
}
class B
{
Parent m_a;
void a() {m_a.a()}
void b() {m_a.b()}
void c() {m_a.c()}
void d() { specialized logic }
...
void zzz() {m_a.zzz()}
}
vs.
class B : Parent
{
void d() { specialized logic }
}
- Retric 19y agoYour first example works fine until void a() Parent.a() calls Parent.d() and you need the code to call B.d(). An interface also works but you end up cutting and pasting a lot of the same code between the two classes.
- 0x44 19y agoI see. His example is meaningful only in a language that does not provide a convention for handling missing methods in the composed class. For instance, in ruby, I can distill your example to: class Parent def a; ... end def b; ... end def c; ... end ... def zzzz; ... end end class B def initialize @parent = Parent.new end def b; special logic end def method_missing(methodname) @parent.send(methodname) end end