mirror of
https://github.com/tmrts/go-patterns.git
synced 2025-02-18 05:23:14 +03:00
87 lines
1.3 KiB
Go
87 lines
1.3 KiB
Go
//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()
|
|
}
|