I want to show you something of a real world example of using an adopter so here instead of see line
I've got visuals to the open.
And the reason for that is that I have a MFC application so MFC Microsoft Foundation Classes is a somewhat
old technology but it's a technology for rendering things on the screen a visual application.
So we're going to use that to take a look at how the adapter would work.
Now I want to show you a simple function here called drop points.
So let's suppose that this is the interface we have now that is the only interface which is going to
allow us to draw things on the screen.
So this works through individual pixels and as you can see we have a bunch of points I'm going to define
point in the second and we simply set each of the corresponding pixels.
Now this idea of points is all well and good.
But let us suppose that the data we have in our own component isn't actually made up of points.
Let's suppose that it's made up of lines.
So this is a particular problem because if you have lines and you need points well you are in trouble.
So let's take a look at how this would work.
So I'm going to add a new header file and this header file is going to be called Geometry and that's
what we're going to define the geometric objects that we want to draw geometry or age.
So instead of pixels what we're going to be working with is lines but of course lines have a beginning
and an end which can be defined as a point points I'm going to have as truck whole point here and it's
going to have coordinates x and y.
There we go.
So this is my point.
I can now define a line so a line would be simply a collection of start and end points.
So we're going to have points start to call me and.
And that's pretty much it for now.
So what we can do now is we can define a vector object and that is quite simply an object which is made
up of many lines.
So the way we can define it is as follows.
We can say that we're going to have a struct called a vector object.
And I'm just going to give it an interface which returns a vector of line iterators so all the vector
object is going to do is have the member begin and then function which returns some sort of a vector
either.
So I'm going to have virtual vector of line iterator and it is going to be called begin.
And yeah we do need maybe an S be in here just so that it works.
We're going to replicate this and have the same called.
And so this is a very simple interface for just you adding a bunch of different lines.
In fact at least now if you want to define a rectangle we have to define it in terms of these vectors
so we can have a struct called a vector rectangle which is going to be a factor object.
And here in the constructor you can take for example the x and y coordinates of the rectangle as well
as the width and height and actually initialize the individual line.
So somewhere in the private section you would have a vector of lines that represent this particular
rectangle.
And then in the constructor actually see how we can generate a constructor in the constructor.
We can actually add the individual parts on the basis of the x y with and the height.
So I'm going to have x and y in width and height like so.
OK so we're going to initialize all of these I'm actually going to just paste in implementation because
it's rather tedious.
So here are these.
So here are the four lines which actually make up the rectangle and we've got this nice vector implementation
going on.
Unfortunately coming back to the CPV file here as I've shown you before we basically have a function
which only draws pixels it doesn't know how to draw a vector or objects at all which is a bit of a problem
and this is exactly the kind of situation where you would need to build an adapter.
So let's imagine that we are in CVP fall here let me actually just stick it in here.
Let's suppose that we've got two rectangles that we need to draw.
So let me define them.
So we're going to have a vector.
I'm going to have a share of yours here.
So we're going to have two vector objects and call them vector objects.
And we're going to have just two rectangles.
So here I will.
Well let's just make sure they share a vector rectangle
like so.
Let's suppose the coordinates are 10 and 10 and the size is a 100 by a hundred.
And we'll duplicate and make another one so we'll make a second rectangle and this one is going to have
coordinates of 30 and 30 for example and the size is going to be 60.
So they'll have the same center effectively.
So 60 by 60 so we have these rectangles but we cannot draw them because remember the API can only draw
pixels that cannot draw lines.
So what do we do while we build an adapter and this is the core of the adapter pattern so if we wanted
to build something we have to go something.
I guess we make a line to point adapter where for each line we actually generate the appropriate point.
So let me do a time def here.
We're going to call vector of points to be points and then we're going to have some sort of constructor.
See if I can generate an empty constructor here except it's not empty because we're adopting a line
we're adapting a line to a sequence of points.
So here we'll take line by reference and we're going to actually define all of the points which make
a line which means that somewhere down here in the private section again we need to actually store the
points points points remember points is just a typedef for a vector of point objects.
So that's what we're going to store here.
And then of course we might want to expose some iterators for actually exposing the different points
because well we have a vector here so we can expose the just the beginning and from that vector.
So virtue points iterator begin.
And we're going to return points that begin and we can do the same for points.
And so I'll just replicate this and change begin to end here as well as here.
That we go OK.
So the key functionality of the adopter is it has to take a line and give you a set of pixels for that
line.
So instead of once again boring you with the implementation I'm just going to stick I'm just going to
stick some code in here.
We'll take a look at it.
So most of it is just adding the individual points and you can see there are special rules here for
when vertical lines are horizontal lines a process that's the only two types of light we can actually
process.
And in addition I have a trs instruction here to tell us how many points we actually generated and
for which line and this is going to come in really useful later on.
So now that we have this line pointed after what we can do is whenever we want to actually rendah something
we can use this adapter.
So if we go into the function which actually renders something on the screen here what we can do.
Well let's go to its actual definition what we can do is we can draw the points right here so essentially
with the adapter I can go through each of the vector objects so I can say for auto 0 in vector objects
and remember each of the vector objects has begin and end members so we can go through each of the points
so for each of the.
Well first of all each vector object has a couple of lines so for auto L in every single vector object
remember there shared pointers so we have to have that asterisk in front of the show.
And then what we do is we actually use the adapter so we make line two points adapter called LPO which
is initialized from this particular line.
And then what we do is we draw the points so we have that interface already we draw the points with
the device context and we do LPO begin and LPO.
And to get the actual range of points that we want to draw.
So this is a very simple Im an implementation we can actually run this and see what we get.
Here we go so here is the actual output.
And as I kind of move the image on and off the screen you're going to be able to see the generated traces
here.
So let me actually kind of move the traces here and then we can play around with this window and actually
move it off the screen and move it onto the screen again.
OK so this is great.
Everything works correctly.
Now there's only one problem and that problem is what happens if I go ahead and I for example try to
resize the window or something.
So as I kind of drag it like this you can see that we're generating a lot more points if you look at
the trace here we are generating more and more points because there is no caching.
There is no caching of points so on every different window resize all that's happening is where regenerating
more of the points that should have already been generated because the lines are changing.
So in the next lesson we're going to take a look at how these temporary objects which the adapter spawns
can actually be cached.