mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-25 14:26:04 +03:00
added shape factory method
This commit is contained in:
parent
f50cc7cb23
commit
edcdcb9456
38
concurrency/mastering_concurrency_in_go/ch1/ch1.go
Normal file
38
concurrency/mastering_concurrency_in_go/ch1/ch1.go
Normal 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()
|
||||||
|
}
|
7
concurrency/mastering_concurrency_in_go/main.go
Normal file
7
concurrency/mastering_concurrency_in_go/main.go
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "github.com/jianhan/go-patterns/concurrency/mastering_concurrency_in_go/ch1"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ch1.Run()
|
||||||
|
}
|
43
creational/factorymethod/shape/shape.go
Normal file
43
creational/factorymethod/shape/shape.go
Normal 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")
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user