views:

1636

answers:

5

I tried to read many articles on dofactory, wikipedia and many sites. I have no idea what the difference is between them.

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.

+1  A: 

From the wiki on Strategy pattern

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.

willcodejavaforfood
I hear an echo ;p
johnc
+6  A: 

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.

HTH, Kent

Kent Boogaart
+1  A: 

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

Robert Gould
A: 

Not many people know that both the Strategy and Bridge patterns are also equivalent to the j_random_hacker pattern. But although the UML diagrams for all three patterns are identical, the intent of the j_random_hacker pattern is subtly different: to promote the use of the j_random_hacker pattern.

j_random_hacker
Looks like someone it trying to earn the Peer Pressure badge...
Motti
No, just trying to have some fun. :) Sometimes Patternese gets a bit much for me.
j_random_hacker
+5  A: 

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 RUN A BEHAVIOUR IN SOFTWARE?).

The syntax is similar but the goal are differents:

  • Strategy: you have more ways for doing an operation; with strategy you can choice the algorithm at run-time and you can modify a single Strategy withot a lot of side-effects at compile-time;
  • Bridge: you can split the hierarchy of interface and class join him with an abstract reference (see explication)
alepuzio