participate


Patterns & OO Design - DP101: Swiss navy knife is an example of Strategy pattern
<<   Back to Forum  |   Give us Feedback
This topic has 6 replies on 1 page.
PaulPhuoc
Posts:177
Registered: 5/23/03
DP101: Swiss navy knife is an example of Strategy pattern   
Dec 8, 2004 8:12 PM

 
Hi,

Is this a good real life example? Thanks.

Swiss navy knife is an example of Strategy pattern in the sense the knife handle is the context class whereas the blade is the Strategy interface and different shapes of the blade are concrete strategy classes.
 
spieler
Posts:815
Registered: 12/29/00
Re: DP101: Swiss navy knife is an example of Strategy pattern   
Dec 9, 2004 12:58 AM (reply 1 of 6)  (In reply to original post )

 
I think a power drill + drills would be a better example.
the machine is the context class
the end of the drills that fit into the machine is the strategy interface.
the drills are strategy classes.

They do all the same: making holes. But depending on the material you use different drills / strategies
 
hookomjj
Posts:218
Registered: 1/25/02
Re: DP101: Swiss navy knife is an example of Strategy pattern   
Dec 9, 2004 7:21 AM (reply 2 of 6)  (In reply to #1 )

 
heheh, the drill sounds more like the adapter, not a strategy... you have drill bits adapt the tool for different applications-- screws, drilling, etc.

Strategy pattern happens internally... in your knife example, it would have to be a knife that doesn't change externally, but simply behaves differently. I would be like a hybrid car, that switches from the electric to gas strategy based on the driving conditions. I must admit my example isn't as 'cool' as a swiss army knife.
 
jvaudry
Posts:790
Registered: 12/8/02
Re: DP101: Swiss navy knife is an example of Strategy pattern   
Dec 17, 2004 1:06 PM (reply 3 of 6)  (In reply to original post )

 
Swiss army knife is an anti-pattern... but you'll learn that next year.
 
Peter-Lawrey
Posts:6,487
Registered: 5/5/04
Re: DP101: Swiss navy knife is an example of Strategy pattern   
Dec 17, 2004 2:25 PM (reply 4 of 6)  (In reply to #3 )

 
I would describe a Swiss army knife as a factory method. It hold many creator methods for different tools (you pull them out) The implementations suggest you don't care what you get as long as it does the job. The implementation is also hidden until you open it.

I would suggest a simple knife is an example of Strategy pattern. Each knife can cut, but whether it is a bread knife, steak knife, paring knife etc changes the way it does it, but all implement cut()
 
Adeodatus
Posts:1,851
Registered: 9/18/04
Re: DP101: Swiss navy knife is an example of Strategy pattern   
Dec 17, 2004 4:26 PM (reply 5 of 6)  (In reply to #4 )

 
new Knife().cut(Cheese.getInstance("Limburger"));
 
Herko_ter_Horst
Posts:2,338
Registered: 8/28/03
Re: DP101: Swiss navy knife is an example of Strategy pattern   
Dec 20, 2004 1:56 AM (reply 6 of 6)  (In reply to #5 )

 
I wouldn't use a Swiss navy knife as a pattern, because it's obviously a fake.

The Swiss are hardly likely to maintain a navy on the Geneva Lake, not to mention supply knives to those non-existing sailors.
 
This topic has 6 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums