A facade is typically built on top of a complicated system.
So rather than building an example from scratch what I'd like to show you is one of my projects.
Now this project is called the bloom and what it basically does is it renders a open G.L. text terminal
which is similar to the ordinary command line console's that you might have in your operating system
except this one is configurable.
So here for example what I'm doing is making a console which has three view ports.
So three vertical columns which actually show the same tech So we have one two three four five being
shown in every single one of those calls.
But as you can see the API here is particularly complicated.
So if I actually execute the program let me just move the console over you can see that I basically
have three columns here but they know this you have these three columns.
I seem to be jumping through far too many hoops in terms of implementing a lot of things.
So here I'm making a buffer and I'm adding this buffer to the different viewpoints and emitting the
viewport to the window and then I'm showing the window.
So I would like some sort of facade which would actually hide all of these details behind a nice little
interface that I can simply take.
And for example I want an interface which lets me create a multi-column setup for example.
So that's exactly what we're going to be building today.
So I'm going to go into add new item and I'm going to add a new header file so I'm going to have a header
file which is going to store our facade.
I will call it console because that's exactly what we're trying to model here so I'm going to have go
at age and that's where we'll build a better API.
So here I'm going to need a lot of things I'll need some headers let me get in.
IO stream
string stream.
Just just a string I think is the one I am after.
I'll have vector.
I'll have a memory and I need some types from my own projects so I'll include a window that age because
we're going to be creating windows and I'll include text buffer as well because we're going to need
text buffers I'll do using namespace Asadi.
Now we can actually make a class called console.
So this is going to be the facade which is going to save me from the creation of those buffers and view
ports and whatnot.
So let's say public section now.
First of all what I want is I want the console to be a singleton.
I don't want people supporting lots of consoles so I'll make a very simple Singleton here instance where
I'll make a console console and I will return console.
And then of course we're going to have a bunch of what are essentially factory methods which will allow
us to make different kinds of consoles.
Now unfortunately we cannot make them static but we will have them available through this Singleton
So we'll have console instance create something or other.
Now the console is going to have a list of windows because we're essentially going to allow later on
to create many windows out of this console so I have a vector offset VTR of a window object so I'll
call this Windows.
And then of course we can actually go ahead and implement some of the functionality and also have a
private section where we can keep some of the defaults.
For example the defaults for the character size.
So the defaults for the console character with for example is going to be 10 and character height is
going to be 14.
In addition I'll have some constructors which are going to be private so that I enforce the singleton
so I'll have the constructor here.
I will also have the copy constructor being deleted and I will have the assignment operator being deleted
here as well.
I will go OK.
So coming back to actually being able to create multi-column console's for example I'll have a function
which returns a shared PCR of a window and I will define a multi-column window where we're going to
take the following arguments so we'll take the window title.
I will take the column count.
I will take the row count.
Actually that should be column with column count and the column with and then the set of characters
that we're going to actually be in.
In terms of the vertical size so the character height.
So you see how many characters.
Hi I want the console to be as kind of an adaptive console.
That's the whole point of the facade.
You basically make the API that you are comfortable working with.
OK so what I'm going to make here is I'm going to make a window
and I will initialize it with some of the arguments.
And then what I'm going to do is for every single column in the overall column count what I'm going
to do is I'm going to create a buffer and I'm going to make a viewport and tie everything together and
I'm not going to bore you with the actual implementation details.
Let me just paste them in here so I'm hiding some of the details.
By creating a buffer creating a viewport tagging the buffer and the viewport together adding it to the
window and I'm doing this in a kind of multi-column scenario.
Now what this lets me do is instead of doing all this horrible stuff that we've been doing here I can
actually use this facade to essentially construct a useful window for me to work with and that is that
is the ultimate goal here.
OK.
So coming back to Visual Studio let me just comment.
All of this out.
Well hopefully no comment on the opening color because that's quite important.
And then what we're going to do is we're going to make a window in a different fashion so I'm going
to say auto window equals console instance seeing how it's a singleton and yes I do need the include
directive here.
So a console instance does multi-column and I'll give it a title test.
And let's suppose that we have two columns and the column with is going to be for the characters.
And in terms of the number of characters hies those are going to be 4.
So these are all settings and what I can do now is I can start adding strings to the individual buffers
because essentially the window has been automatically constructed for me so I can make a for loop where
I'm going to go through 0 to 40.
And what I am going to do is I'll take the window.
I'll take the buffer at the next one so I can actually do that get the buffer here and I will call string
on it to actually write a string to the buffer.
And here I will make a string let's say this is a line and then I will cast the thing to a line so it's
half poost lexical cast to a string the integer and I.
And let's also have.
Well let's also have some sort of indicator of whether there is a second variable here indicating whether
you won the string to be bold or not.
Because that's what my console allows.
And I will do it in a kind of bold not bold way so I will take I I'll take the remainder of division
by 2 and if it's 0 then I will make it bold.
There we go.
OK.
This is our new implementation.
I have the window I can now well I've manipulating the window here calling unstring on it and let's
see if I got the number from brackets.
Yes.
Too many of them.
So now the window is a smart pointer I can dash great then and call show on it.
So we've now implemented the facade and we're using the facade to basically automatically create the
multi-column layout and and populate that layout.
So let's actually execute this and see what we get.
Right now I know it looks particularly weird.
But here is the final result.
So we've essentially managed to manufacture two columns one of them is empty here on the right.
But this column basically alternates ordinary text and some sort of weird formatted text so we have
one line of each shown up here and that is the whole point of the facade design patterns.
Basically taking these complicated constructions and sticking them into a simple API where you can just
have a single method for example which takes a limited number of arguments and all of the complicated
stuff is happening behind the scenes.