polygene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Niclas Hedhman <nic...@hedhman.org>
Subject A Java 8 factory pattern
Date Fri, 21 Oct 2016 08:30:02 GMT
Gang,
while working on something else, an interesting pattern was "discovered",
which I think minimizes the Factoty/Builder stuff quite a bit;

public interface Amount
{
    Property<Long> value();
    Property<Currency> currency();
}


@Structure
ValueBuilderFactory vbf;


Amount a = vbf.create( Amount.class, proto ->
{
    proto.value().set( 100 );
    proto.currency().set( Currency.USD );
    return proto;
} );


And if we need to assign the private mixins;

public interface Amount
{
    interface State
    {
        Property<Long> value();
        Property<Currency> currency();
    }
}

Amount a = vbf.create( Amount.class, (proto, builder) ->
{
    Amount.State state = builder.prototypeFor( Amount.State.class );
    state.value().set( 100 );
    state.currency().set( Currency.USD );
    return proto;
} );

And maybe even a prototype generator, with a Predicate

Stream<Amount> s = vbf.createStream(
    Amount.class,
    proto -> proto.currency().set( Currency.USD),
    (proto, idx) -> idx < 100,
    (proto, idx) -> proto.amount().set(idx)
);

or, perhaps it should (also?) be a Supplier.


A whole new world opened up, but it doesn't "feel" much like Java anymore.

WDYAT?

Cheers
-- 
Niclas Hedhman, Software Developer
http://zest.apache.org - New Energy for Java

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message