Lecture thumbnail 0:00 / 0:00 The facade design pattern is actually quite boring to show because it really depends on what kind of

systems you want to expose through the facade.

So instead of doing some sort of clinical demo, I decided that I would show you something which I actually

use as part of algorithmic trading.

Now, as you probably realize when you’re working with the financial markets, the amount of data and

the frequency of updates is sometimes a bit too much.

And if you use an ordinary console, whether it’s a Windows console or the same thing as a terminal

on OSX or Linux, you’re going to end up with problems.

You’re going to end up with data being too slow to update.

So what we do in the real world is we build our own consoles and in this case, what I have is a console

which is built on a technology called DirectX X.

Now DirectX X is one of the two major graphics technologies, the other one being OpenGL.

And these technologies allow you to render images a lot faster by using the GPU to its full advantage,

by pre rendering every character in the console and then showing the entire console.

So first of all, let me show you what it looks like.

It’s not particularly exciting.

Here is a typical window and in the real trading setup, what happens is I have a window which actually

takes up several monitors and it prints lots of data.

And all of this data keeps updating dynamically in real time as we get more data from the exchanges,

as we open and close positions as the trading goes on.

And you also get to show clocks and all sorts of wonderful things which I unfortunately cannot show

here.

So essentially it’s a grid and the grid has a way of showing the different characters.

Now behind the scenes, the rendering of such a console is a very complicated affair consisting of several

subsystems.

So that’s what we’re going to take a look at.

The first subsystem is called a texture manager.

A texture manager is a tool which actually prepares a texture for every single character.

Now, you’re probably wondering why is an ordinary console so slow to render?

Well, essentially it’s slow to render because every time somebody requests a character to be rendered,

what essentially happens is somebody has to go into the font, you have to read the vector data, you

have to rasterize that data for a particular size, and then you have to render it using a 2D non hardware

accelerator technology like GDI, for example.

This is what you’re looking at right now.

You’re looking at, well, in this case it’s WPF, so it might be hardware accelerated to some degree,

but typically in the ordinary console it’s very slow precisely because the rendering takes up so much

effort and you cannot write thousands of lines per second to it.

So what we’ll do here is we make a texture for every single character, and obviously we’re limiting

ourselves to a couple of default fonts like consolas or silicon.

So we’re limiting ourselves to the Latin characters.

So none of that Unicode stuff.

If you’ve got your stock ticker written in Russian, well, that’s just too bad.

Basically, we don’t support that sort of thing.

So the texture manager is a subsystem.

It is a part of the overall console, but we don’t really want to interact with the texture manager

too much.

We want to have a single unified interface.

I’ll get to that in a moment.

So essentially, in addition to the texture manager, we have this implementation of multiple buffers

and multiple viewports.

So if you look at this, for example, there are two different buffers.

One for the set of values on the left, one for the set of values on the right.

And by buffer we mean an area of memory.

So a one dimensional or two dimensional array where all of this data is actually kept.

So as a consequence of this, what we have as a single console is an interface into multiple buffers,

multiple views.

A view is just a projection of the buffer onto the screen as well as these extra things like the texture

manager for example.

So we have viewports and the viewport is a way of looking into a buffer.

So it keeps a reference to the buffer and it specifies the size of the viewport and the point into which

you’re looking at the buffer and so on and so forth.

And then of course we have the console as the facade.

So the console has typical things like write line and this is a simplified interface over more and more

complicated operations.

So if you look at write line and if you try to follow it, you’ll see that it’s actually a call to write

format.

And when you look at write format, you can see that we’re manipulating viewports, We’re getting the

viewport at the current index.

We’re checking its buffer.

If it’s not null, then we use the buffer to write to the viewport.

If we go into this, then we see that the implementation of writing to a buffer is very sophisticated

because we ignore certain characters.

We make sure that we break on words and there is plenty of customization and there is the overflow.

So if your buffer overflows, you start writing to the beginning of the buffer and those are an internal

pointer which tells you where to start and where to finish reading the buffer.

So it’s a very sophisticated system.

But if you look at how it’s actually.

Lamented then For the most part.

For the most part, it’s a very simple affair in the sense that you can go and you can well, suddenly

here we are doing something simple.

Like we’re saying console, dot, create.

Now if you look at create, you can see that in actual fact, there’s plenty of customization going

on here.

You can specify a set of console creation parameters or you can provide a default.

And from then on, what you can do is you can do things like console, dot show and console dot render

to show the console.

So we’re showing a simplified interface on the console, but at the same time we allow you to do a few

other things, like if you want to customize the console with additional viewports, then those viewports

are added accordingly.

You can also create your own buffers and I’ve inherited from an ordinary buffer to make a table buffer

since our demonstration is actually a display of two tables here.

So that’s what we’re showing.

So the console in this case acts as a kind of gateway or indeed a facade.

The console is a facade for actually doing things.

And the console itself, if you look at the members of the console for a second, let’s just look at

some of the members.

You’ll notice that the console has its own right members.

Now, you might be wondering, well, hold on, if we have plenty of viewports and we have plenty of

buffers, how does it know which one to write to?

And the answer here is that we make educated guesses about where you would actually write text if you

had to write text.

So it would typically be the active viewport and the active viewport would be tracked by what has last

been written to or something to that degree.

So the console class acts as a classical facade.

On the one hand, by offering direct operations for writing something, let’s actually follow this method

for a second.

So as you can see, what we’re doing here is we are calling write, and if we go into write, you can

see that it’s a call to write format.

If we go into write format, once again, we have this idea of a current viewport index.

So we have an index of the current viewport that we’re working with.

And for that viewport we find the buffer.

And if the buffer is not equal to null, then we can actually write the appropriate text.

So that’s how you build a facade over what’s essentially a complicated subsystem.

Now the actual implementation of a facade depends on your subsystems and depends on how you want to

expose them.

So on the one hand, you might want to have just a nice shiny interface like the one I’ve showed right

now, but on the other hand, I give the possibility of going in more deep and manipulating things like

Viewports directly, if that’s what you want to do.

If you want a single viewport with a single buffer, you can easily get it.

That’s what the console gives you automatically.

But if you want customization, then additional options are available as well and that is a better demonstration

of a facade than any kind of clinical example I could come up with.

Play Stop Start Play information alert