mercredi 29 avril 2020

How to implement a checked builder pattern in Java

I'm trying to implement a checked builder pattern similar to how it's described in this: https://dev.to/schreiber_chris/creating-complex-objects-using-checked-builder-pattern

The result I'm trying to reach is as follows:

Builder builder = new Builder('TestVal')
    .when('this').then(new Set<String> {'val1','val2'})
    .when('that').then(new Set<String> {'val3','val4'});

And the resulting object would contain a collection with any number of whens with the associted thens e.g. a Map like this (the param for when() is unique):

'this' => ['val1','val2'],
'that' => ['val3','val4']

I'm struggling with a couple things:

  1. How to associate the values passed into then() with the value passed into when()
  2. How to require then() be called after when(). (e.g. - .when('this').when('that') //invalid

Aucun commentaire:

Enregistrer un commentaire