Strategy Pattern

noun

say it: “STRAT-uh-jee PAT-ern” /ˈstrætədʒi ˈpætərn/

A design pattern where interchangeable classes each hold one way of doing a task.

What is Strategy Pattern?

The Strategy pattern puts each way of doing something in its own class and lets you swap them. A map app can switch between driving, walking and cycling route strategies.

public interface IRouteStrategy { Route Plan(Place from, Place to); }

var planner = new RoutePlanner(new WalkingRoute());
planner.Strategy = new CyclingRoute(); // swap at run time

In a sentence

  • “Shipping cost uses the Strategy pattern: standard, express and pickup are swappable classes.”

Level: Freelancer · More OOP terms at this level

Updated