Facade Design Pattern in PHP

Facade Design Pattern in PHP

Facade Design Pattern is one of the structural design patterns in Software Engineering. Provides a simplified, higher-level interface to a subsystem. Clients can talk to the facade rather than individual classes in the subsystem.

What is the Facade Design Pattern?

Gang of Four (GOF) defines it as:

Provide a unified interface to a set of interfaces in a subsystem. Facade Pattern defines a higher-level interface that makes the subsystem easier to use.

Suppose we have an application with a set of operations to send a message to a Notification Server, like connecting, authenticating, sending, and disconnecting.

So we will need to perform these operations whenever we want to send a message to the Notification Server. However, this increases the complexity of the code. For this reason, the client application will find it difficult to manage it.

So we can apply Facade design pattern here and provide a higher-level interface that makes the subsystem easier to use.

The facade design pattern can basically be represented in UML as:

A Demonstration of Facade Design Pattern in UML

Facade Design Pattern PHP Example

We will make a simple application to demonstrate Facade Design Pattern in PHP. We will create a Facade for a subsystem that is used for the communication between the client and the Notification Server.

Here is our subsystem:

<?php

namespace App;

class AuthToken
{

}

class Connection
{
public function disconnect()
{

}
}
class Message
{
private $message;

/**
* Message constructor.
*
@param $message
*/
public function __construct($message)
{
$this->message = $message;
}

}
class NotificationServer
{
/**
*
@param string $ipAddress
*
@return Connection
*/
public function connect(string $ipAddress): Connection
{
return new Connection();
}

/**
*
@param string $appID
*
@param string $key
*
@return AuthToken
*/
public function authenticate(string $appID, string $key): AuthToken
{
return new AuthToken();
}

/**
*
@param AuthToken $authToken
*
@param Message $message
*
@param string $target
*/
public function send(AuthToken $authToken, Message $message, string $target)
{
echo "Sending a message" . PHP_EOL;
}
}

In this example, whenever we want to send a message, we should create an instance of NotificationServer class, connect the server, authenticate, send the message, and disconnect from the server. But, this situation can increase the complexity of the code. Thus, we apply Facade design pattern to provide a higher-level interface where clients can talk to it rather than individual classes in the subsystem.

The facade class is as follows:

<?php

namespace App;

class NotificationService
{
/**
* A wrapper method to send a message to Notification Server
*
*
@param string $message
*
@param string $target
*/
public function send(string $message, string $target)
{
$server = new NotificationServer();
$connection = $server->connect("ip");
$authToken = $server->authenticate("appID", "key");
$server->send($authToken, new Message($message), $target);
$connection->disconnect();
}
}

Finally, we can use the facade whenever we want to send a message to the Notification Server in the code. Here is the example usage of the Facade:

$service = new \App\NotificationService();
$service->send("Hello World", "target");

That’s all for the Facade Design Pattern in PHP, I hope you enjoyed it. Please feel free to leave comments and share the article with others.

Go to Github repository of example application here:
https://github.com/bariscimen/FacadePatternPHP

PhDc, Senior Software Engineer