怎么感觉Adapter 和Strategy 很像呢?
Adapter:
I want an object of a class calls a method ,while it's not the type or subtype of the argument.
What I need to do is,
first change the argument type into an interface ,
then make the class implements it.Strategy:
you just send the a object(reference) of subtypes to the method
把形参改成接口后,类再实现(implement)它,这个类就继承了接口,
之后就和Strategy的做法没区别了啊?
还有什么区别么?没区别怎么分两种叫法?
有价值的回复立马给30分。
Adapter:
I want an object of a class calls a method ,while it's not the type or subtype of the argument.
What I need to do is,
first change the argument type into an interface ,
then make the class implements it.Strategy:
you just send the a object(reference) of subtypes to the method
把形参改成接口后,类再实现(implement)它,这个类就继承了接口,
之后就和Strategy的做法没区别了啊?
还有什么区别么?没区别怎么分两种叫法?
有价值的回复立马给30分。
适配器(变压器)模式:把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口原因不匹配而无法一起工作的两个类能够一起工作。适配类可以根据参数返还一个合适的实例给客户端。
21、STRATEGY—跟不同类型的MM约会,要用不同的策略,有的请电影比较好,有的则去吃小吃效果不错,有的去海边浪漫最合适,单目的都是为了得到MM的芳心,我的追MM锦囊中有好多Strategy哦。
策略模式:策略模式针对一组算法,将每一个算法封装到具有共同接口的独立的类中,从而使得它们可以相互替换。策略模式使得算法可以在不影响到客户端的情况下发生变化。策略模式把行为和环境分开。环境类负责维持和查询行为类,各种算法在具体的策略类中提供。由于算法和环境独立开来,算法的增减,修改都不会影响到环境和客户端。