Facade Design Pattern: Structural Patterns

Share the Article

What

Facade design pattern is a type of structural pattern which hides the complexity of the system inside simplified interfaces. Facade pattern makes a complex system easy to use for outside world.

Let us take an example of a complex system which is composed of several smaller systems. In this case, to perform a simple job, the client needs to perform multiple steps. However, with Facade pattern, we shall generate a simplified interface. Finally, the client shall be able to perform required job with much more ease.

With Facade pattern, the client need not bother about multiple steps and operations for getting job done.

Why

Let us try to understand the need of Facade design pattern by taking an example of online Music app for Mobile.

In order to play online Music, app need to perform below tasks internally :

  • Check internet connection (mobile data, wifi, etc).
  • Check memory availability in mobile for icons and songs list.
  • Download icons and songs in mobile memory.
  • Create icon and songs list.
  • Display songs in app screen
  • Create app screen with play, pause and next icon.
  • Take input from user for song.
  • Check audio port for audio streaming
  • Finally play music.

An Example without using Facade Design Pattern

Lets us create a Simple Music app application performing above listed tasks + we will also try to learn client interaction with the app-

#include<iostream> //main header using namespace std;//for namespace class InternetConnection { private: bool m_status=true;; public: bool getInternetConnectionStatus() { return m_status; } }; class MemoryAvailablity { private: int m_Avail=10*1024; public: int getMemoryAvailable() { cout << m_Avail; return m_Avail; } }; class MusicApp { public: void DownloadIcons() { } void DownloadSongs() { } void CreateSongList() { } void DisplaySongIcon() { } void CreateAppInterface() { } void CheckAudioPort() { } void PlaySong() { cout<<"song is playing"<<endl; } }; int main() //Client-code { InternetConnection internetStatus; MemoryAvailablity memoryAvail; MusicApp musicApp; if( (memoryAvail.getMemoryAvailable()) >(2 *1024 )) { if( internetStatus.getInternetConnectionStatus() == true) { cout<<"2"<<endl; musicApp.DownloadIcons(); musicApp.DownloadSongs(); musicApp.CreateSongList(); musicApp.DisplaySongIcon(); musicApp.CreateAppInterface(); musicApp.CheckAudioPort(); musicApp.PlaySong(); } } }

As seen above, the client needs to understand the full functionality of the music app. Therefore, all the steps for playing music is called by client. For instance, the client is firstly creating dependent objects, such as, Internet Connection and Memory Availability. And then calls individual functions.

The drawbacks of above approach become visible, whenever the system wants to change the steps. In such scenario, the client must adapt the code accordingly. This means, the client has to understand the whole system complexity.

In order to solve above problem Facade design pattern is the best fit.

How

Lets try to adapt above online Music app with Facade design pattern.

In order to adapt app with facade pattern we need to follow below rules:

Create a Facade class

In this example, the Facade class MusicAppFacade contains all system components.

class MusicAppFacade { private: InternetConnection internetStatus; MemoryAvailablity memoryAvail; MusicApp musicApp; };

Create a simplified interface in Facade class

Create a unified interface for set of interfaces in the system as below.

class MusicAppFacade { public: void openandPlayMusic(); };
Facade Design Pattern in UML
Facade Design Pattern

MusicApp Example using Facade design pattern

#include<iostream> //main header using namespace std; //for namespace class InternetConnection { private: bool m_status=true;; public: bool getInternetConnectionStatus() { return m_status; } }; class MemoryAvailablity { private: int m_Avail=10*1024; public: int getMemoryAvailable() { cout << m_Avail; return m_Avail; } }; class MusicApp { public: void DownloadIcons() { } void DownloadSongs() { } void CreateSongList() { } void DisplaySongIcon() { } void CreateAppInterface() { } void CheckAudioPort() { } void PlaySong() { cout<<"song is playing"<<endl; } }; class MusicAppFacade { private: InternetConnection internetStatus; MemoryAvailablity memoryAvail; MusicApp musicApp; public: void openandPlayMusic() { if( (memoryAvail.getMemoryAvailable()) > (2 *1024 )) { if( internetStatus.getInternetConnectionStatus() == true) { musicApp.DownloadIcons(); musicApp.DownloadSongs(); musicApp.CreateSongList(); musicApp.DisplaySongIcon(); musicApp.CreateAppInterface(); musicApp.CheckAudioPort(); musicApp.PlaySong(); } } } }; int main() //client-code { MusicAppFacade f1; f1.openandplay(); }

As seen above, now we have an updated MusicApp with Facade design pattern. With this approach, now the client uses a very simple interface openandPlayMusic( ) in the MusicAppFacade class. The client need not bother about system complexity any more. Also, any additional steps or reduction in steps in MusicApp class shall not affect the client code.

Pros and Cons of Facade pattern

PROS

  • Firstly, it hides the system complexity behind the Facade class.
  • Secondly, the client is able to easily integrate complex libraries and code with little understanding also.
  • Finally, future adaptations become possible without much changes on client side.

Cons

  • The additional Facade class ultimately becomes owner of your system.

Main Funda: The facade design pattern only beautifies the system and completely optional to use.

Advanced C++ Topics

Abstract Factory Design Pattern
Singleton Design Pattern
Factory Method Design Pattern
Builder Design Pattern
Adapter Design Pattern
Prototype Design Pattern
How std::forward( ) works?
How std::move() function works?
What is reference collapsing?

Share the Article

Leave a Reply

Your email address will not be published. Required fields are marked *