An Intro to Facade Design Pattern in PHP

Facade deals with the fact that how your code should be structured; that's why it falls under the category of structural design patterns. Imagine in a given scenario, we have to use lots of classes and methods to complete a task; without using a pattern like Facade, we need to make different objects from those classes and call their methods one after the other. Now think of a solution that there is just a simple interface (e.g a static method) that handles all subsequent tasks and the only thing you need to do is calling that method; here is the situation that Facade design pattern comes to play and in this tutorial, you will learn how to implement it in PHP programming language.

As a conceptual example, imagine that we want to make rice through the following code sample:

<?php
interface MakingRiceInterface 
{
    public function do();
} 

class WashRice implements MakingRiceInterface
{
    public function do()
    {
        echo "Rice is washed well\n";
    }
}

class AddWater implements MakingRiceInterface
{
    public function do()
    {
        echo "Some water is added\n";
    }
}

class AddOil implements MakingRiceInterface
{
    public function do()
    {
        echo "Some vegetable oil is added\n";
    }
}

class PutItOnTheCooker implements MakingRiceInterface
{
    public function do()
    {
        echo "Pour all of them in a pot and put it on the cooker\n";
    }
}

$washingObj = new WashRice();
$washingObj->do();

$addingWaterObj = new AddWater();
$addingWaterObj->do();

$addingOilObj = new AddOil();
$addingOilObj->do();

$puttingItOnTheCookerObj = new PutItOnTheCooker();
$puttingItOnTheCookerObj->do();

And the result is as follows:

Rice is washed well
Some water is added
Some vegetable oil is added
Pour all of them in a pot and put it on the cooker

As you can see, in order to make rice, we have to use different classes and call their relative methods as well and no need to say that the approach used in the code above adds to the complexity of the software and here is the place where the Facade pattern comes to our rescue. To refactor the above code in a much better way, we can create a class called RiceRecipeFacade that handles all the tasks needed to make rice as follows:

<?php
interface MakingRiceInterface
{
    function do();
}

class WashRice implements MakingRiceInterface
{
    public function do() {
        echo "Rice is washed well\n";
    }
}

class AddWater implements MakingRiceInterface
{
    function do() {
        echo "Some water is added\n";
    }
}

class AddOil implements MakingRiceInterface
{
    function do() {
        echo "Some vegetable oil is added\n";
    }
}

class PutItOnTheCooker implements MakingRiceInterface
{
    function do() {
        echo "Pour all of them in a pot and put it on the cooker\n";
    }
}

class RiceRecipeFacade
{
    public static function makeRice()
    {
        (new WashRice)->do();
        (new AddWater)->do();
        (new AddOil)->do();
        (new PutItOnTheCooker)->do();
    }
}

RiceRecipeFacade::makeRice();

In the output we will have:

Rice is washed well
Some water is added
Some vegetable oil is added
Pour all of them in a pot and put it on the cooker

In fact, all the complexity of instantiating different classes to make rice is handled by RiceRecipeFacade class and its only static method called makeRice() and the user can easily make rice with just writing one line of code.

by Behzad Moradi on 2019-08-12