Design Pattern: Facade Pattern in PHP

Design patterns: facade pattern

In previous article, we already categorized the design patterns, you can check out Design Patterns Overview for more details. You also can checkout the series of Design patterns here

Definition of Facade Pattern

The facade pattern is a software design pattern commonly used with object-oriented programming. The name is by analogy to an architectural facade. A facade is an object that provides a simplified interface to a larger body of code, such as a class library.

The main goal of Facade Pattern

  • Simplicity is the goal of facade pattern.
  • Make your code be structured then it will be easily to understand and well maintained.
  • Defines a higher lever interface that makes the subsystem easier to use.
  • Wrap a complicated subsystem with a simpler interface.

An example Facade Pattern implementation

We will build a simple example about facade pattern then you will understand more about it. In this example we will build a foo data, because we don’t really care about the business logic. Just focus to figure out the structure and idea of Facade Pattern.
Facade Pattern
The diagram above describes how the pattern works, each item in the diagram are described below:

  • ClientFacade: This class contains all functions which hide the complexities of the difficult to use subsystems.
  • PaymentHistory: Define a very simple function to get Payment by client Id.
  • MostPayments: Return a foo number of payment
  • ClientPersonalData: Provide the method to get personal information base on client Id
  • Application: gets Client Data via the Client facade
class ClientFacade {
   * Provide a simplified interface to a larger body of code
   * the subsystem will not know about the business of this function
  public function getClientDataById($clientId) {
     return array(
       'id' => $clientId,
       'address' => $this->getClientAddressById($clientId),
       'mostPayedFor' => $this->getMostPayedFor($clientId),
       'userHistory' => $this->getPaymentHistoryById($clientId)
  private function getClientAddressById($clientId) {
     $clientShippingAddress = '';
     $clientPersonalData = new ClientPersonalData($clientId);
     $clientShippingAddress = $clientPersonalData->getAddress();
     $clientShippingAddress .= ', ' . $clientPersonalData->getCountry();
     $clientShippingAddress .= ', ' . $clientPersonalData->getPostalCode();
     return $clientShippingAddress;
  private function getMostPayedFor($clientId) {
     $mostPayments = new MostPayments();
     return $mostPayments->findMostPaymentOfClientById($clientId);
  private function getPaymentHistoryById($clientId) {
     $paymentHistory = new PaymentHistory();
     return $paymentHistory->findPaymentByClientId($clientId);
class Statistics {
  private $clientData;
  private $facade;
  public function __construct(ClientFacade $facade) {
      $this->clientData = new clientData();
      $this->facade = $facade;
   * Just call function in facade, don't know how this function work to get
   * the information
  public function loadClientStatsById($clientId) {
     $clientStatsData = $this->facade->getClientDataById($clientId);
     $this->clientData->id = $clientStatsData['id'];
     $this->clientData->address = $clientStatsData['address'];
     $this->clientData->mostPayedFor = $clientStatsData['mostPayedFor'];
     $this->clientData->userHistory = $clientStatsData['userHistory'];
class MostPayments {
   public function findMostPaymentOfClientById($clientId) {
       // Return an example value
       return 200;
class PaymentHistory {
    public function findPaymentByClientId($clientId) {
        // Foo data
        return array(10, 100, 1000, 5000);
class ClientPersonalData {
    private $streetAddress;
    private $postalCode;
    private $country;
    public function __construct($clientId) {
        // We will not care about the business logic how to set the personal data in this example
        // we only care about the idea of facade pattern
    public function getAddress() {
        return $this->streetAddress;
    public function getCountry() {
        return $this->country;
    public function getPostalCode() {
        return $this->postalCode;


  • The facade pattern identify a simplicity, unified interface for the subsystem.
  • Design a wrapper class that encapsulates the subsystem
  • The client uses the facade only

Other patterns

Related Post

Leave a Reply