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.
While there is no good answer, and it's largely a matter of taste, I do follow the following rule of thumb, with large space for exceptions to avoid over engineering :
Again, these are the rule of thumb I use, in no way mandatory, and often I find myself not respecting them verbatim.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.