11

I know, this question was asked many times, but I did some research and still don't get it, probably you can help me out: As stated many times, the UML is almost the same. Also the implementation and idea is more or less the same: Instead of sub-typing, you define an Interface, which encapsulates some logic and let's it pass to an abstract. So, even the Microsoft-Blog guys

https://blogs.msdn.microsoft.com/gyanjadal/2015/01/05/difference-between-strategy-and-bridge-patterns/ says:

The simple answer is “They are similar but different”. The implementations are similar but the intentions are different. To give an analogy, a city bus and school bus are both similar vehicles, but they are used for different purposes. One is used to transport people between various parts of the city as a commuter service. The other is used for transporting kids to schools.

"If it sounds like a duck and looks like a duck but it intends to be a swan, it can be either of them", which is what I read here.

Since I still did't get it, so I digged deeper:

https://social.msdn.microsoft.com/Forums/en-US/08775d39-2de0-4598-8872-df21f681b7b3/strategy-vs-bridge-patterns?forum=architecturegeneral

This Thread also doesn't add anything new, except:

They both look the same on the surface to me as well. The main difference I see is the fact that in the Bridge pattern, the abstraction is PART OF the object, but in the Strategy pattern the abstraction is performed BY the object.

But, if we read the definition of strategy:

Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

There is nothing defined, how the Strategy is applied. It could also easily be an Interface on the Abstract, exactly the same the common Strategy-Implementation as LINQ-Orderby etc.

Another interest take on the topic is here:

http://game-engineering.blogspot.ch/2008/07/bridge-pattern-vs-strategy-pattern.html

The mainpart from this excourse:

You say "Strategy" when you want to vary behavior, and you do so not by writing different objects but by introducing a class heirarchy. You say "Bridge" when you expect that you will vary both the interface and the implementation. In both cases you're providing flexibility for a changing implementation; in a Bridge, you're also expecting the interface to change.

Is this probably the main-difference? Since the Implementor and the Abstraction are so loose coupled, I can change the Interface of the Implementor and the Abstraction doesn't have to care? That sounds reasonable, but wouldn't then have the Abstraction to change as well, since they are kindahow connected? Wouldn't that destroy all other principles like Information hiding and DRY?

I also looked at many many examples, which I don't add here for the sake of place, and I couldn't find an Example of either of those patterns I couldn't change to fit the other one. Be it via an Interface-Property or just an Parameter.

Did I miss anything here? Does probably anyone have a REAL-LIFE example of "I wanted to use Strategy, but the Bridge did just fit better", or visa versa, example?

Edit: Why do I justify an own Thread for this Topic (again)? First of all, the accepted answer of the mentioned Thread is the following

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.

I already provided in the previous explanations, that abstracting behavior from external source is exactly the definition of Strategy- and Bridge-Pattern.

Also

and you're using the bridge pattern when you use the same constructs to make your code a bit neater.

Also the strategy pattern makes the code way neater, since it abstracts an whole building block away, thus thightens the Code quite a bit.

