美文网首页
设计模式之策略模式vs模板方法

设计模式之策略模式vs模板方法

作者: 天之見證 | 来源:发表于2024-07-09 12:30 被阅读0次

笔者之前对策略模式和模板方法的理解有些模糊,正好有空查查资料搞清楚两者之间的区别。一句话来说就是:

  1. 策略模式:把具体实现都留给子类或者实现类。重子类
  2. 模板方法:父类定好算法框架,其他的部分细节交由子类实现。重父类

《代码大全》对2个模式的解释如下:

Strategy: Defines a set of algorithms or behaviors that are dynamically interchangeable with each other.

Template Method: Defines the structure of an algorithm but leaves some of the detailed implementation to subclasses

以上表述可以用下图汇总:


image.png

ref:

  1. What is the difference between the template method and the strategy patterns? - Stack Overflow

相关文章

网友评论

      本文标题:设计模式之策略模式vs模板方法

      本文链接:https://www.haomeiwen.com/subject/votzcjtx.html