From d6a877df7dbad6b460ff07128215c0e59af8623d Mon Sep 17 00:00:00 2001 From: ismayilmalik Date: Sun, 31 Dec 2017 21:49:53 +0300 Subject: [PATCH] implemented builder pattern --- creational/builder/builder.go | 99 ++++++++++++++++++++++++++++++ creational/builder/builder_test.go | 33 ++++++++++ 2 files changed, 132 insertions(+) create mode 100644 creational/builder/builder.go create mode 100644 creational/builder/builder_test.go diff --git a/creational/builder/builder.go b/creational/builder/builder.go new file mode 100644 index 0000000..3676d25 --- /dev/null +++ b/creational/builder/builder.go @@ -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() +} diff --git a/creational/builder/builder_test.go b/creational/builder/builder_test.go new file mode 100644 index 0000000..ad41b3c --- /dev/null +++ b/creational/builder/builder_test.go @@ -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) + } +} \ No newline at end of file