Factory Design Pattern in GoLang
Should I make the object in my own or let some factory to make it?
Programs must be expandable and they eventually expands hence you need to write codes which can be expand without much effort.
Factory design pattern is a creational design pattern to predefined blueprint of a solution for a creational problem. First let's dive in to the problem that we are going to solve using factory design pattern.
Let's take a simple example and illustrate the problem. There you asked to make a program to manage shoe making and you make a program as follows.
Then cool you can make
Shoes and sell them with discount. Then you asked to expand the application and to make slippers depending on the choice. How do you do that? Shall we do that as follows.
But there might be multiple footwear making process is running here and there and you need to update that every time when new component is added.
Solution is simple where you need to separate the footwear making logic into another place which we call a factory. The factory create the corresponding object depend on the input given and you don't need to worry about the update the code here and there when new component or a product is added. It's just update the factory method.
Factory method example.
Okay what if I need to create a
Sandals . Then you have to create a
Sandals struct in go and add it to factory method which is as follows.
Hope this will be helpful.
If you have found this helpful please hit that 👏 and share it on social media :).