Factory Pattern You Should Know in Golang(Design Patterns 02)

Go Factory Patterns: Simple, Method, and Abstract - Which One to Choose?

Contact Author@:Medium,LinkedIn,Twitter
golang-factory-pattern(from github.com/MariaLetta/free-gophers-pack)|300

Hello, here is Wesley, Today’s article is about factory pattern in Go. Without further ado, let’s get started.💪

The Factory Pattern is a common creational design pattern used to dynamically create objects and encapsulate object creation details.

Core Concepts of Factory Pattern

  1. Intent: Provide an interface for creating objects, encapsulating the concrete logic of instantiation.
  2. Classification:
    • Simple Factory: Create objects through a single function.
    • Factory Method: Define an interface, allowing subclasses to decide which class to instantiate.
    • Abstract Factory: Provide an interface for creating a series of related or dependent objects.
  3. Applicability:
    • The client is unaware of the specific object construction process.
    • Object creation logic is complex and needs centralized management.

Implementation of Factory Pattern

1. Simple Factory

The simple factory pattern creates objects through a single factory function.

Example: Creating Different Shapes

Advantages:

  • Simplifies object creation, allowing clients to focus on the factory method.

Disadvantages:

  • If new types are added, the factory function code needs to be modified, violating the open-closed principle.

2. Factory Method Pattern

The factory method pattern defines a factory interface for each product, achieving open-closed creation of products.

Example: Creating Shapes through the Factory Method

Advantages:

  • Satisfies the open-closed principle, allowing new types to be added by creating new factory classes without modifying existing code.
  • The factory interface makes the code more extensible.

Disadvantages:

  • Each product requires a corresponding factory class, which may lead to an increase in the number of classes.

3. Abstract Factory Pattern

The abstract factory pattern is used to create a group of related or dependent objects, commonly used in complex systems.

Example: Creating different styles of buttons and text boxes

Result:

1
2
3
4
Rendering Mac Button
Displaying Mac TextBox
Rendering Windows Button
Displaying Windows TextBox

Advantages:

  • Suitable for scenarios where a group of related objects needs to be created.
  • Unified management of product families, making it easier to extend.

Disadvantages:

  • The structure is complex, and modifying the factory interface when adding new product families.

Summary

  • Simple Factory Pattern: Suitable for small projects, but has limited extensibility.
  • Factory Method Pattern: Satisfies the open-closed principle, with each product type having its own independent factory class.
  • Abstract Factory Pattern: Suitable for scenarios where a group of related products needs to be created, supporting product family extension.

In Go, you can choose an appropriate factory pattern based on your actual requirements and design flexible and efficient code by combining interfaces and structs.


More Series Articles about You Should Know In Golang:

https://wesley-wei.medium.com/list/you-should-know-in-golang-e9491363cd9a

And I’m Wesley, delighted to share knowledge from the world of programming. 

Don’t forget to follow me for more informative content, or feel free to share this with others who may also find it beneficial. it would be a great help to me.

Give me some free applauds, highlights, or replies, and I’ll pay attention to those reactions, which will determine whether or not I continue to post this type of article.

See you in the next article. 👋

中文文章: https://programmerscareer.com/zh-cn/golang-factory-pattern/
Author: Medium,LinkedIn,Twitter
Note: Originally written at https://programmerscareer.com/golang-factory-pattern/ at 2024-11-21 10:28.
Copyright: BY-NC-ND 3.0

Singleton Pattern You Should Know in Golang(Design Patterns 03) Builder Pattern You Should Know in Golang(Design Patterns 01)

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×