© Kelt Dockins 2017

Kelt Dockins, Design Patterns in PHP and Laravel, 10.1007/978-1-4842-2451-9_24

24. Strategy

Kelt Dockins

(1)Dolph, Arkansas, USA

$> git checkout strategy

Intent

Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.1

Applications

The strategy pattern is useful when you have algorithms that share the same public interface but work differently under the hood. The strategy pattern generally relies on composition to pass in the varying algorithms. The Laravel encryption 2component uses the same methods to encrypt and decrypt messages. The underlying cipher algorithm can change, though. This is a loose implementation of the strategy ...

Get Design Patterns in PHP and Laravel now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.