Difference between Strategy pattern and Delegation pattern

后端 未结 4 1496
小蘑菇
小蘑菇 2020-12-28 16:13

What is the difference between Strategy pattern and Delegation pattern (not delegates)?

4条回答
  •  太阳男子
    2020-12-28 16:52

    Here's a thought:

    Delegates mimic the delegating class (at least as I've used them, not sure if that's the canonical way or not but that's how I usually do it). So basically, if I have a class that has multiple entry points (methods) and I want to change the implementation at runtime, I would create delegates the implement the same interface.

    If, on the other hand, I had one part of a class that I want to be able to interchange at runtime, I would create Strategy classes with a single method interface (eg. executeCalculation) and make it an aggregate component of the containing class.

    So in summary, a strategy encompasses a single behavior, delegates implement a set of behaviors, and you could use delegates to implement strategies.

提交回复
热议问题