Design Pattern: Facade in PHP
Continuing with the Head First Design Patterns book is the facade pattern. Facade is another design pattern in PHP. The facade pattern is used to provide a simplified interface and to hide the complexity of the actual work going on behind. When does the facade get used? The most basic example is your computer. Say you have Windows 7. Windows 7 provides an interface for you to find your files, to change your background and so forth. But beneath Windows 7, there are multiple independent applications that actually do the work, but Windows 7 provides a unified theme for all these interactions.
If you would like to play around with the code, here’s a copy.
include_once("amplifier.php");
class Tuner {
private $amplifier;
private $description;
private $frequency;
public function __construct(Amplifier $amplifier, $description) {
$this->description = $description;
}
public function on() {
echo($this->description . " on");
echo("<br />");
}
public function off() {
echo($this->description . " off");
echo("<br />");
}
public function setFrequency($frequency) {
echo($this->description . " setting frequency to " . $frequency);
echo("<br />");
$this->frequency = $frequency;
}
public function setAm() {
echo($this->description . " setting AM mode");
echo("<br />");
}
public function setFm() {
echo($this->description . " setting FM mode");
echo("<br />");
}
}
class TheaterLights {
private $description;
public function __construct($description) {
$this->description = $description;
}
public function on() {
echo($this->description . " on");
echo("<br />");
}
public function off() {
echo($this->description . " off");
echo("<br />");
}
public function dim($level) {
echo($this->description . " dimming to " . $level . "%");
echo("<br />");
}
}
class Screen {
private $description;
public function __construct($description) {
$this->description = $description;
}
public function up() {
echo($this->description . " going up");
echo("<br />");
}
public function down() {
echo($this->description . " going down");
echo("<br />");
}
}
include_once("dvdPlayer.php");
class Projector {
private $description;
private $dvdPlayer;
public function __construct($description, DVDPlayer $dvdPlayer) {
$this->description = $description;
$this->dvdPlayer = $dvdPlayer;
}
public function on() {
echo($this->description . " on");
echo("<br />");
}
public function off() {
echo($this->description . " off");
echo("<br />");
}
public function wideScreenMode() {
echo($this->description . " in widescreen mode (16x9 aspect ratio)");
echo("<br />");
}
public function tvMode() {
echo($this->description . " in tv mode (4x3 aspect ratio)");
echo("<br />");
}
}
class PopcornPopper{
private $description;
public function __construct($description){
$this->description = $description;
}
public function on(){
echo($this->description . " on");
echo("<br />");
}
public function off(){
echo($this->description . " off");
echo("<br />");
}
public function pop(){
$this->title = null;
echo($this->description . " popping popcorn");
echo("<br />");
}
}
include_once("amplifier.php");
class DVDPlayer {
private $amplifier;
private $description;
private $currentTrack;
private $movie;
public function __construct($description, Amplifier $amplifier) {
$this->amplifier = $amplifier;
$this->description = $description;
}
public function on() {
echo($this->description . " on");
echo("<br />");
}
public function off() {
echo($this->description . " off");
echo("<br />");
}
public function eject() {
$this->movie = null;
echo($this->description . " eject");
echo("<br />");
}
public function play($movieOrTrack) {
if (is_string($movieOrTrack)) {
$this->movie = $movieOrTrack;
$this->currentTrack = 0;
echo($this->description . " playing " . $this->movie);
} else {
if ($this->movie == null) {
echo($this->description . " can't play track " . $movieOrTrack . " no dvd inserted");
} else {
$this->currentTrack = $movieOrTrack;
echo(description . " playing track " . $this->currentTrack . " of " . $this->movie);
}
}
}
public function stop() {
$this->currentTrack = 0;
echo($this->description . " stopped");
echo("<br />");
}
public function pause() {
echo($this->description . " paused " . $this->movie);
echo("<br />");
}
public function setTwoChannelAudio() {
echo($this->description . " set two channel audio");
echo("<br />");
}
public function setSurroundAudio() {
echo($this->description . " set surround audio");
echo("<br />");
}
public function __toString() {
return $this->description;
}
}
include_once("amplifier.php");
class CDPlayer{
private $amplifier;
private $description;
private $currentTrack;
private $title;
public function __construct(Amplifier $amplifier, $description){
$this->amplifier = $amplifier;
$this->description = $description;
}
public function on(){
echo($this->description . " on");
echo("<br />");
}
public function off(){
echo($this->description . " off");
echo("<br />");
}
public function eject(){
$this->title = null;
echo($this->description . " eject");
echo("<br />");
}
public function play($titleOrTrack){
if(is_string($titleOrTrack)){
$this->title = $titleOrTrack;
$this->currentTrack = 0;
echo($this->description . " playing " . $this->title);
echo("<br />");
}
else{
if($titleOrTrack == null){
echo($this->description . " can't play track " . $this->currentTrack . ", no cd inserted");
echo("<br />");
}
else{
$this->currentTrack = $titleOrTrack;
echo($this->description . " playing track " . $this->currentTrack);
echo("<br />");
}
}
}
public function stop(){
$this->currentTrack = 0;
echo($this->description . " stopped");
echo("<br />");
}
public function pause(){
echo($this->description . " paused " . $this->title);
echo("<br />");
}
public function __toString() {
return $this->description;
}
}
include("tuner.php");
include("dvdPlayer.php");
include("cdPlayer.php");
class Amplifier{
private $tuner;
private $dvd;
private $cd;
private $description;
public function __construct($description){
$this->description = $description;
}
public function on(){
echo($this->description . " on");
echo("<br />");
}
public function off(){
echo($this->description . " off");
echo("<br />");
}
public function setCD(CDPlayer $cd){
echo($this->description . " setting CD player to " . $cd);
echo("<br />");
$this->cd = $cd;
}
public function setDVD(DVDPlayer $dvd){
echo($this->description . " setting DVD player to " . $dvd);
echo("<br />");
$this->dvd = $dvd;
}
public function setStereoSound(){
echo($this->description . " stereo mode on");
echo("<br />");
}
public function setSurroundSound(){
echo($this->description . " surround sound on (5 speakers, 1 subwoofer)");
echo("<br />");
}
public function setTuner(Tuner $tuner){
echo($this->description . " setting tuner to " . $this->dvd);
echo("<br />");
$this->tuner = $tuner;
}
public function setVolume($volume){
echo($this->description . " setting volume to " . $volume);
echo("<br />");
}
}
include_once('amplifier.php');
include_once('tuner.php');
include_once('dvdPlayer.php');
include_once('cdPlayer.php');
include_once('projector.php');
include_once('theaterLights.php');
include_once('screen.php');
include_once('popcornPopper.php');
class HomeTheaterFacade {
private $amplifier;
private $tuner;
private $dvd;
private $cd;
private $projector;
private $lights;
private $screen;
private $popper;
public function __construct(Amplifier $amplifier, Tuner $tuner, DVDPlayer $dvd, CDPlayer $cd, Projector $projector, Screen $screen, TheaterLights $lights, PopcornPopper $popper
) {
$this->amplifier = $amplifier;
$this->tuner = $tuner;
$this->dvd = $dvd;
$this->cd = $cd;
$this->projector = $projector;
$this->screen = $screen;
$this->lights = $lights;
$this->popper = $popper;
}
public function watchMovie($movie) {
echo("Get ready to watch a movie...");
$this->popper->on();
$this->popper->pop();
$this->lights->dim(10);
$this->screen->down();
$this->projector->on();
$this->projector->wideScreenMode();
$this->amplifier->on();
$this->amplifier->setDVD($this->dvd);
$this->amplifier->setSurroundSound();
$this->amplifier->setVolume(5);
$this->dvd->on();
$this->dvd->play($movie);
}
public function endMovie() {
echo("Shutting movie theater down...");
$this->popper->off();
$this->lights->on();
$this->screen->up();
$this->projector->off();
$this->amplifier->off();
$this->dvd->stop();
$this->dvd->eject();
$this->dvd->off();
}
public function listenToCd($cdTitle) {
echo("Get ready for an audiopile experence...");
$this->lights->on();
$this->amplifier->on();
$this->amplifier->setVolume(5);
$this->amplifier->setCD($this->cd);
$this->amplifier->setStereoSound();
$this->cd->on();
$this->cd->play($cdTitle);
}
public function endCd() {
echo("Shutting down CD...");
$this->amplifier->off();
$this->amplifier->setCD($this->cd);
$this->cd->eject();
$this->cd->off();
}
public function listenToRadio($frequency) {
echo("Tuning in the airwaves...");
$this->tuner->on();
$this->tuner->setFrequency($frequency);
$this->amplifier->on();
$this->amplifier->setVolume(5);
$this->amplifier->setTuner($this->tuner);
}
public function endRadio() {
echo("Shutting down the tuner...");
$this->tuner->off();
$this->amplifier->off();
}
}
include_once("homeTheaterFacade.php");
class HomeTheaterTestDrive{
public static function main(){
$amplifier = new Amplifier("Top-O-Line Amplifier");
$tuner = new Tuner($amplifier, "Top-O-Line AM/FM Tuner");
$dvd = new DVDPlayer("Top-O-Line DVD Player", $amplifier);
$cd = new CDPlayer($amplifier, "Top-O-Line CD Player");
$projector = new Projector("Top-O-Line Projector", $dvd);
$lights = new TheaterLights("Theater Ceiling Lights");
$screen = new Screen("Theater Screen");
$popper = new PopcornPopper("Popcorn Popper");
$homeTheater = new HomeTheaterFacade($amplifier, $tuner, $dvd, $cd, $projector, $screen, $lights, $popper);
$homeTheater->watchMovie("Raiders of the Lost Ark");
$homeTheater->endMovie();
$homeTheater->listenToCd("Sound of Music");
$homeTheater->endCd();
$homeTheater->listenToRadio("101.69");
$homeTheater->endRadio();
}
}
HomeTheaterTestDrive::main();
?>
