惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
The GitHub Blog
The GitHub Blog
Recent Announcements
Recent Announcements
A
About on SuperTechFans
G
Google Developers Blog
aimingoo的专栏
aimingoo的专栏
U
Unit 42
WordPress大学
WordPress大学
Y
Y Combinator Blog
罗磊的独立博客
J
Java Code Geeks
Microsoft Azure Blog
Microsoft Azure Blog
腾讯CDC
博客园 - 叶小钗
Stack Overflow Blog
Stack Overflow Blog
Engineering at Meta
Engineering at Meta
Microsoft Security Blog
Microsoft Security Blog
GbyAI
GbyAI
V
V2EX
雷峰网
雷峰网
H
Hackread – Cybersecurity News, Data Breaches, AI and More
S
SegmentFault 最新的问题
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - bmrxntfj

通过match看rust 聊一聊Rust的enum 折旧记录某旅纵横app分析研究 实现自己的js框架 实现自己的Linq to Sql 统一的仓储接口 ddd官方终于发布了一个完整的示例 浅谈值对象 分层随想 prototype模式随想 ddd心得 ddd分层感想 小boss随想 论软件的价值 Composite模式随想 DDD书籍介绍 Bmrxntfj.UIMapper界面对象映射 闲来没事,写个老玩意 规格模式(Specification)
Strategy与Template Method
bmrxntfj · 2008-02-27 · via 博客园 - bmrxntfj

Strategy与Template Method

Strategy封装不同的算法,这些算法可以相互替换,于达到实现不同目的。
Template Method定义一个操作中算法的骨架,将一些步骤的执行延迟到其子类中。

Strategy通过对象组合来实现。
Template Method通过对象继承来实现。
根据“组合优先于继承”原则,在大多数情况下,我们选择Strategy。但并不是一开始
就知道要用Strategy.
有时是通过微重构(组合方法经常被使用),得到Template Method,当问题复杂时,子类需要重写
父类的很多方法,或者子类继承了它本不需要的方法时,应该把Template Method
重构倒Strategy.就像在Header First Deign Patterns中的提到的会飞的鸭子。
Strategy解决了继承带来的耦合问题。