Composite Pattern and simplificaton

Baz burg.basile at yahoo.com
Thu Jul 4 00:03:38 PDT 2013


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



More information about the Digitalmars-d mailing list