Design Patterns in Golang - FACTORY PATTERN

Introduction to Design Patterns

Design patterns are standardized solutions to common problems in software design. They provide a proven template for how to solve a problem in a particular context. One of the creational design patterns that is widely used is the Factory Pattern.

What is the Factory Pattern?

The Factory Pattern is a creational design pattern that provides an interface for creating objects in a super class but allows subclasses to alter the type of objects that will be created. It helps in achieving the abstraction by hiding the creation logic and only exposing an interface for the creation process.

Why Use the Factory Pattern?

  1. Encapsulation of Object Creation: The Factory Pattern encapsulates the instantiation logic, making the code more manageable and reducing the risk of errors.

  2. Decoupling Code: By abstracting the object creation, the code is decoupled from specific classes, making it more flexible and easier to extend.

  3. Single Responsibility Principle: The responsibility of object creation is separated from the object's implementation, adhering to the Single Responsibility Principle.

  4. Ease of Maintenance: Changes to object creation are centralized in the factory, making maintenance easier and reducing the impact on the rest of the codebase.

When to use the factory pattern in your projects

  • Dynamic Object Creation: When objects need to be created dynamically based on user input, configuration, or some other variable.

  • Centralized Creation Logic: When you want to centralize the logic of creating objects to manage it in one place.

  • Complex Initialization: When the creation of objects involves complex setup or initialization that you want to encapsulate.

  • Decoupling: When you want to decouple the creation of objects from their usage, promoting loose coupling and adherence to the dependency inversion principle.

Example of how to use factory pattern in a Real-World Project: Web Application Logger

Let's say you're building a web application and you need different types of loggers: console logger, file logger, and network logger. Each logger might have different configurations and setup processes.

package main

import "fmt"

// Logger is the interface that all types of loggers will implement
type Logger interface {
    Log(message string)
}
// ConsoleLogger is a specific type of logger that logs to the console
type ConsoleLogger struct{}

func (c ConsoleLogger) Log(message string) {
    fmt.Println("ConsoleLogger:", message)
}

// FileLogger is a specific type of logger that logs to a file
type FileLogger struct {
    FilePath string
}

func (f FileLogger) Log(message string) {
    // For simplicity, we just print the message here
    fmt.Printf("FileLogger (%s): %s\n", f.FilePath, message)
}

// NetworkLogger is a specific type of logger that sends logs over the network
type NetworkLogger struct {
    Endpoint string
}

func (n NetworkLogger) Log(message string) {
    // For simplicity, we just print the message here
    fmt.Printf("NetworkLogger (%s): %s\n", n.Endpoint, message)
}

// LoggerFactory is the factory function that creates loggers
func LoggerFactory(loggerType string, config string) Logger {
    switch loggerType {
    case "console":
        return ConsoleLogger{}
    case "file":
        return FileLogger{FilePath: config}
    case "network":
        return NetworkLogger{Endpoint: config}
    default:
        return nil
    }
}

func main() {
    consoleLogger := LoggerFactory("console", "")
    fileLogger := LoggerFactory("file", "/var/log/app.log")
    networkLogger := LoggerFactory("network", "http://logserver.example.com")

    consoleLogger.Log("This is a console log message")
    fileLogger.Log("This is a file log message")
    networkLogger.Log("This is a network log message")
}

Explanation

  1. Logger Interface: All loggers implement the Logger interface with a Log method.

  2. Specific Loggers: We have three loggers (ConsoleLogger, FileLogger, NetworkLogger) that implement the Logger interface.

  3. Factory Function: LoggerFactory takes a loggerType and a config string. Depending on the loggerType, it creates and returns the appropriate logger with the provided configuration.