Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
Tests
uml Added Behavioral pattern UMLs Jul 31, 2014
ComparatorInterface.php
Context.php
DateComparator.php
IdComparator.php
README.rst [Behavioral/Strategy] Fixing files inclusion. Nov 15, 2017

README.rst

Strategy

Terminology:

  • Context
  • Strategy
  • Concrete Strategy

Purpose

To separate strategies and to enable fast switching between them. Also this pattern is a good alternative to inheritance (instead of having an abstract class that is extended).

Examples

  • sorting a list of objects, one strategy by date, the other by id
  • simplify unit testing: e.g. switching between file and in-memory storage

UML Diagram

Alt Strategy UML Diagram

Code

You can also find this code on GitHub

Context.php

.. literalinclude:: Context.php
   :language: php
   :linenos:

ComparatorInterface.php

.. literalinclude:: ComparatorInterface.php
   :language: php
   :linenos:

DateComparator.php

.. literalinclude:: DateComparator.php
   :language: php
   :linenos:

IdComparator.php

.. literalinclude:: IdComparator.php
   :language: php
   :linenos:

Test

Tests/StrategyTest.php

.. literalinclude:: Tests/StrategyTest.php
   :language: php
   :linenos: