Composite Pattern and simplificaton

Baz burg.basile at yahoo.com
Thu Jul 4 02:54:23 PDT 2013


On Thursday, 4 July 2013 at 09:26:24 UTC, JS wrote:
> On Thursday, 4 July 2013 at 07:03:39 UTC, Baz wrote:
>> On Wednesday, 3 July 2013 at 10:12:34 UTC, JS wrote:
>>> Is there any nifty D features that allow one to simplify the 
>>> composite pattern,
>>>
>>> e.g.,
>>>
>>>   class A : B
>>>   {
>>>       B b;
>>>   }
>>>
>>> Where I would like to have class A's implementation of B be 
>>> use b. This would avoid a lot of boilerplate code if just 
>>> redirecting A's implementation of B to b.
>>>
>>> e.g.,
>>>
>>>  class A : B(use b)
>>>  {
>>>      B b;
>>>  }
>>>
>>>
>>> or maybe more D'ish
>>>
>>>  class A : B
>>>  {
>>>     B b;
>>>     alias b A:B;
>>>  }
>>>
>>> probably some fancy mixin could be used:
>>>
>>>  class A : B
>>>  {
>>>     B b;
>>>     mixin simpleComposite(b);   // Just implements B with 
>>> redirection to b
>>>  }
>>
>> You can also try to overload opCast() in the container class, 
>> a bit in the same fashion that the 'alias this' stuff (except 
>> that alias this will not work with many sub classes). The idea 
>> is exposed here:
>>
>> http://dpaste.dzfl.pl/33d1b2c3
>
> This isn't what I'm really after. At some point I might want to 
> implement the interface partially.
>
> interface A { ... }
> class B : A {
>    A a;
>    // implementation of A goes here, but I'd like it to just 
> redirect to a for all that I do not explicitly implement //
> }
>
> There seems to be some cool stuff that Kenji has used that I 
> was not familiar with. I'm not quite sure if it does exactly 
> what I'm asking but it looks close.

You're right, actually my answer was a bit off-topic since it was 
mostly related to the first 'Dicebot' answer and the 'alias this' 
stuff. BTW it's not perfect since using the opCast doesn't allow 
to 'mirror' several Objects of the same class...


More information about the Digitalmars-d mailing list