mirror of
https://github.com/crazybber/go-pattern-examples.git
synced 2025-02-16 17:23:15 +03:00
update README in template pattern
This commit is contained in:
parent
4f6f503260
commit
87d44adc6d
@ -22,7 +22,7 @@
|
|||||||
+ [x] [闭包选项模式(Function Option)](./behavior/02_option)
|
+ [x] [闭包选项模式(Function Option)](./behavior/02_option)
|
||||||
+ [x] [观察者模式(Observer)](./behavior/10_observer)
|
+ [x] [观察者模式(Observer)](./behavior/10_observer)
|
||||||
+ [ ] [命令模式(Command)](./behavior/11_command)
|
+ [ ] [命令模式(Command)](./behavior/11_command)
|
||||||
+ [ ] [迭代器模式(Iterator)](./behavior/04_iterator)
|
+ [x] [迭代器模式(Iterator)](./behavior/04_iterator)
|
||||||
+ [ ] [模板方法模式(Template Method)](./behavior/14_template_method)
|
+ [ ] [模板方法模式(Template Method)](./behavior/14_template_method)
|
||||||
+ [x] [策略模式(Strategy)](./behavior/12_strategy)
|
+ [x] [策略模式(Strategy)](./behavior/12_strategy)
|
||||||
+ [ ] [状态模式(State)](./behavior/behavior16_state)
|
+ [ ] [状态模式(State)](./behavior/behavior16_state)
|
||||||
@ -82,6 +82,12 @@
|
|||||||
|
|
||||||
[go-resiliency](https://github.com/eapache/go-resiliency)
|
[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)
|
||||||
|
|
||||||
|
|
||||||
## 更多
|
## 更多
|
||||||
|
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
# 模版方法模式
|
# 模版方法模式
|
||||||
|
|
||||||
模版方法模式使用继承机制,把通用步骤和通用方法放到父类中,把具体实现延迟到子类中实现。使得实现符合开闭原则。
|
模板方法就是为了将方法和结构各自实现分开到不同的地方,并且可以单独实现,往往是将将方法/处理器的实现延迟到子类或者其他可注入的类型中。
|
||||||
|
|
||||||
如实例代码中通用步骤在父类中实现(`准备`、`下载`、`保存`、`收尾`)下载和保存的具体实现留到子类中,并且提供 `保存`方法的默认实现。
|
模板方法就是设计的核心思想体现,如果把其他的模式都忘了,只要还记得一个模式,就能变化其他的任意模块,设计模式的作用要点就是要封装变化点,尽量降低耦合,方法,结构分离,接口继承,结构继承,都是为了将不同的变化点封装到不同的模块,以使其能够单独演化,尽量做到通用。
|
||||||
|
|
||||||
因为Golang不提供继承机制,需要使用匿名组合模拟实现继承。
|
go 的结构对象和接口的分离做的更彻底,不需要像其他语言一样显式声明继承或者实现关系,所以在实现模板方法时候更灵活。
|
||||||
|
|
||||||
此处需要注意:因为父类需要调用子类方法,所以子类需要匿名组合父类的同时,父类需要持有子类的引用。
|
此处需要注意:因为父类需要调用子类方法,所以子类需要匿名组合父类的同时,父类需要持有子类的引用。
|
||||||
|
Loading…
x
Reference in New Issue
Block a user