I think anyone, who read the whole topic sees, that there is more on this topic just than this 2 sentences.

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
Matthias Müller
  • 3,336
  • 3
  • 33
  • 65
  • 7
    Truthfully, I never say "I wanted to use SomePattern". I solve the problem, then use patterns to describe what I did to other developers. – Eris Aug 07 '16 at 15:49
  • 2
    Possible duplicate of [What is the difference between the bridge pattern and the strategy pattern?](http://stackoverflow.com/questions/464524/what-is-the-difference-between-the-bridge-pattern-and-the-strategy-pattern) – Eris Aug 07 '16 at 19:10
  • 1
    Also: http://stackoverflow.com/questions/5863530/strategy-vs-bridge-patterns?rq=1 – Eris Aug 07 '16 at 19:11
  • I looked into this threads, but the general consensus is "Bridge is structural, Strategy is behavioral", which isn't enough for me to solve this riddle. Closing the topic because nobody can give a proper answer doesn't make sense, since often enough, people learn from a question, not the given answer, which reflects most often just one oppinion. – Matthias Müller Aug 08 '16 at 11:19
  • Frankly, the accepted answer at http://stackoverflow.com/a/464549/1168342 is not a great answer (it cites Wikipedia and the information about UML is wrong and was removed from the page long ago). – Fuhrmanator Aug 08 '16 at 18:38
  • 1
    The `RemoteControl` example from Head First Design Patterns is a pretty good explanation of Bridge. In that example, it's closer to the Command pattern than the Strategy pattern. – Fuhrmanator Aug 08 '16 at 18:58
  • Thanks, I know the book, I will look into it. – Matthias Müller Aug 12 '16 at 21:04

2 Answers2

3

I checked original design patterns book to see how the authors were defining Bridge pattern. Their real-life examle shows a case when both abstraction and imlementation hierarchies can change independently (i.e. new subclasses can be introduced for an abstraction; new subclasses can be introduced for implementations). Their example deals with window library that can work for different window systems.

In the original example the authors used a different window system from IBM, but I believe a good current analogy would be different Linux window managers (GNOME, KDE, etc.).

So, imagine a Window abstraction, and two implementations for GNOME and KDE. And now imagine that you want to add new Window subclass, TransparentWindow. TransparentWindow extends Window, so as GNOMEWindow and KDEWindow. But you also have to provide imlementations for TransparentWindow: GNOMETransparentWindow and KDETransparentWindow. The hierarchy starts looking messy.

Imagine a new type of window, or new window manager - XFCE. To avoid complicated hierarchies, they introduce the Bridge pattern, and make the two hierarchies separate (i.e. TransparentWindow extends Window; GNOMEWindow and KDEWindow extend WindowImpl).

To me it seems that the tricky part is to define an interface for implementation, since the hierarchies of abstractions will need to define their operations using only that interface.

A learning example of Bridge pattern that I liked is here, and I liked it because it does not use artificial classes ConcreteImplementor1 and ConcreteImplementor2. When it comes to real-life examples, I believe I saw this pattern in Selenium WebDriver implementation, but I am not 100% sure now.

F1Krazy
  • 146
  • 1
  • 4
  • 14
oldbam
  • 2,397
  • 1
  • 16
  • 24
  • Many thanks, seems so strange to me that a pattern works so hard against all the known principles: If the Implementation and the Hierarchy know so few about each other, someone from out side needs to know, what to pass. So, the Encapsulation, high cohesion, and even loose coupling are all gone. – Matthias Müller Aug 11 '16 at 10:00
  • TransparentWindow knows that it has to draw a border, and set background color to transparent. WindowImpl has a method to draw a border and a method to set background color. GNOMEWindowImpl knows how to draw a border and set a background in GNOME window manager. GNOMEWindowImpl does not know that those two operations will be called to draw a Transparent window. So, it's not that they Abstraction and Implementation know so few about each other, it's more of them both knowing what they need to know :) – oldbam Aug 11 '16 at 10:51
  • Hm, ok thanks, as written in the previous comment, the main difference seems to be infact the orthogonal hierarchy. Basically, the strategy pattern could do that also, but the bridge one is exactly thought for that. – Matthias Müller Aug 11 '16 at 14:42
1

In the Strategy pattern, the activities of the "Parent" for a particular operation are constant whereas the activities of the "Child" can vary. However, in the Bridge Pattern, the activities of the Parent, as well as the Child, can vary.

So, for example,

public class Ticket {
    
    Date dateOfTravel;
    int distance;
    Vehicle vehicle;
    Seat  seat;
    
    public float getTotalFare(){
         //depends on 
               //Distance
               //Vehicle - whether Vehicle is AC or non-AC. 
               //Seat - based on the location of the Seat.
     
        //Fare = vehicleBaseFare*seatMultiplier*distance

    }
    
}

In the above, the variations depend on the Parent (distance) as well as the children (Vehicle and Seat). So, here Vehicle and Seat both acted like Bridge.

Now, here

public class Vehicle {

   TrackingDevice device;

   public Coordinates getCoordinates(){
       return device.getCoordinates();
   }
}

Here, the Parent's role was constant, i.e., nothing! So, this was a Strategy pattern.

Viplove Dev
  • 15
  • 1
  • 6