首先先看一段比较简单的代码,大概能说明策略模式什么意思:
一、Intent
Define a family of algorithms, encapsulate each one, and make them interchangeable.Strategy lets the algorithem vary idependently from clients that use it.
二、实现时注意的问题:
(1)定义strategy和context之间的借口,这个很重要,他确定了context如何调用strategy中的虚接口,一般有两种:一种是让context把数据放在参数中传递给strategy操作;另一种,让context将自己作为一个参数传递给strategy。
(2)将strategy作为模板参数传递给strategy,这个在c++中比较适用。