Search code examples
design-patternsstrategy-patternbridge

What is the difference between the bridge pattern and the strategy pattern?


I tried to read many articles on dofactory, wikipedia and many sites. I have no idea on differences between bridge pattern and the strategy pattern.

I know both of them decouple an abstraction from its implementation and can change implementation at run time.

But I still don't know in which situation I should use strategy or in which situation I should use bridge.


Solution

  • Semantics. From wikipedia:

    The UML class diagram for the Strategy pattern is the same as the diagram for the Bridge pattern. However, these two design patterns aren't the same in their intent. While the Strategy pattern is meant for behavior, the Bridge pattern is meant for structure.

    The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.

    As I understand it, you're using the strategy pattern when you're abstracting behavior that could be provided from an external source (eg. config could specify to load some plugin assembly), and you're using the bridge pattern when you use the same constructs to make your code a bit neater. The actual code will look very similar - you're just applying the patterns for slightly different reasons.