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.
![[Screenshot 2023-09-03 at 11.55.50 PM.png]]
![[Screenshot 2023-09-01 at 4.42.51 PM.png]]
The strategy pattern is used in the above design to implement the various behaviors of the ducks that are created. Therefore, the duck behavior has been encapsulated into its own set of classes that can be easily expanded and changed, even at runtime if needed.
### Identify the aspects of your application that vary and separate them from what stays the same.
In other words, if you have got some aspect of your code that is changing say with every new requirement, then you know you've got a behavior that needs to be pulled out and separated from all the stuff that doesn't change. Encapsulating the parts that change helps us later alter or extend the parts that vary without affecting those that don't.
### Program to an interface, not an implementation
Interfaces allow us to exploit polymorphism by programming to a supertype so that the actual runtime object isn't locked into the code.
### favor composition over inheritance
Creating systems using composition gives us a lot more flexibility. Not only does it let us encapsulate a family of algorithms into their own set of classes, but it also lets us change behavior at runtime as long as the object we're composing with implements the correct behavior interface.