|
<!doctype html> |
|
<html> |
|
<head> |
|
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.7/css/materialize.min.css"> |
|
<title>Device Bridge</title> |
|
<body class="container"> |
|
|
|
<h1>Bridge Design Pattern</h1> |
|
<p><i>"decouple an abstraction from its implementation so that the two can vary independently"</i> (GoF 151) |
|
|
|
<p>Demo is a derivative of <a href="http://www.newthinktank.com/2012/10/bridge-design-pattern-tutorial/">http://www.newthinktank.com/2012/10/bridge-design-pattern-tutorial/</a>. |
|
<p>Note: A more competent programmer has not yet verified if this properly follows the Bridge Design Pattern. |
|
|
|
<hr> |
|
<?php |
|
|
|
interface IEntertainmentDevice { |
|
|
|
function button1pressed(); |
|
function button2pressed(); |
|
function button3pressed(); |
|
function button4pressed(); |
|
function button5pressed(); |
|
|
|
} |
|
|
|
abstract class EntertainmentDevice implements IEntertainmentDevice { |
|
|
|
protected $state; |
|
protected $maxSetting; |
|
protected $volume; |
|
|
|
public function button1pressed(){ |
|
$this->state++; |
|
$this->stateChange(); |
|
} |
|
|
|
public function button2pressed(){ |
|
$this->state--; |
|
$this->stateChange(); |
|
} |
|
|
|
public function button3pressed(){ |
|
$this->volume++; |
|
$this->volumeChange(); |
|
} |
|
|
|
public function button4pressed(){ |
|
$this->volume--; |
|
$this->volumeChange(); |
|
} |
|
|
|
public abstract function button5pressed(); |
|
public abstract function deviceStateFeedback(); |
|
|
|
public function stateChange(){ |
|
$this->stateCheckMaxMin(); |
|
$this->deviceStateFeedback(); |
|
} |
|
|
|
public function volumeChange(){ |
|
$this->volumeCheckMaxMin(); |
|
$this->deviceVolumeFeedback(); |
|
} |
|
|
|
public function stateCheckMaxMin(){ |
|
if($this->state > $this->maxSetting) {$this->state = 0;} |
|
elseif($this->state < 0 ) {$this->state = $this->maxSetting;} |
|
} |
|
|
|
public function volumeCheckMaxMin(){ |
|
if($this->volume > 100) {$this->volume = 100;} |
|
elseif($this->volume < 0 ) {$this->volume = 0;} |
|
} |
|
|
|
public function deviceVolumeFeedback(){ |
|
echo "Volume set to " , intval($this->volume) ,"<br>"; //intval necessary to prevent invisible 0's (false) |
|
} |
|
|
|
} |
|
|
|
class TVDevice extends EntertainmentDevice { |
|
|
|
protected $picture_in_picture = false; |
|
|
|
public function __construct($state,$maxSetting){ |
|
$this->state = $state; |
|
$this->maxSetting = $maxSetting; |
|
} |
|
|
|
public function button5pressed(){ |
|
$this->mute(); |
|
} |
|
|
|
public function mute(){ |
|
$this->volume = 0; |
|
$this->volumeChange(); |
|
} |
|
|
|
public function pip(){ |
|
$this->picture_in_picture = !$this->picture_in_picture; |
|
$this->devicePipChange(); |
|
} |
|
|
|
public function devicePipChange(){ |
|
$this->devicePipFeedback(); |
|
} |
|
|
|
public function devicePipFeedback(){ |
|
echo "Turned Picture in Picture feature ", ($this->picture_in_picture? "On" : "Off") ,"<br>"; |
|
} |
|
|
|
public function deviceStateFeedback(){ |
|
echo "Set channel to ", $this->state ,"<br>"; |
|
} |
|
|
|
} |
|
|
|
class DVDPlayerDevice extends EntertainmentDevice { |
|
|
|
protected $is_standby = false; |
|
protected $play_state = "stopped"; |
|
|
|
public function __construct($state,$maxSetting){ |
|
$this->state = $state; |
|
$this->maxSetting = $maxSetting; |
|
} |
|
|
|
public function standby(){ |
|
$this->is_standby = !$this->is_standby; |
|
$this->standbyStateChange(); |
|
} |
|
|
|
public function play(){ |
|
$this->play_state = $this->play_state === "playing" ? "stopped" : "playing"; |
|
$this->playStateChange(); |
|
} |
|
|
|
public function deviceStateFeedback(){ |
|
echo "Skipped to DVD chapter ", intval($this->state) ,"<br>"; |
|
} |
|
public function deviceplayStateFeedback(){ |
|
echo "DVD is ", $this->play_state ,"<br>"; |
|
} |
|
|
|
public function button5pressed(){ |
|
$this->play(); |
|
} |
|
|
|
public function playStateChange(){ |
|
$this->deviceplayStateFeedback(); |
|
} |
|
|
|
public function standbyStateFeedback(){ |
|
echo "DVD Player is on", ($this->is_standby? " standby mode.": " and ready.") ,"<br>"; |
|
} |
|
|
|
public function standbyStateChange(){ |
|
$this->standbyStateFeedback(); |
|
} |
|
} |
|
|
|
abstract class RemoteControl { |
|
|
|
protected $device; |
|
|
|
public function __construct(EntertainmentDevice $device){ |
|
$this->device = $device; |
|
} |
|
|
|
public function deviceStateFeedback(){ |
|
$this->device->deviceStateFeedback(); |
|
} |
|
|
|
public function deviceVolumeFeedback(){ |
|
$this->device->deviceVolumeFeedback(); |
|
} |
|
|
|
public function button1pressed(){ |
|
$this->device->button1pressed(); |
|
} |
|
public function button2pressed(){ |
|
$this->device->button2pressed(); |
|
} |
|
public function button3pressed(){ |
|
$this->device->button3pressed(); |
|
} |
|
public function button4pressed(){ |
|
$this->device->button4pressed(); |
|
} |
|
public function button5pressed(){ |
|
$this->device->button5pressed(); |
|
} |
|
|
|
public abstract function button6pressed(); |
|
|
|
|
|
} |
|
|
|
class TVRemote extends RemoteControl { |
|
|
|
public function button6pressed(){ |
|
$this->device->pip(); |
|
} |
|
} |
|
|
|
class DVDRemote extends RemoteControl { |
|
|
|
public function button6pressed(){ |
|
$this->device->standby(); |
|
} |
|
} |
|
|
|
|
|
$test = new TVDevice(0,100); |
|
$rem = new TVRemote($test); |
|
|
|
|
|
$rem->button1pressed(); |
|
$rem->button1pressed(); |
|
$rem->button1pressed(); |
|
$rem->button1pressed(); |
|
$rem->button2pressed(); |
|
$rem->button6pressed(); |
|
$rem->button3pressed(); |
|
$rem->button2pressed(); |
|
$rem->button1pressed(); |
|
$rem->button4pressed(); |
|
$rem->button4pressed(); |
|
$rem->button3pressed(); |
|
$rem->button6pressed(); |
|
|
|
|
|
echo "<hr>"; |
|
|
|
$test = new DVDPlayerDevice(0,10); |
|
$rem = new DVDRemote($test); |
|
|
|
|
|
$rem->button5pressed(); |
|
$rem->button3pressed(); |
|
$rem->button3pressed(); |
|
$rem->button5pressed(); |
|
$rem->button6pressed(); |
|
$rem->button6pressed(); |
|
$rem->button2pressed(); |
|
$rem->button1pressed(); |
|
$rem->button4pressed(); |
|
$rem->button4pressed(); |
|
$rem->button3pressed(); |
|
$rem->button6pressed(); |
|
|
|
|
|
?> |
|
<hr> |
|
<p> Eh? |