mirror of
https://github.com/codeguy/php-the-right-way.git
synced 2025-08-09 15:36:36 +02:00
Added the strategy pattern to the Design Patterns page
This commit is contained in:
@@ -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 %}
|
||||
<?php
|
||||
|
||||
interface OutputInterface
|
||||
{
|
||||
public function load();
|
||||
}
|
||||
|
||||
class OutputSerializedArray implements OutputInterface
|
||||
{
|
||||
public function load()
|
||||
{
|
||||
return serialize($array_of_data);
|
||||
}
|
||||
}
|
||||
|
||||
class OutputJsonString implements OutputInterface
|
||||
{
|
||||
public function load()
|
||||
{
|
||||
return json_encode($array_of_data);
|
||||
}
|
||||
}
|
||||
|
||||
class OutputArray implements OutputInterface
|
||||
{
|
||||
public function load()
|
||||
{
|
||||
return $array_of_data;
|
||||
}
|
||||
}
|
||||
{% endhighlight %}
|
||||
|
||||
By encapsulating the above algorithms you are making it nice and clear in your code that other developers can easily
|
||||
add new output types without affecting the client code.
|
||||
|
||||
You will see how each concrete 'output' class implements an OutputInterface - this serves two purposes, primarily it
|
||||
provides a simple contract which must be obeyed by any new concrete implementations. Secondly by implementing a common
|
||||
interface you will see in the next section that you can now utilise [Type Hinting](http://php.net/manual/en/language.oop5.typehinting.php) to ensure that the client which is utilising these behaviours is of the correct type in
|
||||
this case 'OutputInterface'.
|
||||
|
||||
The next snippet of code outlines how a calling client class might use one of these algorithms and even better set the
|
||||
behaviour required at runtime:
|
||||
{% highlight php %}
|
||||
<?php
|
||||
|
||||
class SomeClientClass
|
||||
{
|
||||
private $output;
|
||||
|
||||
public function __construct(){}
|
||||
|
||||
public function setOutput(OutputInterface $output_type)
|
||||
{
|
||||
$this->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 %}
|
||||
<?php
|
||||
|
||||
$client = new SomeClientClass();
|
||||
|
||||
// Want an array?
|
||||
$client->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
|
||||
|
Reference in New Issue
Block a user