• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Strategy pattern and bridge pattern

 
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
reply
    Bookmark Topic Watch Topic
  • New Topic