Composite Pattern and simplificaton

JS js.mdnq at gmail.com
Thu Jul 4 02:34:32 PDT 2013


On Thursday, 4 July 2013 at 05:57:45 UTC, Kenji Hara wrote:
> 2013/7/4 JS <js.mdnq at gmail.com>
>
>> I'm trying to write a mixin that will solve the problem but 
>> since I can't
>> seem to build up strings progressively it's a huge pain in the 
>> ass.
>>
>
> How about this?
> Unfortunately this code doesn't work with git head, because it 
> requires
> both one small std.typecons.wrap bug fix and its one small 
> improvement. But
> I'll make a PR to fix them soon.
>
> // -------------
> // core side
>
> import std.typecons : Proxy;
> import std.typecons : wrap, unwrap;
> import std.typecons : WhiteHole, NotImplementedError;
> import std.exception : enforce;
>
> public interface Interface
> {
>     int foo();
>     int bar();
> }
>
> private class Pluggable
> {
>     Interface impl;
>     mixin Proxy!impl;
>
>     static Interface defaultImpl;
>     static this() { defaultImpl = new WhiteHole!Interface(); }
>
>     this() { impl = defaultImpl; }
>
>     int foo() { return 1; }     // pre-defined default behavior
> }
>
> public Interface createPluggable()
> {
>     return new Pluggable().wrap!Interface;
> }
> public Interface setPlugin(Interface i, Interface plugin)
> {
>     Pluggable p = enforce(i.unwrap!Pluggable);
>     p.impl = plugin ? plugin : Pluggable.defaultImpl;
>     return i;
> }
>
> // -------------
> // user side
>
> class Plugin : Interface
> {
>     override int foo() { return 10; }
>     override int bar() { return 20; }
> }
>
> void main()
> {
>     import std.exception : assertThrown;
>
>     Interface i = createPluggable();
>
>     assert(i.foo() == 1);
>     assertThrown!NotImplementedError(i.bar());
>
>     i.setPlugin(new Plugin());  // set plug-in
>
>     assert(i.foo() == 1);
>     assert(i.bar() == 20);
>
>     i.setPlugin(null);          // remove plug-in
>
>     assert(i.foo() == 1);
>     assertThrown!NotImplementedError(i.bar());
> }
>
> Kenji Hara

Unfortunately I can't test it until head is updated but it looks 
close to what I'm wanting... not 100% sure though. At the very 
least wrap is something similar to what I was trying to implement 
more or less so I can look in that for details if I need to.








More information about the Digitalmars-d mailing list