🎮 Strategy Design Pattern
The Strategy Pattern is a behavioral design pattern that allows you to define a family of algorithms, encapsulate them, and make them interchangeable at runtime.
In other words:
-
Instead of hardcoding behavior, you can swap it dynamically.
-
You encapsulate each “strategy” (algorithm/behavior) in its own class.
-
The client (game object) doesn’t need to know how the strategy works — just that it uses one.
Escape Room Example Scenario
Section titled “Escape Room Example Scenario”Imagine you have puzzles in your escape room. Different puzzles might require different solving strategies:
-
Cracking a cipher puzzle.
-
Solving a math riddle.
-
Picking a lock.
Define the Strategy Interface
Section titled “Define the Strategy Interface”// Strategy interfacepublic interface PuzzleSolvingStrategy { void solve();}Implement Concrete Strategies
Section titled “Implement Concrete Strategies”// Strategy 1: Cipher puzzlepublic class CipherPuzzleStrategy implements PuzzleSolvingStrategy { @Override public void solve() { System.out.println("Decoding the secret cipher text..."); }}
// Strategy 2: Math riddle puzzlepublic class MathRiddleStrategy implements PuzzleSolvingStrategy { @Override public void solve() { System.out.println("Solving the math riddle with logic..."); }}
// Strategy 3: Lock picking puzzlepublic class LockPickingStrategy implements PuzzleSolvingStrategy { @Override public void solve() { System.out.println("Carefully picking the lock..."); }}Create the Context
Section titled “Create the Context”The context is the object that uses a strategy.
In our game, the Puzzle class can hold a strategy and delegate solving to it.
public class Puzzle { private PuzzleSolvingStrategy strategy;
// Inject strategy at runtime public void setStrategy(PuzzleSolvingStrategy strategy) { this.strategy = strategy; }
public void attemptSolve() { if (strategy == null) { System.out.println("No solving strategy selected!"); } else { strategy.solve(); } }}Using the Strategy in the Game
Section titled “Using the Strategy in the Game”public class EscapeRoomGame { public static void main(String[] args) { Puzzle puzzle = new Puzzle();
// Switch strategies at runtime puzzle.setStrategy(new CipherPuzzleStrategy()); puzzle.attemptSolve(); // "Decoding the secret cipher text..."
puzzle.setStrategy(new MathRiddleStrategy()); puzzle.attemptSolve(); // "Solving the math riddle with logic..."
puzzle.setStrategy(new LockPickingStrategy()); puzzle.attemptSolve(); // "Carefully picking the lock..." }}Why is this Useful in a Game?
Section titled “Why is this Useful in a Game?”-
Flexibility: You can easily add new puzzle-solving strategies (e.g., “Laser Reflection Puzzle”) without changing the core
Puzzleclass. -
Reusability: Each strategy is independent and can be reused across multiple puzzles.
-
Maintainability: Changes in one solving algorithm don’t affect others.
Next Step Ideas 💡
Section titled “Next Step Ideas 💡”-
Make players choose a strategy (input from the console).
-
Assign puzzles dynamically with random strategies.
-
Combine Strategy with Factory Method to generate puzzles.
✅ Key Takeaway:
The Strategy Pattern lets you encapsulate behaviors (solving puzzles) and swap them dynamically, making your escape room game extensible and maintainable.