polygene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Merlin <p...@nosphere.org>
Subject Re: A Java 8 factory pattern
Date Sat, 22 Oct 2016 12:53:12 GMT
Ahh lambdas :-)

Niclas Hedhman a écrit :
> 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;
> } );

I kinda like it. But it's not that different than:

ValueBuilder<Amount> builder = vbf.newValueBuilder( Amount.class );
Amount.State state = builder.prototypeFor( Amount.State.class );
state.value().set( 100 );
state.currency().set( Currency.USD );
Amount a = builder.newInstance();


> 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.

Now that could be interesting :)

What about a supplier for the sequence?

Stream<Amount> s = vbf.createStream(
    Amount.class,
    proto -> proto.currency().set( Currency.USD ),
    () -> IntStream.range(23, 42).filter( idx -> idx & 1 == 0 ),
    (proto, idx) -> proto.amount().set(idx)
);

Could also be used with infinite streams:

Stream<Amount> s = vbf.createStream(
    Amount.class,
    proto -> proto.currency().set( Currency.USB ),
    () -> IntStream.generate( ..something infinite.. ),
    (proto, step) -> proto.amount().set(step.getAmount())
).limit(100);

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

Yep :)



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