Golang Design Patterns – Factory Method. The place to create a variety of drinks from coffee beans

Tram Ho

I. Factory Method – Creational Patterns.

  • The Factory Method design pattern (call it Factory for short) is also a very commonly used design pattern in real projects. The purpose of Factory takes users away from complicated initialization of objects by abstraction that work, and provides what users need such as getting data from a specific source, from web service, database…
  • More specifically, Factory Method creates a higher layer , where focusing on the initialization of objects, we delegate (delegate) the initialization of similar object types to this layer instead of having to processed at that object. Just like making financial investments (stock or cryptocurrency) through AI bots, I don’t have to worry about buying any company’s shares or coins, just let the bots do it all

II. What does Factory Method bring to developers?

Outlining a few things above, I summarize for you the following main ideas that this design pattern brings:

  • Factory takes all the work of object initialization to another place (another object, another package) without leaving it in the place where it is defined.
  • They all communicate through the interface without having to go directly to any specific initialization stage
  • Group objects that are similar in some way (cappuccinos and lattes are both made from coffee beans)

III. Practical examples

Give a specific problem so that we can easily imagine. Now we need to implement a coffee maker, this machine can make cappuccino and latte automatically for customers, each drink will have their name on it. I will name it so easy to remember, call the coffee maker CoffeeBarFactory, Cappuccino and Latte objects are all called CoffeeDrink – the same method is GetName implemented from an interface ICoffeeDrink

Before going into coding, I will briefly talk about a few things to pay attention to when using Factory Method:

  • All drinks (Cappuccino, Latte,…) collectively called CoffeeDrink need to implement ICoffeeDrink interface, here specifically GetName
  • We delegate all instantiation of the drink object to CoffeeBarFactory
  • Allowed to add many other drinks from coffee through CoffeeBarFactory

IV. Implementation

  • First define the CoffeeDrink struct and the ICoffeeDrink interface. Struct CoffeeDrink will implement the method defined in ICoffeeDrink

  • Next we will define Cappuccino and Latte objects, along with their creation function, note that these functions are private, hiding their initialization from the outside.

  • Finally, CoffeeBarFactory, here for the simplest, I just define a function called GetCoffeeDrink, of course this is the public accessor function:

  • That’s enough, let’s run the sample program:

  • Result:


V. Conclusion

Through the example above, you have clearly understood the function of the design pattern Factory Method, which is to group objects of the same type and put the initialization (outside of scope). Besides, adding many types of CoffeeDrink will not be too complicated anymore.

Well, the above is just an example, there is no bot to trade for you, like crypto, only CZ is that AI bot (just kidding)

Thank you for viewing the article.

VI. References

  • Go Design Patterns (Mario Castro Contreras)
Share the news now

Source : Viblo