1
0
mirror of https://github.com/tmrts/go-patterns.git synced 2024-11-25 06:26:06 +03:00

creational/abstract-factory: implement abstract pattern

This commit is contained in:
Inux 2016-09-13 19:45:23 +02:00
parent 418cd23c0a
commit 10f0e2a5a3
2 changed files with 95 additions and 0 deletions

View File

@ -0,0 +1,88 @@
//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{}
break
case "darwin":
factory = &osxFactory{}
break
default:
factory = &otherOSFactory{}
}
button := factory.createButton()
button.paint()
}

View File

@ -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).