Tier 4 Series 302

302B: The Strategy Switch

Pilot Record
Student Profile
"If your aircraft needs to switch between "Hover Mode", "Jet Mode", and "Submersible Mode", a giant if-else statement is a structural failure. It creates a Monolith script that breaks every time you add a new mode. A Navigator uses Strategies to plug in brains on the fly."

The Concept: The Strategy Pattern

The **Strategy Pattern** defines a family of algorithms (behaviors) and makes them interchangeable.

* **Interface:** `IMovementStrategy` defines the contract (e.g., `Move()`).
* **Concrete Strategies:** `HoverFlight`, `JetPropulsion`, `UnderwaterDrive`.
* **Context:** The Player script just holds a reference to the *current* strategy.
Red Flag Detected

The AI Trap: "The Switch Monolith"

You ask the AI: "Handle movement for flying, walking, and swimming."

// AI-Generated Code: The "God" Method
void Move() {
    // Audit Fail: This function will grow to 500 lines.
    if (mode == "Fly") { ... }
    else if (mode == "Walk") { ... }
    else if (mode == "Swim") { ... }
}

This is "Cyclomatic Complexity." Adding a fourth mode requires editing the core script, risking bugs in the other three modes.

Elite Telemetry

Research shows "Elite" teams achieve 15% faster lead times by keeping AI on a "very tight leash."

  • Small Batches Solving one problem at a time prevents logic drift.
  • Modular Design Localizing the "blast radius" of AI changes.
  • Tight Loops Rapid iteration with constant code review.

The Navigator's Correction

Corrective Protocol
// Corrected: Modular & Swappable
public interface IMovementStrategy { void Move(Transform t); }

public class Player : MonoBehaviour {
    private IMovementStrategy _strategy;
    
    public void SetMode(IMovementStrategy newMode) {
        _strategy = newMode;
    }

    void Update() => _strategy.Move(transform);
}
Your Pilot Command
> A skilled Navigator directs the AI to use Strategies.
Next Mission
The Drone Factory