The strategy design pattern encapsulates an algorithm and lets you choose the right one. It is really pretty simple. For example, going back to our family example, everyone loves to watch a movie over a weekend. However, dads love action flicks, moms love romantic movies, and kids being kids, love cartoons.
In Java, we would express the strategy pattern as follows:
public interface MovieGenre { public String describeABit(); } public class ActionFlick implements MovieGenre { @Override public String describeABit() { return "guns firing, fights, wild chases etc."; } } public class Animation implements MovieGenre { @Override public String describeABit() { return "cartoons! What else?"; } } public class RomanticMovie implements MovieGenre { @Override public String describeABit() { return "some romantic flick"; } }
Here, our family members are input as a class:
public abstract class FamilyMember { private final String name; private...