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 t
The Bridge pattern is a structural pattern (HOW DO YOU BUILD A SOFTWARE COMPONENT?). The Strategy pattern is a dynamic pattern (HOW DO YOU WANT TO RUN A BEHAVIOUR IN SOFTWARE?).
The syntax is similar but the goals are different:
Adding to willcodejavaforfood's answer, they can be the same, in implementation. However you use strategy to swap strategies such as sorting strategy, while you use bridge to bridge the implementations of two object's say a database wrapper, and a network adaptor so the client code can use either working against the same API. So the naming actually says it all
For strategy pattern only the implementation varies.
Suppose, class A is using class B which has multiple implementations available. So in that case B would be abstract with actual implementation provided at runtime. This is strategy pattern
Now if A itself is abstract. Both A and B may vary. You would use Bridge pattern.
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.
Bridge: ( A structural pattern)
Bridge pattern decouples abstraction and implementation and allows both to vary independently.
Use this pattern when :
Strategy: ( Behavioural pattern)
Strategy patterns enable you to switch between multiple algorithms from a family of algorithms at run time.
Use Strategy pattern when :
Related posts:
When do you use the Bridge Pattern? How is it different from Adapter pattern?
Real World Example of the Strategy Pattern
Strategy pattern is used when you wish to plug algorithm or strategy at run time. As category of pattern also implies that it deals with behaviour of the objects. On the other hand bridge is structural pattern and deals with structural hierarchy of the objects. It decouples the abstraction from implementation by introducing a refined abstraction between them. Refined abstraction can be confused with the run time strategy plugged (In Strategy pattern). Bridge pattern deals with the structural aspects by providing a mechanism to avoid creating n number of classes.