Implementation

Let’s say you want to fulfil your dream of having a smart house. You have bought a lot of smart devices from different providers and connected them to your network, but there is a problem — every device provides its interface (call it an API) so it becomes very tedious to manage different devices separately to accomplish a single task.

For instance, you want to watch a movie just like in the cinema. For this, you have to set up your house environment in a similar way:

Turn off the lights;

Turn on the TV;

Turn on the audio system;

Connect to some kind of movie streaming platform, e.g. Netflix;

Start playing the movie.

For all of these steps, you have to call several APIs just to set-up your environment. Wouldn’t it be nice just to say “Start playing The Matrix in home cinema mode” to your smart home assistant or turn on a single switch in your smart home mobile application and execute all of these steps as a single action? To implement this kind of functionality, the Facade design pattern is a great option!

Class diagram

The class diagram below shows the implementation of the Facade design pattern:

Class Diagram — Implementation of the Facade design pattern

There are several APIs provided to communicate with smart devices (turn them on and off): AudioApi, CameraApi, PlaystationApi, SmartHomeApi and TvApi. NetflixApi provides methods to connect to the Netflix platform, disconnect from it and play the selected movie.

APIs are used by the facade classes:

GamingFacade — uses the PlaystationApi and CameraApi and provides methods related to gaming and streaming actions;

SmartHomeFacade — uses the AudioApi, CameraApi, SmartHomeApi, TvApi and NetflixApi. It provides methods for gaming, streaming actions (GamingFacade is reused with some additional communication together with other smart devices) and actions related to playing a movie from the Netflix platform.

Both of the facades use the SmartHomeState class to save the current state of smart devices.

FacadeExample widget contains the SmartHomeFacade to communicate with the smart devices using the provided action methods in the facade.

APIs

AudioApi — an API to turn the smart speakers ON/OFF.

CameraApi — an API to turn the streaming camera ON/OFF.

NetflixApi — an API to connect to the Netflix platform, disconnect from it and play the movie.

PlaystationApi — an API to turn the gaming console (PlayStation) ON/OFF.

SmartHomeApi — an API to turn the smart lights ON/OFF.

TvApi — an API to turn the smart TV ON/OFF.

Kittens break

If you have read to this point, you definitely deserved this!

SmartHomeState

A class which holds the current state of all the smart devices at home.

GamingFacade

A facade class which uses APIs of the PlayStation and streaming camera and provides simplified methods to use them:

startGaming() — uses the PlaystationApi to turn the gaming console on;

stopGaming() — uses the PlaystationApi to turn the gaming console off;

startStreaming() — uses the CameraApi to turn the streaming camera on and calls the startGaming method;

stopStreaming() — uses the CameraApi to turn the streaming camera off and calls the stopGaming method.

SmartHomeFacade

A facade class which uses APIs of the smart TV, audio devices, Netflix platform and smart home equipment. Also, GamingFacade is used. Several methods are provided to simplify smart home actions:

startMovie() — uses several different APIs to turn off the lights, turn on the smart TV and speakers, connect to the Netflix platform and start playing the selected movie;

stopMovie() — uses several different APIs to disconnect from the Netflix, turn off the smart TV and speakers, also turns the lights back on;

startGaming() — uses the SmartHomeApi to turn the lights off, turns the smart TV on via the TvApi and calls the GamingFacade to start the gaming session;

stopGaming() — uses the GamingFacade to stop the gaming session, turns the smart TV off using the TvApi and turns the lights back on via SmartHomeApi;

startStreaming() — uses the SmartHomeApi to turn the lights on, turns the smart TV on via the TvApi and calls the GamingFacade to start the streaming session;

stopStreaming() — uses the GamingFacade to stop the streaming session, turns the smart TV off using the TvApi and turns the lights back on via SmartHomeApi.

Example

First of all, a markdown file is prepared and provided as a pattern’s description:

FacadeExample widget contains the SmartHomeState which hold the current state of smart devices and SmartHomeFacade to simplify the “smart actions”.

This widget only knows the simplified methods provided by the smart home facade but does not care about their implementation details, dependencies between classes or other facades and the amount of different APIs used to execute the single action. This allows implementing a complicated logic to handle the smart home actions just by turning the switch ON/OFF in ModeSwitcher widgets. Also, the implementation details of the smart devices’ handling methods in the SmartHomeFacade could be changed/improved without affecting the UI code.

The final result of the Facade design pattern’s implementation looks like this:

As you can see in the example, it is just enough to turn the switch on/off to communicate with multiple smart devices and change their state through the provided facade methods.

All of the code changes for the Facade design pattern and its example implementation could be found here.