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.

http://localhost:8080/pattern/simple

http://localhost:8080/pattern/bad

http://localhost:8080/pattern/good

That’s all. See, how simple it is.

Let me know if you find this helpful.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Govinda Raj

Govinda Raj

Senior Software Developer. Tech Enthusiast and love coding. My portfolio: https://govinda-raj.github.io/