The Strategy Pattern defines a family of algorithms,
encapsulates each one, and makes them interchangeable.
Strategy lets the algorithm vary independently from
clients that use it.
The Duck class that declares two reference variables for the behavior interface types.
All duck subclasses inherit these.
The interface that all flying behavior classes implement.
And some implementations of this interface:
The quack behavior interface is similar to the fly behavior interface.
The following code snippets show two implementations of that interface:
A subclass of Duck inherits the quackBehavior and flyBehavior instance variables.
If performQuack() is called on the quackBehavior member the responsibility for the quack
is delegated to the Quack behavior in the following example.
The following main application shows that with the strategy pattern it is
possible to change the behavior at runtime.
Starting this application results in the following output.
Comments