Skip to content

Latest commit

 

History

History

Strategy

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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

Context.php

Comparator.php

Comparator.php

DateComparator.php

DateComparator.php

IdComparator.php

IdComparator.php

Test

Tests/StrategyTest.php

Tests/StrategyTest.php