Factory Method Pattern in Go: Theory, Use Cases, and Implementation
This article explains why design patterns are needed, introduces the Factory Method pattern with its UML roles, provides a step‑by‑step Go implementation—including interfaces, concrete factories, and client code—covers a clothing‑brand example, and discusses the pattern's advantages and drawbacks while also noting its source from a Go design‑patterns book.
Design patterns capture proven solutions from past practice, allowing developers to accelerate development that involves multiple components and to use familiar programming languages for specific solutions.
Using design patterns improves development speed, predictability, and developers' thinking, coding, and design abilities; it standardizes code, makes it more engineering‑oriented, and enhances reusability, readability, reliability, flexibility, and maintainability.
The Factory Method pattern (Factory Method Pattern) defines an interface for creating objects while letting subclasses decide which class to instantiate. Its UML class diagram includes four roles: Factory, ConcreteFactory, Product, and ConcreteProduct.
Implementation in Go follows these steps:
1) Define the factory interface: type Factory interface { FactoryMethod(owner string) Product }
2) Implement the concrete factory class: type ConcreteFactory struct {} func (cf *ConcreteFactory) FactoryMethod(owner string) Product { p := &ConcreteProduct{}; return p }
3) Define the product interface: type Product interface { Use() }
4) Implement the concrete product class: type ConcreteProduct struct {} func (p *ConcreteProduct) Use() { fmt.Println("This is a concrete product") }
5) Create client code that uses the factory to produce and use a product: package main import "github.com/shirdonl/goDesignPattern/chapter2/factory/example" func main() { factory := example.ConcreteFactory{} product := factory.FactoryMethod("shirdon") product.Use() }
A practical example models a clothing‑brand management application. An IClothes interface defines setName , setSize , GetName , and GetSize . Two concrete brands, ANTA and PEAK , embed a clothes struct that implements the interface. The ClothesFactory (implemented as MakeClothes ) creates the appropriate brand based on a string parameter, and the main function demonstrates creating and printing details for both brands.
The pattern’s advantages include modular extensibility, independent testability of factory components, and meaningful method naming; its disadvantages are increased class count leading to higher complexity and added abstraction that can raise the learning curve.
The content is excerpted from the book “Go Language Design Patterns”, which offers concise yet deep coverage of Go design patterns and software architecture, and the book is currently promoted with a limited‑time 50% discount.
FunTester
10k followers, 1k articles | completely useless
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.