From 87d44adc6d435e576d6f39906ca2a38755251af5 Mon Sep 17 00:00:00 2001 From: Edward Date: Sun, 3 May 2020 19:33:29 +0800 Subject: [PATCH] update README in template pattern --- README.md | 8 +++++++- behavior/05_template_method/README.md | 6 +++--- .../{templatemethod.go => template_method.go} | 0 .../{templatemethod_test.go => template_method_test.go} | 0 4 files changed, 10 insertions(+), 4 deletions(-) rename behavior/05_template_method/{templatemethod.go => template_method.go} (100%) rename behavior/05_template_method/{templatemethod_test.go => template_method_test.go} (100%) diff --git a/README.md b/README.md index c63979d..a8671b0 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ + [x] [闭包选项模式(Function Option)](./behavior/02_option) + [x] [观察者模式(Observer)](./behavior/10_observer) + [ ] [命令模式(Command)](./behavior/11_command) -+ [ ] [迭代器模式(Iterator)](./behavior/04_iterator) ++ [x] [迭代器模式(Iterator)](./behavior/04_iterator) + [ ] [模板方法模式(Template Method)](./behavior/14_template_method) + [x] [策略模式(Strategy)](./behavior/12_strategy) + [ ] [状态模式(State)](./behavior/behavior16_state) @@ -82,6 +82,12 @@ [go-resiliency](https://github.com/eapache/go-resiliency) +[Behavioral](https://github.com/AlexanderGrom/go-patterns/tree/master/Behavioral) + +[go-patterns](https://github.com/sevenelevenlee/go-patterns) + +[go_design_pattern](https://github.com/monochromegane/go_design_pattern) + ## 更多 diff --git a/behavior/05_template_method/README.md b/behavior/05_template_method/README.md index ad8dadd..72d362b 100644 --- a/behavior/05_template_method/README.md +++ b/behavior/05_template_method/README.md @@ -1,9 +1,9 @@ # 模版方法模式 -模版方法模式使用继承机制,把通用步骤和通用方法放到父类中,把具体实现延迟到子类中实现。使得实现符合开闭原则。 +模板方法就是为了将方法和结构各自实现分开到不同的地方,并且可以单独实现,往往是将将方法/处理器的实现延迟到子类或者其他可注入的类型中。 -如实例代码中通用步骤在父类中实现(`准备`、`下载`、`保存`、`收尾`)下载和保存的具体实现留到子类中,并且提供 `保存`方法的默认实现。 +模板方法就是设计的核心思想体现,如果把其他的模式都忘了,只要还记得一个模式,就能变化其他的任意模块,设计模式的作用要点就是要封装变化点,尽量降低耦合,方法,结构分离,接口继承,结构继承,都是为了将不同的变化点封装到不同的模块,以使其能够单独演化,尽量做到通用。 -因为Golang不提供继承机制,需要使用匿名组合模拟实现继承。 +go 的结构对象和接口的分离做的更彻底,不需要像其他语言一样显式声明继承或者实现关系,所以在实现模板方法时候更灵活。 此处需要注意:因为父类需要调用子类方法,所以子类需要匿名组合父类的同时,父类需要持有子类的引用。 diff --git a/behavior/05_template_method/templatemethod.go b/behavior/05_template_method/template_method.go similarity index 100% rename from behavior/05_template_method/templatemethod.go rename to behavior/05_template_method/template_method.go diff --git a/behavior/05_template_method/templatemethod_test.go b/behavior/05_template_method/template_method_test.go similarity index 100% rename from behavior/05_template_method/templatemethod_test.go rename to behavior/05_template_method/template_method_test.go