Tag Archives: Facade Pattern

Facade in Laravel

facede-larvel

What is Facade

Facades provide a “static” interface to classes that are available in the application’s service container(managing class dependencies and performing dependency injection(class dependencies are “injected” into the class via the constructor or, in some cases, “setter” methods.) ).

Laravel facades serve as “static proxies” to underlying classes in the service container, providing the benefit of a short, expressive syntax while maintaining more flexibility(In terms of configuring class/libraries being used) than traditional static methods.

All of Laravel’s facades are defined in the Illuminate\Support\Facades namespace.

How Facades Work

In a Laravel application, a facade is a class that provides access to an object from the service container. The machinery that makes this work is in the Facade class. Laravel’s facades, and any custom facades you create, will extend the base Illuminate\Support\Facades\Facade class.

The Facade base class makes use of the __callStatic() magic-method to defer calls from your facade to an object resolved from the container.

In the below lines , a call is made to the Laravel cache system. By looking at code, one might assume that the static method get is being called on the Cache class:

<?php

use Illuminate\Support\Facades\Cache;

function someMethod(){
    $id = 'abc'; //some key on which data is saved
    $user = Cache::get($id);
    
}

//If we look at that Illuminate\Support\Facades\Cache class, you'll see that there is no static method named get:

class Cache extends Facade
{
    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor() { return 'cache'; }
}

If we look at that Illuminate\Support\Facades\Cache class, you’ll see that there is no static method named get:

Instead, the Cache facade extends the base Facade class and defines the method getFacadeAccessor(). This method’s job is to return the name of a service container binding.

When a user references any static method on the Cache facade, Laravel resolves the cache binding from the service container and runs the requested method (in this case, get) against that object.

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
    }
}