Created
January 4, 2017 13:05
-
-
Save rederlo/c31803a33e6c39f3e612df8caf974d46 to your computer and use it in GitHub Desktop.
Exemplo Strategy design patterns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface OutputInterface | |
{ | |
public function load($arrayOfData); | |
} | |
class SerializedArrayOutput implements OutputInterface | |
{ | |
public function load($arrayOfData) | |
{ | |
return serialize($arrayOfData); | |
} | |
} | |
class JsonStringOutput implements OutputInterface | |
{ | |
public function load($arrayOfData) | |
{ | |
return json_encode($arrayOfData); | |
} | |
} | |
class ArrayOutput implements OutputInterface | |
{ | |
public function load($arrayOfData) | |
{ | |
return $arrayOfData; | |
} | |
} | |
class Client | |
{ | |
private $output; | |
public $name; | |
public function setOutput(OutputInterface $outputType) | |
{ | |
$this->output = $outputType; | |
} | |
public function loadOutput() | |
{ | |
return $this->output->load(get_object_vars($this)); | |
} | |
} | |
$client = new Client(); | |
$client->name = "Ederlo Rodrigo"; | |
// Quer um array? | |
$client->setOutput(new ArrayOutput()); | |
$data = $client->loadOutput(); | |
var_dump($data); | |
// Quer um JSON? | |
$client->setOutput(new JsonStringOutput()); | |
$data = $client->loadOutput(); | |
var_dump($data); | |
// Quer um Serialize? | |
$client->setOutput(new SerializedArrayOutput()); | |
$data = $client->loadOutput(); | |
var_dump($data); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment