From 876810c06da07b3b9bb30e84b26f790099c72b84 Mon Sep 17 00:00:00 2001 From: nitesh420 Date: Mon, 20 Aug 2018 00:36:39 +0530 Subject: [PATCH] Add files via upload --- structural/flyweight.go | 54 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 structural/flyweight.go diff --git a/structural/flyweight.go b/structural/flyweight.go new file mode 100644 index 0000000..134b5d0 --- /dev/null +++ b/structural/flyweight.go @@ -0,0 +1,54 @@ +package main +import ( + "fmt" + "math/rand" +) + +type Shape interface { + draw() +} + +type Circle struct{ + x,y,r int + color string +} + +func (c Circle)draw() { + fmt.Println("Draw circle..here",c.x,c.y,c.color) +} + +type ShapeFactory struct{ + shapeMap map[string]Shape + colorArray []string +} + +func NewShapeFactory()ShapeFactory{ + shapeFactoryVar:=ShapeFactory{} + shapeFactoryVar.shapeMap=make(map[string]Shape) + return shapeFactoryVar +} + +func(s ShapeFactory)getShape(color string) Shape{ + if circleVar,ok:=s.shapeMap[color];ok { + fmt.Println("Cirle object of "+color +" already created..") + return circleVar + } + circleVar:=Circle{} + circleVar.x=rand.Intn(100) + circleVar.y=rand.Intn(100) + circleVar.color=color + s.shapeMap[circleVar.color] =circleVar + return circleVar +} + +func main(){ + var shapevar Shape + shapeFactoryVar:=NewShapeFactory() + colorArray:=[]string{"red","yellow","blue","black","orange","green","purple","white"} + for i:=1;i<=10;i++ { + color:=colorArray[rand.Intn(len(colorArray)-1)] + shapevar =shapeFactoryVar.getShape(color) + shapevar.draw() + } + +} \ No newline at end of file