🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)
🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)

🔧 Programmierung 🕛 kürzlich 5 Min Lesezeit
0

Factory Design Pattern

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

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




  1. 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.


  2. 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.


  3. 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.




CODE
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.




CODE
// 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.




CODE
// 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.




CODE
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




  1. Interface (Notification): The Notification interface defines the behavior (the Send method) expected from any type of notification.


  2. Concrete Structs (SMS and Email): These structs are specific implementations of the Notification interface. Each struct provides its version of the Send method.


  3. Factory Function (NotificationFactory): This function takes in a parameter to decide which notification type to create and returns a Notification interface. The calling code uses the interface rather than directly interacting with SMS or Email, 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 the Notification 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






CODE
// 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".




CODE
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.




CODE
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.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
GPT-6 Astra Release Today? OpenAI’s Next Major AI Model Is Almost Here
1 Quelle
Apple accuses OpenAI of destroying evidence as trade-secrets fight intensifies
1 Quelle
Major AI platforms go down in unprecedented simultaneous outage
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Factory Design Pattern

Thematisch verwandte Begriffe: Factory, Design, Pattern · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...