All PatternsCreational
Factory Method
Defines an interface for creating objects, letting subclasses decide which class to instantiate.
Plugin systemsUI component creationDocument generators
Understanding Factory Method
The Factory Method pattern defines an interface for creating objects but lets subclasses or factory functions decide which class to instantiate. In Go, we typically implement this using factory functions that return interface types, enabling loose coupling and easy extensibility.
How It Works
Client
Factory
CreateProduct()
ProductA
ProductB
Create()
Product
1
Client Request
Client asks the factory to create a product without knowing the concrete type.
1 / 4
Basic Implementation
A simple factory function that creates different document types:
main.go
Loading editor...
Real-World Example: Logger Factory
A configurable logger factory that creates different logging backends:
main.go
Loading editor...