Lecture thumbnail 0:00 / 0:00 The adapter pattern is all about conforming an interface that you are given to the interface that you
actually have.
So let’s imagine that we’re doing a very simplified drawing application and let’s suppose that the only
thing which our system knows how to do is how to draw pixels, individual points on the screen.
So we may as well define a point with public fields X and Y and some sort of constructor to initialize
them.
And let us suppose that the only piece of functionality which we actually have available to us is the
functionality for drawing a point.
So we have some sort of draw point method which takes a point.
And in our case we’re just going to simulate the drawing of a point.
I’m not going to take its X and Y coordinates and try to set it on the screen, nothing like that.
So this is the only interface that we have.
Now.
Let’s suppose that somewhere in a different part of our application has built a vector drawing functionality.
So they’ve essentially built all the pieces which are required for vector drawings, and we don’t know
how to do that.
So let’s suppose that they decided to make a line composed out of two points, start and end once again
with a constructor.
And let’s suppose that they extended this idea with the idea of graphic objects which are made up of
collections of these lines.
So you have a class called, let’s say, Vector Object, which is a collection of lines.
Collection of lines like so.
And then they’ve gone ahead and whenever they wanted to define a particular shape like a rectangle,
they use this ID of a vector object and they simply initialize it in the constructor.
So in the constructor for the rectangle, for example, they would simply create the four lines and
add them to the collection, which would look something like this.
So in this case, if we have the rectangle taking the x and Y coordinates as well as the width and the
height, that would be how you initialize it.
So you make the four lines with the appropriate points and you add them to the collection.
Since we are a collection, we have the method called Add.
So this is how someone else has defined a rectangle as a vector object.
Now let’s suppose they decide to give us a bunch of these vector objects.
So let’s have private static readonly list of vector objects called vector objects.
So they give us this list and they say, Can you please draw it for us?
And we may as well initialize it with a bunch of members.
So you can see Resharper really stumbling here.
New list of vector objects.
There we go.
So I’m going to make two of these vector objects.
In fact, I’ll two, I’ll have two vector rectangles.
So we’ll have a rectangle one one with width, ten by ten.
It’s actually a square.
Let’s have another square.
So we’ll have a new rectangle with, let’s say coordinates three and three and size six by six.
And by the way, probably want to call them vector rectangles just to be explicit.
So vector rectangles.
There we go.
Okay.
So somebody comes up to us and they sell they give us this set of vector objects.
They say, Oh, look, I have two vector objects, two vector rectangles for you to draw.
And the point is we can only draw individual pixels, so we can only draw individual points.
And the question is, well, how can we then adapt one to another because we’re given vector objects
and we are kind of in the raster mindset.
So this is where you build an adapter, you build a tool which actually converts a single line because
all the vector objects are made up of lines.
You convert a line into a set of points because a line can be represented as a set of points.
So let’s build an adapter.
So we’re going to make a line to point adapter, and this is going to be a collection of points because
that’s what it returns.
We take a point, we take a line rather, and we try to convert it in all the points which exist on
this line.
So here, in addition to all of the other things that we’re going to be doing, I also want to keep
the count of the number of invocations of the line to point conversion.
So here I’ll have private static int count, which is going to be zero by default.
And then in the constructor what we do is we take a line, line, line like so first of all, let’s
write some information about this line.
So I’m going to write line here some info.
First of all, I’ll put in the number of invocations that we’ve made, number of conversions.
So plus plus count will give me the values one, two, three and so on for the number of times we’ve
called this.
And then I’m going to say that we’re generating points for line and then I’ll specify the line coordinates
here.
So we’ll begin with a line start x and line start y line start y.
Let’s put a comma in between them and we’ll put the ends in here.
So this is going to be line end dot x and line dot NY.
There we go.
So that’s some diagnostic information.
Probably got a spurious semicolon in here and then we can actually compute the coordinates of all the
different points.
Now, once again, I’m not going to bore you with typing out all this code because it’s actually quite
boring, but here it is.
So we essentially calculate the left, right, top and bottom margins of the line.
We calculate its X and Y change and it’s either vertical or horizontal in our system.
So depending on what it is, we once again call the Add method and we generate a bunch of points corresponding
to this line and we add them to ourselves because we are ourselves a collection of points.
So having constructed this adapter, what we can now finally do is we can take these vector objects
and we can actually render them using the drawpoint functionality that we have.
So for each vector object in vector objects, what we’re going to do is we’re going to do another for
each.
So this here will be a.
For each var line in V0.
Remember, each vector object is just a collection of lines.
So for each of these, what we have to do is we have to make an adapter.
So we say var adapter equals new line to point adapter.
We feed the line to point adapter a line and we get a bunch of points as a result.
So a line to point adapter is a collection of points and that means that we can do adapter dot for each.
So I’m going to use more links for each here and I’m going to call draw point on each of those points.
Okay, So this is a very simple adaptation of a vector interface to what we have here, which is a raster
interface.
So once again, I can control F five to execute and we’re going to see just a bunch of dots, but we’re
also going to see that diagnostic information that we added.
Here we go.
So as you can see, we have the lines being generated or the points being generated for each of the
lines.
And you can see that for this line, for example, there are ten, I believe 10 or 11 points generated
and they’re shown here.
We could output them with their actual coordinates.
But I’m just putting dots out here so that you can see that something is in fact being converted.
So as you can see, we’ve generated a total number of eight different point sets for a line.
And this is okay until you refactor this and move it into a separate method.
Let’s actually select this and extract a method we’re going to call a method draw.
And then let’s take a look at what happens if you call draw twice.
If we execute this, you can see that we’ve now generated the lines or the point sets for the line 16
times and that was completely unnecessary.
So unfortunately, the side effect of using the adapter is that it generates a lot of temporary information.
And in the next clip, we’re actually going to see how to reduce this amount of temporary information
by using caching.
Play Stop Play Play Play Start Play