Created
June 23, 2020 13:21
-
-
Save kennyray/b1843ac005c9663ace3ae9de7bf046d8 to your computer and use it in GitHub Desktop.
Remotes
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 RemoteControl | |
{ | |
public function powerOn() : void; | |
} | |
class Television | |
{ | |
public function setPower(bool $state) : void | |
{ | |
// truncated code | |
} | |
} | |
class CableBox | |
{ | |
public function setPower(bool $state) : void | |
{ | |
// truncated code | |
} | |
} | |
class TVRemote implements RemoteControl | |
{ | |
private $_tv; | |
public function __construct(Television $tv) | |
{ | |
$this->_tv = $tv; | |
} | |
public function powerOn() :void | |
{ | |
$this->_tv->setPower(true); | |
} | |
} | |
////////////////// MY CODE ////////////////////////// | |
class CableRemote implements RemoteControl | |
{ | |
private $_box; | |
public function __construct(CableBox $box) | |
{ | |
$this->_box = $box; | |
} | |
public function powerOn() :void | |
{ | |
$this->_box->setPower(true); | |
} | |
} | |
// This will allow the user to select which device they want to turn on. | |
// I may have taken it further than intended. I'm not sure if the goal | |
// was to turn both on at the same time with the universal remote | |
// or to allow the user to select the device to control. | |
class UniversalRemote implements RemoteControl | |
{ | |
private $device; | |
public function __construct(RemoteControl $device) | |
{ | |
$this->device = $device; | |
} | |
public function powerOn() :void | |
{ | |
$this->device->powerOn(); | |
} | |
} | |
class User | |
{ | |
private $remoteType; | |
public function pickupRemote(RemoteControl $remoteType) | |
{ | |
$this->remoteType = $remoteType; | |
} | |
public function pressPowerOn() | |
{ | |
return $this->remoteType->powerOn(); | |
} | |
} | |
$user = new User(); | |
// Pick up tv remote and press power on | |
$user->pickupRemote(new TVRemote(new Television())); | |
$user->pressPowerOn(); | |
// Pick up universal remote, select TV, and press power on | |
$user->pickupRemote(new UniversalRemote(new TVRemote(new Television()))); | |
$user->pressPowerOn(); | |
// Pick up universal remote, select Cable Box, and press power on | |
$user->pickupRemote(new UniversalRemote(new CableRemote(new CableBox()))); | |
$user->pressPowerOn(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment