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.
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
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.
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()