Category Archives: Facade Pattern

Facade Pattern – Design Pattern

facade

Intent of facade is to provide the ease of use to any library(subsystem) through a an interface/class.

The Facade Pattern provides a unifide interface to a set of Interfaces in a subsystem. Facade defines a higher level interface that makes the subsystem easier to use.

Here We can take example of a complex theater system. For playing a movie we may need to set a different programs of each subsystem, Now We can look at facade is some thing like that reduces steps in performing a task, rather than tuning each individual program to work.

In below example TheaterFacade object makes easier to operate programs.

<?php
class TheaterFacade {
    public $dvd, $music, $movie, $player;

    public function TheaterFacade(DVD $dvd, Music $music, Movie $movie, Player $player)
    {
        $this->dvd = $dvd;
        $this->music = $music;
        $this->movie = $movie;
        $this->player = $player;
    }

    public function playMovie($movieName)
    {
        $this->music->setMusic(7);
        $this->movie->setMovie($movieName);
        $this->player->play();
        ...
        ...
    }

    public function stopMovie()
    {
        $this->music->stopMusic();
        $this->player->stop();
        ...
        ...
    }

}

class Music {
    
    public function setMusic($volume)
    {
      //setting volume function
    }

    public function stopMusic()
    {
      //stop function
    }
}

class Player {
    public function play(){
       //play function
    }

    public function stop(){
       //stop player function
    }

}

class Movie {

    public function setMovie ($movie) {
        //set movie to play
    }
}