Facade design pattern provides a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use. It wraps a complicated subsystem with a simpler interface.
Example
Here the Auth class is a facade. To authorize the user client calls static method Auth:authorize(). Inside this method the user object is created and other methods are called.
class User
{
private $login;
private $fullName;
private $roles = [];
public function __construct(string $login, string $password) {
$this->login = $login;
$this->fullName = "User Name";
$this->roles = ['user', 'moderator'];
echo "User found<br>";
}
}
class AuthenticationProvider
{
public static function authenticate(User $user)
{
echo "User authenticated<br>";
return true;
}
}
class SessionHelper
{
public static function create(User $user)
{
echo "Session created<br>";
}
}
//class Auth ia a Facade
class Auth
{
public static function authorize(string $login, string $password)
{
$user = new User($login, $password);
if (AuthenticationProvider::authenticate($user)) {
SessionHelper::create($user);
}
echo "User authorized<br>";
}
}
Auth::authorize('user1', 'pass123');
Result
User found
User authenticated
Session created
User authorized