Skip to content

Instantly share code, notes, and snippets.

@frostbitten
Last active October 4, 2016 02:10
Show Gist options
  • Save frostbitten/43d53caf76a8ae2ef3354b5d0347f2ec to your computer and use it in GitHub Desktop.
Save frostbitten/43d53caf76a8ae2ef3354b5d0347f2ec to your computer and use it in GitHub Desktop.
Bridge Design Pattern Test
<!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?

#Program Output


#Bridge Design Pattern

"decouple an abstraction from its implementation so that the two can vary independently" (GoF 151)

Demo is a derivative of http://www.newthinktank.com/2012/10/bridge-design-pattern-tutorial/.

Note: A more competent programmer has not yet verified if this properly follows the Bridge Design Pattern.


  • Set channel to 1
  • Set channel to 2
  • Set channel to 3
  • Set channel to 4
  • Set channel to 3
  • Turned Picture in Picture feature On
  • Volume set to 1
  • Set channel to 2
  • Set channel to 3
  • Volume set to 0
  • Volume set to 0
  • Volume set to 1
  • Turned Picture in Picture feature Off

  • DVD is playing
  • Volume set to 1
  • Volume set to 2
  • DVD is stopped
  • DVD Player is on standby mode.
  • DVD Player is on and ready.
  • Skipped to DVD chapter 10
  • Skipped to DVD chapter 0
  • Volume set to 1
  • Volume set to 0
  • Volume set to 1
  • DVD Player is on standby mode.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment