Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Abstract factory 馃帀 #1

Merged
merged 3 commits into from Dec 19, 2019
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to鈥
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -0,0 +1,7 @@
language: go
go:
- master
env:
- GO111MODULE=on
script:
- go test .
@@ -0,0 +1,85 @@
package patterns

import (
"fmt"
)

// Button interface
type Button interface {
Paint()
}

// GUIFactory interface
type GUIFactory interface {
CreateButton() Button
}

// ReactFactory struct
type ReactFactory struct{}

// CreateButton method on ReactFactory struct
func (f *ReactFactory) CreateButton() Button {
return new(ReactButton)
}

// VueFactory struct
type VueFactory struct{}

// CreateButton method on VueFactory struct
func (f *VueFactory) CreateButton() Button {
return new(VueButton)
}

// AngularFactory struct
type AngularFactory struct{}

// CreateButton method on AngularFactory struct
func (f *AngularFactory) CreateButton() Button {
return new(AngularButton)
}

// ReactButton struct
type ReactButton struct{}

// Paint method on ReactButton struct
func (b *ReactButton) Paint() {
fmt.Println("ReactButton")
}

// VueButton struct
type VueButton struct{}

// Paint method on VueButton struct
func (b *VueButton) Paint() {
fmt.Println("VueButton")
}

// AngularButton struct
type AngularButton struct{}

// Paint method on AngularButton struct
func (b *AngularButton) Paint() {
fmt.Println("AngularButton")
}

const (
// React denote
React = iota
// Vue denote
Vue
// Angular denote
Angular
)

// BuildFactory function return GUIFactory instance
func BuildFactory(appearance int) GUIFactory {
switch appearance {
case React:
return new(ReactFactory)
case Vue:
return new(VueFactory)
case Angular:
return new(AngularFactory)
}
return nil
}
@@ -0,0 +1,23 @@
package patterns

func ExampleBuildFactory() {
if factory := BuildFactory(React); factory != nil {
reactButton := factory.CreateButton()
reactButton.Paint()
}

if factory := BuildFactory(Vue); factory != nil {
vueButton := factory.CreateButton()
vueButton.Paint()
}

if factory := BuildFactory(Angular); factory != nil {
angularButton := factory.CreateButton()
angularButton.Paint()
}

// Output:
// ReactButton
// VueButton
// AngularButton
}
ProTip! Use n and p to navigate between commits in a pull request.
You can鈥檛 perform that action at this time.