codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎭

Design Patterns

28 / 100 topics
19Introduction to Behavioral Patterns20Chain of Responsibility Pattern21Command Pattern22Interpreter Pattern23Iterator Pattern24Mediator Pattern25Memento Pattern26Observer Pattern27State Pattern28Strategy Pattern29Template Method Pattern30Visitor Pattern33Practical Exercises for Behavioral Patterns
Tutorials/Design Patterns/Strategy Pattern
🎭Design Patterns

Strategy Pattern

Updated 2026-05-15
10 min read

Strategy Pattern

The Strategy pattern is one of the behavioral design patterns that focuses on defining a family of algorithms, encapsulating each one, and making them interchangeable. This allows algorithms to vary independently from clients that use them. It promotes flexibility and reusability in your code.

Introduction

Imagine you're developing a game where characters can move around in different ways (e.g., walking, running, flying). You might start by implementing these movement behaviors directly within the character classes. However, as your game grows, adding new movement types or modifying existing ones becomes cumbersome and tightly coupled with character classes.

The Strategy pattern helps solve this problem by encapsulating each movement behavior into a separate class. Characters can then use different strategies (movement behaviors) interchangeably at runtime without changing their core structure.

Concept

The key components of the Strategy pattern are:

  1. Context: This is the class that uses a strategy. It maintains a reference to a Strategy object and delegates some behavior to it.
  2. Strategy: This is an interface or abstract class defining the common methods for all concrete strategies.
  3. Concrete Strategies: These are classes implementing the Strategy interface, each providing a specific algorithm.

The Strategy pattern allows you to define a family of algorithms, encapsulate each one, and make them interchangeable. It promotes flexibility and reusability in your code.

Examples

Let's walk through an example where we implement the Strategy pattern for different movement behaviors in a game.

Step 1: Define the Strategy Interface

First, we define a MovementStrategy interface that all concrete strategies will implement.

JavaScript
1interface MovementStrategy {
2 move(): void;
3}

Step 2: Implement Concrete Strategies

Next, we create concrete strategy classes for different movement behaviors.

JavaScript
1class WalkMovement implements MovementStrategy {
2 move() {
3 console.log("Walking...");
4 }
5}
6
7class RunMovement implements MovementStrategy {
8 move() {
9 console.log("Running quickly...");
10 }
11}
12
13class FlyMovement implements MovementStrategy {
14 move() {
15 console.log("Flying high in the sky...");
16 }
17}

Step 3: Create the Context Class

The Character class acts as the context and uses a MovementStrategy.

JavaScript
1class Character {
2 private movementStrategy: MovementStrategy;
3
4 constructor(strategy: MovementStrategy) {
5 this.movementStrategy = strategy;
6 }
7
8 setMovementStrategy(strategy: MovementStrategy) {
9 this.movementStrategy = strategy;
10 }
11
12 move() {
13 this.movementStrategy.move();
14 }
15}

Step 4: Use the Strategy Pattern

Now, we can create characters and change their movement strategies at runtime.

JavaScript
1const character = new Character(new WalkMovement());
2character.move(); // Output: Walking...
3
4character.setMovementStrategy(new RunMovement());
5character.move(); // Output: Running quickly...
6
7character.setMovementStrategy(new FlyMovement());
8character.move(); // Output: Flying high in the sky...

Output

Output
Walking...
Running quickly...
Flying high in the sky...

What's Next?

In this tutorial, we explored the Strategy pattern and how it allows you to encapsulate algorithms and make them interchangeable. In the next section, we will delve into another behavioral pattern called the Template Method Pattern.

Stay tuned for more insights into design patterns and how they can help you write better, more maintainable code!


PreviousState PatternNext Template Method Pattern

Recommended Gear

State PatternTemplate Method Pattern