Tuesday, December 23, 2014

Strategy Pattern

Strategy Pattern suggest following design strategy:

1. Define a family of algorithms (an interface)
2. Encapsulate each one and make them interchangeable (composition)
3. The idea is to let the algorithm vary independently from clients that use it.

Principles behind Strategy Pattern:

1. Identify the aspects of your application that vary and separate them from what stays the same.
    - take the parts that vary and encapsulate them, so that later you can alter or extend the parts that vary without affecting those that don't.

2. Program to an interface, not an implementation.
    - "program to an interface" really means "program to a supertype".

3. Favor composition over inheritance.
    - this allows you to change behavior at runtime as long as the object you are composing with implements the correct behavior interface.

What problems are solved by Strategy Pattern?

1. Changing Code in sub-classes
2. Duplicate Code

No comments:

Post a Comment