I like to make use of the builder pattern whenever an object has both mandatory and optional properties. But building objects is usually the Spring framework responsibility, so let’s see how you can employ it using both Java and XML-based Spring configurations.
A Builder example
Let’s start from the following Builder class.
If you’re using Spring Java-based configuration then this is how you’d do it:
The XML-based configuration is more verbose and not as intuitive as the Java-based configuration:
You can make use of the Builder pattern no matter the Spring configuration mode you’ve already chosen. If you have doubts about it’s usefulness, here are three compelling reasons you should be aware of.
|Reference:||The Builder pattern and the Spring framework from our JCG partner Vlad Mihalcea at the Vlad Mihalcea’s Blog blog.|