Strategy Pattern

Intent
Select algorithms on-the-fly at runtime.
Motivation
Write different loggers
Implementation
  • a strategy is a variable whose value is a function or method
  • No need for separate classes that differ in a few well understood ways
  • Strategy objects are not a no-no, but there is no inherent need for separate classes for each strategy instance
Examples
with functions
$logger = "a_logger_function"; $logger($message);
with objects
$strategy = "a_logger_class"; $log_object = new $strategy; $log_object->log(message);
Powered by Drupal, an open source content management system