Hey, I have just reduced the price for all products. Let's prepare our programming skills for the post-COVID era. Check it out »
Strategy

Strategy in PHP

Strategy is a behavioral design pattern that turns a set of behaviors into objects and makes them interchangeable inside original context object.

The original object, called context, holds a reference to a strategy object and delegates it executing the behavior. In order to change the way the context performs its work, other objects may replace the currently linked strategy object with another one.

Usage of the pattern in PHP

Complexity:

Popularity:

Usage examples: The Strategy pattern is often used in PHP code, especially when it’s necessary to switch algorithms at runtime. However, the pattern has a strong competitor represented by anonymous functions, introduced in PHP 5.3 in 2009.

Identification: Strategy pattern can be recognized by a method that lets nested object do the actual work, as well as the setter that allows replacing that object with a different one.

Strategy in Other Languages

Design Patterns: Strategy in Java Design Patterns: Strategy in C# Design Patterns: Strategy in C++ Design Patterns: Strategy in Python Design Patterns: Strategy in Ruby Design Patterns: Strategy in Swift Design Patterns: Strategy in TypeScript Design Patterns: Strategy in Go