Often I find the need to engineer objects with configurable functionality.
To exemplify, assume I'm creating a DateIterator
. The configurable option(s) might be whether to iterate the closed interval [start, end]
or the open-end interval [start, end)
.
new DateIterator(boolean openInterval);
new DateIterator(Interval.OPEN_END);
new DateIterator().openEnd();
new OpenEndedDateIterator();
To this comes a few alternatives which I consider inferior, like integer-based configuration new DateIterator(Interval.OPEN_END);
or property based configuration.
Are there any other approaches? Which approach you do you prefer?
I'd say the Builder pattern makes sense here:
DateIterator di =
DateIterator.builder()
.withStartDate(new Date())
.withOpenEnd()
.build();
That way your actual DateIterator can be immutable, while the builder returned by DateIterator.builder()
does the configuration work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With