From 322ab0d8318f0103751df4bc6e928796d52a67c4 Mon Sep 17 00:00:00 2001 From: ismayilmalik Date: Sat, 6 Jan 2018 23:46:59 +0400 Subject: [PATCH] implemented prototype pattern --- creational/prototype/prototype.go | 47 +++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 creational/prototype/prototype.go diff --git a/creational/prototype/prototype.go b/creational/prototype/prototype.go new file mode 100644 index 0000000..e0f5ca3 --- /dev/null +++ b/creational/prototype/prototype.go @@ -0,0 +1,47 @@ +package prototye + +import "fmt" +import "errors" + +const ( + WHITE = 1 + GREEN = 2 + BLACK = 3 +) + +type ProductInfoGetter interface { + GetInfo() string +} + +type Tshirt struct { + Size string + Color string +} + +func (t Tshirt) GetInfo() string { + return fmt.Sprintf("Tshirt size: '%s', Color: '%s'.", t.Size, t.Color) +} + +var whiteTshirt *Tshirt = &Tshirt{ "M", "white" } +var greenTshirt *Tshirt = &Tshirt{ "L", "green" } +var blackTshirt *Tshirt = &Tshirt{ "S", "black" } + +func GetTshirClone(t int) (ProductInfoGetter, error) { + switch t { + case WHITE: + return whiteTshirt, nil + case GREEN: + return greenTshirt, nil + case BLACK: + return blackTshirt, nil + default: + return nil, errors.New(fmt.Sprintf("Prototype id %d not recognized.", t)) + } +} + + + + + + +