Last active
May 23, 2019 02:06
-
-
Save hallboav/2267c94aa01fb196b509025727f9cba6 to your computer and use it in GitHub Desktop.
State machine
This file contains hidden or 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 PortaInterface | |
{ | |
public function abrir(); | |
public function fechar(); | |
} | |
abstract class AbstractPorta implements PortaInterface | |
{ | |
public function abrir() | |
{ | |
throw new \BadMethodCallException('Não é possível abrir esta porta.'); | |
} | |
public function fechar() | |
{ | |
throw new \BadMethodCallException('Não é possível fechar esta porta.'); | |
} | |
} | |
class PortaFechada extends AbstractPorta | |
{ | |
public function abrir() | |
{ | |
return new PortaAberta(); | |
} | |
} | |
class PortaAberta extends AbstractPorta | |
{ | |
public function fechar() | |
{ | |
return new PortaFechada(); | |
} | |
} | |
$initialState = new PortaFechada(); | |
$initialState | |
->abrir() | |
->fechar() | |
->abrir() | |
->fechar(); |
This file contains hidden or 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 PortaInterface | |
{ | |
public function abrir(); | |
public function fechar(); | |
} | |
class Porta implements PortaInterface | |
{ | |
private $isAberta; | |
public function __construct($isAberta) | |
{ | |
$this->isAberta = $isAberta; | |
} | |
public function abrir() | |
{ | |
if ($this->isAberta) { | |
throw new \BadMethodCallException('Não é possível abrir esta porta.'); | |
} | |
$this->isAberta = true; | |
return $this; | |
} | |
public function fechar() | |
{ | |
if (!$this->isAberta) { | |
throw new \BadMethodCallException('Não é possível fechar esta porta.'); | |
} | |
$this->isAberta = false; | |
return $this; | |
} | |
} | |
$porta = new Porta(false); | |
$porta | |
->abrir() | |
->fechar() | |
->abrir() | |
->fechar(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment