A facade refers to the front wall of a large building that can be seen from the outside.
The facade design pattern is analogous to a facade in architecture. From the outside, we can see the face of the building without knowing about the complex internal structure.
In the same way, a facade object serves as a front-facing interface masking more complex underlying or structural code.
First, we will try to understand the problem with an example
A client wants to share a post to different websites, and the client can simply call respective classes to share. Till now, things look cool.
What if the client has to use this logic in multiple places? The client has to write these functions in many places.
Let's move toward the solution. In order to remove these unnecessary function calls, let's create a single class that will internally call other classes.
<?php
class shareFacade {
protected $twitter;
protected $google;
protected $reddit;
function __construct($twitterObj,$gooleObj,$redditObj)
{
$this->twitter = $twitterObj;
$this->google = $gooleObj;
$this->reddit = $redditObj;
}
// One function makes all the job of calling all the share methods
// that belong to all the social networks.
function share($url,$title,$status)
{
$this->twitter->tweet($status, $url);
$this->google->share($url);
$this->reddit->reddit($url, $title);
}
}
// Create the objects from the classes.
$twitterObj = new CodeTwit();
$gooleObj = new Googlize();
$redditObj = new Reddiator();
// Pass the objects to the class facade object.
$shareObj = new shareFacade($twitterObj,$gooleObj,$redditObj);
// Call only 1 method to share your post with all the social networks.
$shareObj->share('https://www.codebucket.in/','My greatest post','Read my greatest post ever.');