Minimalised Factory Pattern

Govinda Raj
2 min readJun 27, 2020

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.

--

--

Govinda Raj

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