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 Builder Pattern 馃帀 #2

Open
wants to merge 1 commit into
base: master
from
Open
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,95 @@
package patterns

// BuildProcess interface
type BuildProcess interface {
SetColor() BuildProcess
SetTag() BuildProcess
GetUIProduct() UIProduct
}

// UIDirector struct
type UIDirector struct {
builder BuildProcess
}

// Construct method on UIDirector struct
func (f *UIDirector) Construct() {
f.builder.SetTag().SetColor()
}

// SetBuilder method on UIDirector struct
func (f *UIDirector) SetBuilder(b BuildProcess) {
f.builder = b
}

// UIProduct struct
type UIProduct struct {
Color string
Tag string
}

// ReactBuilder struct
type ReactBuilder struct {
v UIProduct
}

// SetColor method on BuildProcess struct
func (c *ReactBuilder) SetColor() BuildProcess {
c.v.Color = "Blue"
return c
}

// SetTag method on BuildProcess struct
func (c *ReactBuilder) SetTag() BuildProcess {
c.v.Tag = "React"
return c
}

// GetUIProduct method on UIProduct struct
func (c *ReactBuilder) GetUIProduct() UIProduct {
return c.v
}

// VueBuilder struct
type VueBuilder struct {
v UIProduct
}

// SetColor method on BuildProcess struct
func (b *VueBuilder) SetColor() BuildProcess {
b.v.Color = "Green"
return b
}

// SetTag method on BuildProcess struct
func (b *VueBuilder) SetTag() BuildProcess {
b.v.Tag = "Vue"
return b
}

// GetUIProduct method on UIProduct struct
func (b *VueBuilder) GetUIProduct() UIProduct {
return b.v
}

// AngularBuilder struct
type AngularBuilder struct {
v UIProduct
}

// SetColor method on BuildProcess struct
func (b *AngularBuilder) SetColor() BuildProcess {
b.v.Color = "Red"
return b
}

// SetTag method on BuildProcess struct
func (b *AngularBuilder) SetTag() BuildProcess {
b.v.Tag = "Angular"
return b
}

// GetUIProduct method on UIProduct struct
func (b *AngularBuilder) GetUIProduct() UIProduct {
return b.v
}
@@ -0,0 +1,51 @@
package patterns

import "testing"

func TestBuilderPattern(t *testing.T) {
uiDirector := UIDirector{}

reactBuilder := &ReactBuilder{}
uiDirector.SetBuilder(reactBuilder)
uiDirector.Construct()

react := reactBuilder.GetUIProduct()

if react.Color != "Blue" {
t.Errorf("Color on a react ui must be 'Blue' and they were %s\n", react.Color)
}

if react.Tag != "React" {
t.Errorf("Tag on a react ui must be 'React' and was %s\n", react.Tag)
}

VueBuilder := &VueBuilder{}

uiDirector.SetBuilder(VueBuilder)
uiDirector.Construct()

vue := VueBuilder.GetUIProduct()

if vue.Color != "Green" {
t.Errorf("Color on a vue must be 'Green' and they were %s\n", vue.Color)
}

if vue.Tag != "Vue" {
t.Errorf("Tag on a vue must be 'Vue' and was %s\n", vue.Tag)
}

angularBuilder := &AngularBuilder{}

uiDirector.SetBuilder(angularBuilder)
uiDirector.Construct()

angular := angularBuilder.GetUIProduct()

if angular.Color != "Red" {
t.Errorf("Color on a angular must be 'Red' and they were %s\n", angular.Color)
}

if angular.Tag != "Angular" {
t.Errorf("Tag on a angular must be 'Bus' and was %s\n", angular.Tag)
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can鈥檛 perform that action at this time.