Click on text below to jump to specific point in the video. Click and drag lower right corner to resize video.


This is John from caveofprogramming.com. In this tutorial we are going to look at the facade pattern. A facade pattern just means using a class, typically that provides you with a simpler interface to some other classes. In other words, a facade.

In Eclipse, I have a class called App with a main method.
The idea here is I am simulating a little game. If you write a game what you typically do is you will have part of program called the game loop, which is just a massive loop which will go on forever.

I will just have an infinite loop for simplicity here and then use it within the game where you get some sort of user input.
Then you update game object, in other words, player and bad guys and so on. And then finally you draw the game object to the screen. This is a typical pattern in a game.

And I have created some dummy objects we might use.
Here I have got an InputSystem class which is a dummy implementation. The idea is that this class will get the input from the user. They we have got a GameObjects class which contains different objects in the game - bad guys, good guys etc. And we have a GameConsole object whose responsibility is to take the list of objects and draw them to the screen so the user can see them. To actually run our game, we need some objects - let us instantiate those. We have InputSystem object. We will need a GameObject. We will need a class to represent the screen - GameConsole class.

Now if we were not using facade pattern, we would say input.getInput() to get input from user.
Then we use this information to update the game object. And finally, we will draw the game object using screen.draw.

This is not using facade, and the idea behind the facade pattern is that we can simplify these with another class.
This is not using the facade pattern. Let us create a new class called Game. This is going to be our facade class. Instead of having the GameObjects in main, let us have them as instance variables in the Game class. And we can just have one single method here - call it update(). Now we can cut all the code from main into the update method. Now in our main method, we can simplify the game loop a lot.We will have a Game object. And call game.update().

This is just one possible simple example of a facade type pattern and there is no really fixed definition of what facade means.
It is a general idea of simplifying code into a bunch of different objects like we have here. And creating a simplified API to those objects. We have hidden the complexity of the different objects.We have simplified it to this game object and this Game class is our facade.

Thats the facade pattern - very simple.

Video outline created using VideoJots