Strategy Pattern - PHP Design Patterns

Strategy Pattern - PHP Design Patterns
PHP Strategy Pattern: Strategy lets the algorithm vary independently from clients that use it. Strategy is one of the patterns included in the influential book Design Patterns by Gamma et al. that popularized the concept of using patterns in software design.

For instance, a class that performs validation on incoming data may use a strategy pattern to select a validation algorithm based on the type of data, the source of the data, user choice, or other discriminating factors. These factors are not known for each case until run-time, and may require radically different validation to be performed. The validation strategies, encapsulated separately from the validating object, may be used by other validating objects in different areas of the system (or even different systems) without code duplication.

The essential requirement in the programming language is the ability to store a reference to some code in a data structure and retrieve it. This can be achieved by mechanisms such as the native function pointer, the first-class function, classes or class instances in object-oriented programming languages, or accessing the language implementation's internal storage of code via reflection. See Wikipedia

PHP Design Patterns #4

Copyright (c) 2014 Rodrigo Silveira http://www.easylearntutorial.com

RRelated Posts

Adapter Pattern - PHP Design Patterns

Adapter pattern PHP: An adapter helps two incompatible interfaces to work together. This is the real world definition for an adapter. The adapter design pattern is used when you want two different

Strategy Pattern – Design Patterns (ep 1)

Video series on Design Patterns for Object Oriented Languages. This time we look at Strategy Pattern. ► The playlist https://www.youtube.com/playlist?list=PLrhzvIcii6GNjpARdnO4ueTUAVR9eMBpc ►

Factory Pattern - PHP Design Patterns

In class-based programming, the factory method pattern is a creation pattern which uses factory methods to deal with the problem of creating objects without specifying the exact class of object that

Dependency Injection

This week, we're going to talk about the topic of Dependency Injection in Object oriented code (specifically PHP). You don't need a fancy container to do it, it's actually quite simple to do


  • Chris Scholes
    How would this be better than using a switch statement in the constructor and just have one class?
  • Александр Мотузов
    That's not a strategy pattern
  • Nguai al
    This pattern could be implemented without knowing that I actually implemented it. This sounds like very logical thing to do.
  • Constantin Galbenu
    This is not strategy pattern.
  • Kiên Trần
    Thank you
  • Jason B
    What a great explanation thank you.
  • Austin White
    Make sure you "implement" the interface: class QuickSort implements SortStrategy {. Thanks for the help!
  • Rodrigo Silveira
    Of course, the only reason for creating that interface was to have each strategy class implement it