Skip to main content

Command Palette

Search for a command to run...

Golang - Factory Method Pattern

Updated
3 min read
Golang - Factory Method Pattern
M

Senior Freelancer & Technical Lead

Working as a Golang developer since 2020. Working as a mobile developer since 2013.

Focussed on architecture, testability and clean code. Open minded & product driven. Based in Rhede, available world-wide

The Factory Method pattern is one of the most commonly used creational patterns in software development. It is part of the famous "Gang of Four" design patterns mentioned in their book "Design Patterns: Elements of Reusable Object-Oriented Software". In this article, we will discuss how to implement the Factory Method pattern in Golang.

Understanding the Factory Method Pattern

The Factory Method pattern is a creational pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created. In simpler terms, it is a way of creating objects without specifying the exact class of object that will be created.

The Factory Method pattern is useful when we want to create objects without knowing their exact class. Instead of creating objects directly, we delegate the creation to a factory that can decide which class of object to create based on certain conditions. This makes our code more flexible and easier to maintain.

Implementing Factory Method Pattern in Golang

In Golang, we can implement the Factory Method pattern using an interface and a struct. The interface will define a method that will be implemented by the struct to create objects. Here is an example:

type Product interface {
    GetName() string
}

type Factory interface {
    CreateProduct() Product
}

type ConcreteProduct struct {
    name string
}

func (p *ConcreteProduct) GetName() string {
    return p.name
}

type ConcreteFactory struct {}

func (f *ConcreteFactory) CreateProduct() Product {
    return &ConcreteProduct{name: "Concrete Product"}
}

In the code above, we define the Product interface which will be implemented by the ConcreteProduct struct. We also define the Factory interface which will be implemented by the ConcreteFactory struct to create objects.

The CreateProduct() method in ConcreteFactory creates an instance of ConcreteProduct and returns it as a Product interface. This way, we can create objects without knowing the exact class of the object that will be created.

Example Usage

Here is an example of how to use the Factory Method pattern in Golang:

func main() {
    factory := &ConcreteFactory{}
    product := factory.CreateProduct()
    fmt.Println(product.GetName()) // Output: Concrete Product
}

In the code above, we create an instance of ConcreteFactory and call its CreateProduct() method to create a Product instance. We then call the GetName() method on the product instance to get its name.

Advantages of Using the Factory Method Pattern

The Factory Method pattern provides several advantages over other creational patterns. These include:

  • Flexibility: The Factory Method pattern allows us to create objects without knowing their exact class. This makes our code more flexible and easier to maintain.

  • Scalability: The Factory Method pattern can be easily scaled to support new types of objects. We can simply create a new subclass of the factory and implement the CreateProduct() method to create the new type of object.

  • Decoupling: The Factory Method pattern decouples the creation of objects from their usage. This makes our code more modular and easier to test.

Conclusion

The Factory Method pattern is a useful pattern for creating objects without knowing their exact class. In Golang, we can implement this pattern using an interface and a struct. By using this pattern, we can write better and more flexible code. So, if you want to improve the scalability, maintainability and flexibility of your code, consider using the Factory Method pattern.


References

Golang Patterns

Part 16 of 19

Welcome to the Golang Patterns tutorial series! In this series, we will explore the various software patterns that can be applied in Golang to create flexible, scalable, and maintainable software.

Up next

Golang - Options vs Builder Pattern

When it comes to creating complex objects in Golang, two patterns are commonly used: the Options pattern and the Builder pattern. Both patterns have their advantages and disadvantages, and choosing the right pattern depends on the specific needs of y...