added shape factory method

This commit is contained in:
Jian Han 2018-01-06 23:30:19 +10:00
parent f50cc7cb23
commit edcdcb9456
3 changed files with 88 additions and 0 deletions

View File

@ -0,0 +1,38 @@
package ch1
import (
"fmt"
"sync"
"time"
)
type Job struct {
i int
max int
text string
}
func outputText(j *Job, goGroup *sync.WaitGroup) {
for j.i < j.max {
time.Sleep(1 * time.Millisecond)
fmt.Println(j.text)
j.i++
}
goGroup.Done()
}
func Run() {
goGroup := new(sync.WaitGroup)
fmt.Println("Starting")
hello := new(Job)
hello.text = "hello"
hello.i = 0
hello.max = 2
world := new(Job)
world.text = "world"
world.i = 0
world.max = 2
go outputText(hello, goGroup)
go outputText(world, goGroup)
goGroup.Add(2)
goGroup.Wait()
}

View File

@ -0,0 +1,7 @@
package main
import "github.com/jianhan/go-patterns/concurrency/mastering_concurrency_in_go/ch1"
func main() {
ch1.Run()
}

View File

@ -0,0 +1,43 @@
package shape
import (
"errors"
"github.com/davecgh/go-spew/spew"
)
type Shape interface {
Draw()
}
type Circle struct {
}
func (c *Circle) Draw() {
spew.Dump("Draw Circle")
}
type Square struct {
}
func (s *Square) Draw() {
spew.Dump("Draw Square")
}
type Rec struct {
}
func (r *Rec) Draw() {
spew.Dump("Draw Rec")
}
func GetShape(shape string) (Shape, error) {
if shape == "Circle" {
return &Circle{}, nil
} else if shape == "Square" {
return &Square{}, nil
} else if shape == "Rec" {
return &Rec{}, nil
}
return nil, errors.New("Shape not found")
}