From 6a74cbe51bf0a08e32b01c32b2966fda52e3db89 Mon Sep 17 00:00:00 2001 From: Edward Date: Wed, 6 May 2020 17:12:28 +0800 Subject: [PATCH] add a fanin pattern codes --- README.md | 6 ++--- gomore/04_fan_in/fan_in.go | 38 +++++++++++++++++++++++++++++++ gomore/04_fan_in/fan_in_test.go | 26 +++++++++++++++++++++ gomore/05_fan_out/fan_out_test.go | 6 ++--- 4 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 gomore/04_fan_in/fan_in.go create mode 100644 gomore/04_fan_in/fan_in_test.go diff --git a/README.md b/README.md index 734136e..1820d20 100644 --- a/README.md +++ b/README.md @@ -4,12 +4,11 @@ 目前包括了**39种Go中常用的、面向工程化和最佳实践的模式/套路**,自然也包含常见的23种设计模式,重点是这里全部是例子、通俗易懂,甚至每个模式下的例子,改一下名字,稍微再增加几行代码就可以直接用在项目和工程中了。 - 每一种设计模式都有其特定的应用场景和要解决的问题,了解模式的关键点就在于弄清这些目标场景和问题,千万不要纠结于:为什么这个模式叫这个名字,这个模式为啥要这样用? **这些模式不是你总结的,也不是我的总结的,如果是你的写的,你可以按照自己的喜欢的感觉给这些套路取名字,让别人去费劲想。** -## 姿势 Ways +## 了解一下姿势 Ways + 所谓模式就是套路,如功夫,招有定式 + 学习模式,就是学习套路,弄清楚套路要解决的目标场景,这很重要. @@ -26,7 +25,7 @@ + [x] [迭代器模式(Iterator)](./behavior/04_iterator) + [x] [模板方法模式(Template Method)](./behavior/05_template_method) + [x] [策略模式(Strategy)](./behavior/12_strategy) -+ [ ] [WIP][状态模式(State)](./behavior/behavior16_state) ++ [x] [状态模式(State)](./behavior/behavior16_state) + [x] [访问者模式(Visitor)](./behavior/07_visitor) + [x] [解释器模式(Interpreter)](./behavior/08_interpreter) + [x] [职责链模式(Chain of Responsibility)](./behavior/06_chain_of_responsibility) @@ -76,7 +75,6 @@ [go-resiliency](https://github.com/eapache/go-resiliency) | [Behavioral](https://github.com/AlexanderGrom/go-patterns/tree/master/Behavioral)|v| | [go-patterns](https://github.com/sevenelevenlee/go-patterns) | [go_design_pattern](https://github.com/monochromegane/go_design_pattern)|p| - ## 更多 More 需要重新温习下Go基础?看这里 diff --git a/gomore/04_fan_in/fan_in.go b/gomore/04_fan_in/fan_in.go new file mode 100644 index 0000000..84dcfcc --- /dev/null +++ b/gomore/04_fan_in/fan_in.go @@ -0,0 +1,38 @@ +package fanin + +func generatePipeline(numbers []int) <-chan int { + out := make(chan int) + go func() { + for _, n := range numbers { + out <- n + } + close(out) + }() + return out +} + +func squareNumber(in <-chan int) <-chan int { + out := make(chan int) + go func() { + for n := range in { + out <- n * n + } + close(out) + }() + return out +} + +func fanIn(input1, input2 <-chan int) <-chan int { + c := make(chan int) + go func() { + for { + select { + case s := <-input1: + c <- s + case s := <-input2: + c <- s + } + } + }() + return c +} diff --git a/gomore/04_fan_in/fan_in_test.go b/gomore/04_fan_in/fan_in_test.go new file mode 100644 index 0000000..8cc4d4e --- /dev/null +++ b/gomore/04_fan_in/fan_in_test.go @@ -0,0 +1,26 @@ +package fanin + +import ( + "fmt" + "testing" +) + +func TestFanIn(T *testing.T) { + randomNumbers := []int{13, 44, 56, 99, 9, 45, 67, 90, 78, 23} + // generate the common channel with inputs + inputChan := generatePipeline(randomNumbers) + + // Fan-out to 2 Go-routine + c1 := squareNumber(inputChan) + c2 := squareNumber(inputChan) + + // Fan-in the resulting squared numbers + c := fanIn(c1, c2) + sum := 0 + + // Do the summation + for i := 0; i < len(randomNumbers); i++ { + sum += <-c + } + fmt.Printf("Total Sum of Squares: %d", sum) +} diff --git a/gomore/05_fan_out/fan_out_test.go b/gomore/05_fan_out/fan_out_test.go index d75c88d..1d67a37 100644 --- a/gomore/05_fan_out/fan_out_test.go +++ b/gomore/05_fan_out/fan_out_test.go @@ -1,6 +1,6 @@ -fanout +package fanout -import concurrency +import "concurrency" type taggingDispatcher struct { Address string @@ -49,4 +49,4 @@ tagging.pipeline.Start() func main(){ tagging.pipeline.Dispatch(youStruct{}) -} \ No newline at end of file +}