Skip to main content
GameDev.net gamedev.net
🔒 Locked

Difference between Strategy and Bridge pattern?

Started by ukdeveloper Dec 8, 2008 at 8:53 PM 1 replies 4.5k views
Original Post
ukdeveloper
ukdeveloper
Hi folks, Looking over some past papers and one of the questions asks to compare the strategy and bridge patterns and clearly state the difference between them. I have been wracking my brains all day and cannot see it, they are so similar that I can't see what the difference is. My current understanding is this: Strategy pattern: You wish to reduce coupling and code repetition for a given task, but the given task is relatively common (e.g. two types of bank account withdrawal, one with a fee and one without), so you implement a strategy by using an abstract class or interface, with concrete subclasses implementing the exact detail of the operation. This is preferable to having a Withdraw() method declared virtual in the Account baseclass as that leads to spurious code repetition and a higher level of coupling. You might have AccountWithdrawal foo = new AccountWithdrawalWithFee();, to use polymorphism, and assign that to an account representing a bank account. So you can then have BankAccount.withdraw(); and it knows to use the AccountWithdrawalWithFee() to perform the right strategy. Bridge pattern: You have an abstract class called Shape and off of it you have subclasses called Rectangle and Circle. You have a class called Draw, an instance of which is included in the Shape class. Draw has two separate drawing packages called DrawingType1 and DrawingType2, so again you would do Draw foo = new DrawingType1(); and assign it to the Shape object, which you might create as Shape bar = new Rectangle();. I am struggling so hard to see what the difference is here. As far as I can see the two patterns are exactly the same or is my understanding flawed? Have I even got the patterns right at all? I'm confused and any help would be greatly appreciated. I have Googled this to death and the explanations I'm getting are either very confusing, contradictory or don't explain the difference clearly. TIA, ukd.
Telastyn
Telastyn
That's a pretty erm... a-typical bridge example. (note; that was before I googled and saw this sort of crap example is the wikipedia article)

Generally the sides of the bridge are completely different modules, or have different interfaces. The context tends to be bi-directional. The bridge also tends to be used just to make two things work together without too much coupling. Very structural.

The strategy pattern needn't really have two sides. It's meant to be a removable/replacable bit consumed or queried by a class. It tends not to 'make things work' but to create an abstraction that represents a strategy or configuration about how/what to do. Very behavioral.
Antheus
Antheus
Quote:
Original post by ukdeveloper

Looking over some past papers and one of the questions asks to compare the strategy and bridge patterns and clearly state the difference between them.


I just hope that these were some internet papers or blogs or forum posts, and not some school curriculum.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.