diff --git a/pages/Design-Patterns.md b/pages/Design-Patterns.md index e82a61d..f89234c 100644 --- a/pages/Design-Patterns.md +++ b/pages/Design-Patterns.md @@ -149,6 +149,98 @@ application, as the object using the shared or global resource requires no knowl * [Singleton pattern on Wikipedia](https://en.wikipedia.org/wiki/Singleton_pattern) +## Strategy + +With the strategy pattern you encapsulate specific families of algorithms allowing the client class responsible for +instantiating a particular algorithm to have no knowledge of the actual implementation. +There are several variations on the strategy pattern, the simplest of which is outlined below: + +This first code snippet outlines a family of algorithms; you may want a serialized array, some JSON or maybe +just an array of data: +{% highlight php %} +output = $output_type; + } + + public function loadOutput() + { + return $this->output->load(); + } +} +{% endhighlight %} + +The calling client class above has a private property which must be set at runtime and be of type 'OutputInterface' +once this property is set a call to loadOutput() will call the load() method in the concrete class of the output type +that has been set. +{% highlight php %} +setOutput(new OutputArray()); +$data = $client->loadOutput(); + +// Want some JSON? +$client->setOutput(new OutputJsonString()); +$data = $client->loadOutput(); + +{% endhighlight %} + +* [Strategy pattern on Wikipedia](http://en.wikipedia.org/wiki/Strategy_pattern) + ## Front Controller The front controller pattern is where you have a single entrance point for you web application (e.g. index.php) that