aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Strategy pattern and bridge pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Strategy pattern and bridge pattern" Watch "Strategy pattern and bridge pattern" New topic
Author

Strategy pattern and bridge pattern

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
http://www.giantflyingsaucer.com/blog/?p=1072

I read this tutorial and still can not understadn how strategy pattern works, It just define an interface...Defend, and defines classes who use that interface!

What does it mean?

I also read this tutorial for bridge pattern:

http://www.allapplabs.com/java_design_patterns/bridge_pattern.htm

Just create interface an other classes use that, It makes no sense for me,

what do these patterns?And what are differences?
Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
The strategy pattern allows you to make changes to the behavior of an object at runtime. Using the same anology as the article you could potently define a Navy class that defends the high seas, but if attacked by submarines the Navy is defensless since it is not defending below the surface. Now you have to change the Navy class to counter this threat, which obviously can't be done at runtime. Instead of this you can make the defensive orders independent of the Navy class and insert the orders at runtime to counter threats.

The Navy class is unchanged but the instance is able to counter the new threat.

The bridge structures seperates the interface from it's implementation, which is what Interfaces should usually do. The interface Switch can have switchOn() and switchOff() functions but their implementation is left to the classes that use them. There are many types of switches, for many things and their implementation varies, changes in implementation should not affect the interface.
Ran Pleasant
Ranch Hand

Joined: Jan 16, 2003
Posts: 75
abalfazl hossein wrote:...still can not understadn how strategy pattern works...



I'm giving a business example using different ways of calculating interest rates. Interest rates can be calculated using a give number of days in a month and a given number of days in a years, such as 30/360, 30/364, and 30/365. If we put these different calculations into different objects we might first create an abstract super class name InterestCalculation and then create the following subclasses: InterestCalculation30/360, InterestCalculation30/364, and InterestCalculation30/365. Now we can design a loan object that has a field to hold an object of type InterestCalculation which will handled calculating the interest of the loan. Of course different loans might take use different interest calcualtions. To handle this we just plug-in the correct subclass instance of InterestCalculation into the instance of a loan. If we didn't use the Strategy Pattern then we would probably have to have some type of big IF/ELSE IF statement in our loan class to handle the different calculations. Any time you see a number of If/Else If statements in an object it is probably in need of the Strategy pattern.

Ran

Ran
Arnold Reuser
Ranch Hand

Joined: Nov 20, 2003
Posts: 196
To clarify both the strategy pattern, the bridge pattern and the difference between them :

Metaphors used to explan the strategy pattern :
• How do you sort the contact list in your mobile phone? By surname, by family name or maybe even by company.
These are different strategies to sort your contact list.
• Modes of transportation to an airport is an example of a Strategy.
Several options exist such as driving one's own car, taking a taxi, an airport shuttle, a city bus, or a limousine service.
For some airports, subways and helicopters are also available as a mode of transportation to the airport.
Any of these modes of transportation will get a traveler to the airport, and they can be used interchangeably.
The travel agency will, on the behalf of the traveler, pick the type of transportation based on based on tradeoffs between cost, convenience, and time.

Metaphors used to explain the bridge pattern :
• The switch of the fan. The switch is the interface and the actual implementation is the Running of the fan once its switched-on.
Still, both the switch and the fan are independent of each other. Another switch can be plugged in for the fan and or can be connected to light bulb.


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strategy pattern and bridge pattern