In Go, the Factory Method pattern provides a way to encapsulate object creation so that the caller does not need to know the specifics of how objects are constructed. Instead of hardcoding which struct to instantiate, we define a factory function that returns the appropriate struct based on some input.
Key Components in Go
Interface: Defines the methods that any struct should implement to be considered a "product" of the factory. This provides a common behavior for any struct type that the factory can return.
Concrete Types (Structs): These are specific implementations of the interface. They contain the actual functionality that will be used but are abstracted away by the interface.
Factory Function: A function that returns the interface type. It chooses which struct to return based on input parameters. This allows the calling code to interact with different structs uniformly via the interface.
Example Scenario: Notification System
Let’s imagine a notification system where we might want to send different types of notifications, such as SMS or Email. Instead of directly creating instances of each notification type, we use a factory function to decide which type to return. This is beneficial when there might be more types of notifications in the future, like push notifications.
Step 1: Define the Interface
In Go, we create an interface named Notification
, which declares the Send
method. Any struct implementing Send
will fulfill this interface.
package main
import "fmt"
// Notification interface defines behavior that all notifications should have.
type Notification interface {
Send() string
}
Step 2: Create Concrete Structs
Next, we define specific structs for each type of notification: SMS
and Email
. Each struct implements the Send
method required by the Notification
interface.
// SMS is a concrete struct that represents SMS notifications.
type SMS struct{}
// Send method for SMS struct, satisfying the Notification interface.
func (s SMS) Send() string {
return "Sending SMS Notification"
}
// Email is another concrete struct representing email notifications.
type Email struct{}
// Send method for Email struct, also satisfying the Notification interface.
func (e Email) Send() string {
return "Sending Email Notification"
}
Step 3: Create the Factory Function
The factory function NotificationFactory
accepts a notificationType
string parameter and returns a Notification
interface. It decides which struct to return based on the input type. This way, the client code doesn’t need to know about the specific structs; it only interacts with the Notification
interface.
// NotificationFactory is the Factory Method function.
// It returns a Notification based on the input type.
func NotificationFactory(notificationType string) Notification {
switch notificationType {
case "sms":
return SMS{}
case "email":
return Email{}
default:
return nil
}
}
Step 4: Using the Factory Method
In the main
function, we use the NotificationFactory
to create different types of notifications. The client code doesn’t need to know about SMS
or Email
structs; it only interacts with the Notification
interface, making it easy to extend with new types if needed.
func main() {
// Request an SMS notification
notification := NotificationFactory("sms")
if notification != nil {
fmt.Println(notification.Send()) // Output: Sending SMS Notification
}
// Request an Email notification
notification = NotificationFactory("email")
if notification != nil {
fmt.Println(notification.Send()) // Output: Sending Email Notification
}
}
Explanation of the Factory Method Pattern in Go Terms
Interface (
Notification
): TheNotification
interface defines the behavior (theSend
method) expected from any type of notification.Concrete Structs (
SMS
andEmail
): These structs are specific implementations of theNotification
interface. Each struct provides its version of theSend
method.Factory Function (
NotificationFactory
): This function takes in a parameter to decide which notification type to create and returns aNotification
interface. The calling code uses the interface rather than directly interacting withSMS
orEmail
, allowing the code to be flexible and easily extendable.
Why Use This Pattern in Go?
-
Flexibility: The calling code interacts only with the
Notification
interface, making it easy to add new notification types without changing the client code. - Encapsulation: The factory function hides the details of which struct is created based on the input, promoting clean code.
-
Extensibility: Adding a new notification type (e.g.,
PushNotification
) only requires implementing theNotification
interface and adding a case in the factory function.
Extending the Factory Method Pattern in Go
Suppose we want to add a new notification type, such as PushNotification
. We create a new struct and implement the Notification
interface, then modify the factory function to recognize the new type.
Adding a New Notification Type: PushNotification
// PushNotification is another concrete struct representing push notifications.
type PushNotification struct{}
// Send method for PushNotification struct, also satisfying the Notification interface.
func (p PushNotification) Send() string {
return "Sending Push Notification"
}
Updating the Factory Function to Handle the New Type
We modify the NotificationFactory
to handle PushNotification
by adding a case for "push"
.
func NotificationFactory(notificationType string) Notification {
switch notificationType {
case "sms":
return SMS{}
case "email":
return Email{}
case "push":
return PushNotification{}
default:
return nil
}
}
Using the Extended Factory
With the new type added, the client code remains the same but can now request a "push"
notification as well.
func main() {
// Request a Push notification
notification := NotificationFactory("push")
if notification != nil {
fmt.Println(notification.Send()) // Output: Sending Push Notification
}
}
Summary
The Factory Method pattern in Go:
-
Defines an interface (
Notification
) that different structs (SMS
,Email
,PushNotification
) implement. -
Provides a factory function (
NotificationFactory
) that determines which struct to instantiate based on input. -
Promotes loose coupling by ensuring the client only interacts with the
Notification
interface, not the specific structs.
This pattern is an effective way to manage object creation in Go when you need to decouple client code from specific struct implementations, promote scalability, and cleanly manage different types of related objects.