The strategy method works well with the template method.
The template method specifies requirements of the class. The strategy method allows you to use objects to defined the behaviours, and to define them at run-time using dependency injection.
class CRUDTemplate
{
protected $updater;
protected $creator;
protected $deletor;
protected $loader;
public function __construct(IUpdate $updater, ICreator $creator, IDeletor $delete, ILoader $loader)
{
$this->updater = $updater;
//...snipped...
}
// sample template function; others are create, delete, update
public function load($id)
{
$this->content = $this->loader->load($id);
}
}
While the sample code just shows the template functions calling each of the strategy, more can be done in a specialized overloaded version of the template functions, such as validation of input, initisation and such. And after all, something has to call the strategies.
The pros is that, for the example above, you can use different different set of strategies for different conditions (CRUD may not be the best example here). So if you want to change the way the data is output, but retain how it is saved/created/loaded,you just need to change the output strategy.
The cons is that you have to make sure the strategies are for the right context, as there is no type checking to ensure the strategies you pass in are valid (any will work as long as it is of the expectd base class). Oh yeah, the CRUDTemplate could derive from a base class too.
For data, in PHP I find that the array works best for passing complex parameters. Of course, you have to do your validation of the array