Current Location: Home> Latest Articles> In-depth Exploration of Strategy Pattern in PHP: A Design Pattern for Flexible Code

In-depth Exploration of Strategy Pattern in PHP: A Design Pattern for Flexible Code

gitbox 2025-07-02

Concept and Use Cases of Strategy Pattern

The strategy pattern is a behavioral design pattern that defines a family of interchangeable algorithms and decouples the selection and usage of the algorithm from its implementation.

In PHP, the strategy pattern can help us deal with classes that exhibit different behaviors. By encapsulating different algorithms, it allows us to switch between algorithm implementations at runtime, enhancing the flexibility and scalability of the code.

Using Strategy Pattern for Different Sorting Algorithms

Let's assume we need to implement a sorting class where we can choose different sorting algorithms based on the requirements.

Creating a Strategy Interface

First, we create a sorting strategy interface that defines a sort() method:


interface SortStrategy {
    public function sort(array $data): array;
}

Implementing Concrete Sorting Strategies

Next, we implement concrete sorting strategies, using bubble sort and quick sort as examples:


class BubbleSort implements SortStrategy {
    public function sort(array $data): array {
        // Bubble sort
        // ...
    }
}

class QuickSort implements SortStrategy {
    public function sort(array $data): array {
        // Quick sort
        // ...
    }
}

Creating a Sorting Context Class

Now, let's create a sorting context class that holds a sorting strategy object and provides a method for switching strategies:


class SortContext {
    private $strategy;

    public function __construct(SortStrategy $strategy) {
        $this->strategy = $strategy;
    }

    public function setStrategy(SortStrategy $strategy) {
        $this->strategy = $strategy;
    }

    public function sort(array $data): array {
        return $this->strategy->sort($data);
    }
}

Using the Strategy Pattern for Sorting

Now, we can use the strategy pattern for sorting. Let's choose the bubble sort strategy:


$data = [5, 2, 7, 1, 4];
$context = new SortContext(new BubbleSort());
$sortedData = $context->sort($data);

By calling the sort() method, the $data array will be sorted using the bubble sort algorithm.

Switching Sorting Strategies

If we want to switch to the quick sort algorithm, we can simply call the setStrategy() method to change the strategy:


$context->setStrategy(new QuickSort());
$sortedData = $context->sort($data);

Now, $sortedData will be sorted using the quick sort algorithm.

Advantages and Disadvantages of Strategy Pattern

Advantages

The strategy pattern enables free switching of algorithms. By encapsulating concrete algorithms, it decouples the selection and usage of the algorithm from its implementation.

It also adheres to the Open/Closed Principle, making it easy to add new algorithms.

Disadvantages

The strategy pattern increases the number of classes, as we need to create multiple classes to implement different algorithms.

For simpler use cases, the strategy pattern might introduce unnecessary complexity, making the code more complicated.

Conclusion

The strategy pattern is a design pattern that allows the free switching of algorithms by abstracting them out, decoupling their selection and usage from their implementation.

In PHP, the strategy pattern can help us manage classes with different behaviors, making the code more flexible and scalable.

Strategy pattern is widely applied in real-world projects, such as choosing payment methods, file storage options, and more.

Though it introduces more classes, the strategy pattern offers better extensibility and maintainability. Therefore, when designing, we should consider whether to use the strategy pattern based on the specific use case.