Skip to content

Add creational/abstract-factory #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ A curated collection of idiomatic design & application patterns for Go language.

| Pattern | Description | Status |
|:-------:|:----------- |:------:|
| [Abstract Factory](/creational/abstract_factory.md) | Provides an interface for creating families of releated objects | |
| [Abstract Factory](/creational/abstract-factory.md) | Provides an interface for creating families of releated objects | |
| [Builder](/creational/builder.md) | Builds a complex object using simple objects | ✔ |
| [Factory Method](/creational/factory.md) | Defers instantiation of an object to a specialized function for creating instances | ✔ |
| [Object Pool](/creational/object-pool.md) | Instantiates and maintains a group of objects instances of the same type | ✔ |
Expand Down
86 changes: 86 additions & 0 deletions creational/abstract-factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//Source code adapted from https://en.wikipedia.org/wiki/Abstract_factory_pattern
//Added "otherOS" in case this code is executed on other os than windows, mac os x
package main

import (
"fmt"
"runtime"
)

type iButton interface {
paint()
}

type iGUIFactory interface {
createButton() iButton
}

type winFactory struct {
}

func (WF *winFactory) createButton() iButton {
return newWinButton()
}

type osxFactory struct {
}

func (WF *osxFactory) createButton() iButton {
return newOSXButton()
}

type otherOSFactory struct {
}

func (WF *otherOSFactory) createButton() iButton {
return newOtherOSButton()
}

type winButton struct {
}

func (wb *winButton) paint() {
fmt.Println("WinButton")
}

func newWinButton() *winButton {
return &winButton{}
}

type osxButton struct {
}

func (ob *osxButton) paint() {
fmt.Println("OSXButton")
}

func newOSXButton() *osxButton {
return &osxButton{}
}

type otherOSButton struct {
}

func (ob *otherOSButton) paint() {
fmt.Println("OtherOSButton")
}

func newOtherOSButton() *otherOSButton {
return &otherOSButton{}
}

func main() {
var factory iGUIFactory

switch runtime.GOOS {
case "windows":
factory = &winFactory{}
case "darwin":
factory = &osxFactory{}
default:
factory = &otherOSFactory{}
}

button := factory.createButton()
button.paint()
}
7 changes: 7 additions & 0 deletions creational/abstract-factory.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Abstract Factory Pattern

The [abstract factory design pattern](https://en.wikipedia.org/wiki/Abstract_factory_pattern) provides an interface for creating families of releated objects

# Implementation and Example

An example with implementation and usage can be found in [abstract-factory.go](abstract-factory.go).