设计模式之Strategy vs Template Method

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

  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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。