polygene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Niclas Hedhman <nic...@hedhman.org>
Subject Re: A Java 8 factory pattern
Date Sat, 22 Oct 2016 13:05:23 GMT
I am knee-deep in replacing "String identity" with "Identity identity", but
will probably do some work on this tomorrow to see if it "feels" good or
not.

Another immediate candidate is then UnitOfWork itself... which is less
needy since we already have UnitOfWorkPropagation taking care of it most of
the time.

On Sat, Oct 22, 2016 at 8:53 PM, Paul Merlin <paul@nosphere.org> wrote:

> 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 :)
>
>
>


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

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