Skip to content

Instantly share code, notes, and snippets.

@domzgarcia
Created November 23, 2018 12:51
Show Gist options
  • Save domzgarcia/04bd0f1f32c1813c38f6be82ac7227e8 to your computer and use it in GitHub Desktop.
Save domzgarcia/04bd0f1f32c1813c38f6be82ac7227e8 to your computer and use it in GitHub Desktop.
<?php
class Pokemon
{
public function attack()
{
echo 'Punch';
}
public function rest()
{
echo 'Sleep';
}
}
class Pikachu extends Pokemon
{
public function attack()
{
echo 'Lightning!';
}
public function duplicate()
{
echo '2x';
}
}
class Squerrel extends Pokemon
{
public function attack()
{
echo 'Water ball!';
}
public function rest()
{
echo 'Self destroy!';
}
}
class PokemonFactory
{
public function build($name)
{
return new $name();
}
}
class Trainer
{
protected $pokemon;
public function usePokemonBaseOnElementType($type)
{
$factory = new PokemonFactory();
if ($type == 'light') {
$this->setPokemon($factory->build('Pikachu'));
} elseif ($type == 'water') {
$this->setPokemon($factory->build('Squerrel'));
}
}
public function setPokemon(Pokemon $pokemon)
{
$this->pokemon = $pokemon;
}
public function useYourSkill()
{
$this->pokemon->attack();
}
public function pleaseRest()
{
$this->pokemon->rest();
}
public function doYourBest()
{
return $this->pokemon;
}
}
$trainer = new Trainer();
$trainer->usePokemonBaseOnElementType('light');
$trainer->useYourSkill();
$trainer->pleaseRest();
$trainer->doYourBest()->duplicate();
class ATMFacade
{
public static function checkBalance()
{
return CheckBalanceBuilder();
}
public static function deposit()
{
}
public static function withdraw()
{
}
}
echo ATMFacade::checkBalance();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment