implemented builder pattern
This commit is contained in:
parent
6e66e82290
commit
d6a877df7d
99
creational/builder/builder.go
Normal file
99
creational/builder/builder.go
Normal file
@ -0,0 +1,99 @@
|
||||
package builder
|
||||
|
||||
type PersonalComputer struct {
|
||||
ramCap int
|
||||
hddCap int
|
||||
cpu string
|
||||
os string
|
||||
gpu string
|
||||
}
|
||||
|
||||
type PCBuilder interface {
|
||||
SetRAM() PCBuilder
|
||||
SetHDD() PCBuilder
|
||||
SetCPU() PCBuilder
|
||||
SetOS() PCBuilder
|
||||
SetGPU() PCBuilder
|
||||
GetPC() PersonalComputer
|
||||
}
|
||||
|
||||
// Hoem edition pc builder
|
||||
type HomeEditionPCBuilder struct {
|
||||
pc PersonalComputer
|
||||
}
|
||||
|
||||
func (b *HomeEditionPCBuilder) SetRAM() PCBuilder {
|
||||
b.pc.ramCap = 4
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *HomeEditionPCBuilder) SetHDD() PCBuilder {
|
||||
b.pc.hddCap = 500
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *HomeEditionPCBuilder) SetCPU() PCBuilder {
|
||||
b.pc.cpu = "i3"
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *HomeEditionPCBuilder) SetOS() PCBuilder {
|
||||
b.pc.os = "Windows 7 Home Edition"
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *HomeEditionPCBuilder) SetGPU() PCBuilder {
|
||||
b.pc.gpu = "Intel Graphic Card"
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *HomeEditionPCBuilder) GetPC() PersonalComputer {
|
||||
return b.pc
|
||||
}
|
||||
|
||||
// Game edition pc builder
|
||||
type GameEditionPCBuilder struct {
|
||||
pc PersonalComputer
|
||||
}
|
||||
|
||||
func (b *GameEditionPCBuilder) SetRAM() PCBuilder {
|
||||
b.pc.ramCap = 16
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *GameEditionPCBuilder) SetHDD() PCBuilder {
|
||||
b.pc.hddCap = 500
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *GameEditionPCBuilder) SetCPU() PCBuilder {
|
||||
b.pc.cpu = "i7"
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *GameEditionPCBuilder) SetOS() PCBuilder {
|
||||
b.pc.os = "Windows 7 Ultimate"
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *GameEditionPCBuilder) SetGPU() PCBuilder {
|
||||
b.pc.gpu = "AMD Radeon X80"
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *GameEditionPCBuilder) GetPC() PersonalComputer {
|
||||
return b.pc
|
||||
}
|
||||
|
||||
//Manufacturer object which aware of build process for builder type
|
||||
type Manufacturer struct {
|
||||
b PCBuilder
|
||||
}
|
||||
|
||||
func (m *Manufacturer) SetBuilder(builder PCBuilder) {
|
||||
m.b = builder
|
||||
}
|
||||
|
||||
func (m *Manufacturer) ConstructPC() {
|
||||
m.b.SetCPU().SetHDD().SetRAM().SetGPU().SetOS()
|
||||
}
|
33
creational/builder/builder_test.go
Normal file
33
creational/builder/builder_test.go
Normal file
@ -0,0 +1,33 @@
|
||||
package builder
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestManufacturer(t *testing.T) {
|
||||
manufacturer := Manufacturer{}
|
||||
|
||||
homePCBuilder := &HomeEditionPCBuilder{}
|
||||
manufacturer.SetBuilder(homePCBuilder)
|
||||
manufacturer.ConstructPC()
|
||||
homePC := homePCBuilder.GetPC()
|
||||
|
||||
if homePC.cpu != "i3" {
|
||||
t.Errorf("Home edition PC cpu shoul be 'i3' but found %s", homePC.cpu)
|
||||
}
|
||||
|
||||
if homePC.gpu != "Intel Graphic Card" {
|
||||
t.Errorf("Home edition PC gpu shoul be 'Intel Graphic Card' but found %s", homePC.cpu)
|
||||
}
|
||||
|
||||
gamePCBuilder := &GameEditionPCBuilder{}
|
||||
manufacturer.SetBuilder(gamePCBuilder)
|
||||
manufacturer.ConstructPC()
|
||||
gamePC := gamePCBuilder.GetPC()
|
||||
|
||||
if gamePC.cpu != "i7" {
|
||||
t.Errorf("Game edition PC cpu shoul be 'i7' but found %s", gamePC.cpu)
|
||||
}
|
||||
|
||||
if gamePC.gpu != "AMD Radeon X80" {
|
||||
t.Errorf("Game edition PC gpu shoul be 'Intel Graphic Card' but found %s", gamePC.cpu)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user