For the complete documentation index, see llms.txt. This page is also available as Markdown.

Strategy Pattern

The Strategy Pattern is a Behavioral Design Pattern that allows you to define multiple algorithms (strategies) and switch between them dynamically at runtime without changing the main code.

It helps follow the Open/Closed Principle:

  • Open for extension

  • Closed for modification


Real-Life Example

Think about a payment system:

A user can pay using:

  • Credit Card

  • PayPal

  • Stripe

  • Mobile Banking

Instead of writing large if-else conditions, we create separate payment strategies.


Problem Without Strategy Pattern

class PaymentService {

    public function pay($method, $amount) {

        if($method == 'paypal') {
            return "Paid $amount using PayPal";
        }

        if($method == 'stripe') {
            return "Paid $amount using Stripe";
        }

        if($method == 'bkash') {
            return "Paid $amount using bKash";
        }
    }
}

Problems

❌ Huge if-else blocks ❌ Hard to maintain ❌ Difficult to add new payment methods ❌ Violates Open/Closed Principle


Strategy Pattern Solution

Step 1: Create Strategy Interface

This interface defines a common behavior for all payment methods.


Step 2: Create Concrete Strategies

PayPal Strategy


Stripe Strategy


bKash Strategy


Step 3: Create Context Class

The Context class uses a strategy object.


Step 4: Use Strategy Dynamically

Pay with PayPal

Output


Pay with Stripe


Pay with bKash


Full Working Example


UML Structure


Advantages

✅ Easy to Add New Strategy

Add new payment methods without modifying existing code.


✅ Cleaner Code

Avoids huge conditional statements.


✅ Follows SOLID Principles

Especially:

  • Open/Closed Principle

  • Single Responsibility Principle


✅ Runtime Flexibility

Strategies can change dynamically.


Real Laravel Examples

Feature
Strategy Used

Cache Drivers

Redis/File/Database

Payment Gateways

Stripe/PayPal

Notification Channels

Mail/SMS/Slack

Queue Drivers

Redis/SQS/Database

Authentication Guards

Session/API


Another Example: Sorting Strategy


When to Use Strategy Pattern

Use Strategy Pattern when:

✅ Multiple algorithms exist ✅ You want to switch behavior dynamically ✅ Large if-else blocks exist ✅ Logic changes frequently ✅ Need clean and scalable architecture


When NOT to Use

❌ Only one algorithm exists ❌ Logic is very simple ❌ Too many small classes become unnecessary


Bridge Pattern vs Strategy Pattern

Both Bridge and Strategy patterns use composition instead of inheritance, so many developers confuse them. But their purposes are completely different.


Main Difference

Pattern
Purpose

Bridge Pattern

Separates abstraction from implementation

Strategy Pattern

Changes behavior/algorithm dynamically


Simple Understanding

Bridge Pattern

Used when you have:

  • Multiple abstractions

  • Multiple implementations

And you want to avoid class explosion.

Focus:

👉 Structure and scalability


Strategy Pattern

Used when you have:

  • Multiple algorithms or behaviors

And you want to switch them dynamically.

Focus:

👉 Behavior selection


Real-Life Analogy

Bridge Pattern Example

Think of:

  • Remote Controls

  • TVs

Different remotes can work with different TVs.

Bridge separates:

  • Abstraction = Remote

  • Implementation = TV


Strategy Pattern Example

Think of payment methods:

  • PayPal

  • Stripe

  • bKash

You choose one strategy dynamically.


1. Strategy Pattern Example

Goal

Change payment behavior dynamically.


Strategy Interface


Concrete Strategies


Context Class


Usage


Strategy Pattern Structure


Strategy Pattern Purpose

✅ Swap algorithms ✅ Dynamic behavior ✅ Remove if-else conditions ✅ Encapsulate business rules


2. Bridge Pattern Example

Goal

Separate abstraction from implementation.


Problem Without Bridge

Huge class explosion.


Bridge Solution


Implementation Interface


Concrete Implementations



Abstraction Class


Refined Abstraction


Usage


Bridge Pattern Structure


Bridge Pattern Purpose

✅ Avoid class explosion ✅ Independent scalability ✅ Separate abstraction from implementation ✅ Better flexibility


Key Differences

Feature
Strategy
Bridge

Type

Behavioral Pattern

Structural Pattern

Purpose

Change behavior

Separate abstraction & implementation

Focus

Algorithms

Structure

Runtime Change

Yes

Usually not primary goal

Composition Used For

Behavior

Architecture

Main Problem Solved

Large conditional logic

Class explosion

Example

Payment methods

Remote + TV


Easy Interview Answer

Strategy Pattern

Strategy Pattern changes object behavior dynamically by selecting different algorithms at runtime.


Bridge Pattern

Bridge Pattern separates abstraction from implementation so both can evolve independently.

Last updated