Minimalised Factory Pattern
Recently, I discovered a way to use a Factory pattern without if/else, switch-case, reflection, and map. If you are using Spring Boot, the BeanFactory really going to help you a lot.
Let’s say we have one use case, where you need to pick the service on runtime depending on the client request.
Let’s create a Spring Boot Application from https://start.spring.io/ using Spring Web dependency.
So the idea is; We can use spring’s Bean Factory which stores all beans of the application. We don’t need to add the code block to get the bean on runtime. To do so we need to ask BeanFactory to please keep my bean name what I have mentioned.
Let’s test the application now. Let’s run the application on 8080 port.
Hit the following link on the browser.
That’s all. See, how simple it is.
Let me know if you find this helpful.