Lecture thumbnail 0:00 / 0:00 We’re going to begin by looking at a very synthetic example of a protection proxy.
And a protection proxy is something which checks whether you have the right to call a particular method
or whether you have the right to access a particular value.
So here is a classic example.
Let’s suppose that we have an interface called Eicar and it has a single method called drive.
So this method can be implemented by an actual car.
So we’re going to have an actual car which implements Eicar and here I’m going to implement the member
and here for driving a car, I’ll just write line that the car is being driven.
Okay?
Now what we want to do is we want to control people who are too young.
Don’t go ahead and drive the car because that could be dangerous.
So what we do is we make a car proxy and the car proxy is something which has the same interface as
a car but performs additional checks.
So here what we can do is we can make a class called Car proxy, which is also going to implement a
car.
It’s also going to implement a car.
So it has to implement that drive member.
But in addition, what you can do is you can, for example, have a constructor which takes a driver.
So we can think of a driver as just a person which has an age.
So I’ll have a public int age here and then we take the driver in the constructor.
So I’ll have driver driver and I’ll initialize it in the constructor like so.
And then we change the behavior of drive.
So even though we can aggregate the car here so we can have private car, car equals new car and that’s
the car that’s going to be driven.
We can actually check whether or not this particular person is actually old enough to drive.
So here we can say if driver dot age is greater than or equal to 16, for example, then yes, it’s
okay.
You can take the car and you can drive it.
Otherwise what we can do is we can, for example, write line that you are too young, too young, like
so.
So this is how you implement a simple protection proxy and we can start using it so we can say iCar.
iCar car equals new car proxy.
So notice if you had a car here, you could change it to a car proxy without any problems because they
have identical APIs.
And here we can make a new driver who is age 12 and let’s make a constructor for the driver just so
that it’s a bit more convenient.
So I’ll do CDP here.
There we go.
And now we can try driving the car and we can see what the result is.
So we can say car drive like so and execute this.
And we are saying to young, and if we change the age to 22, for example, then now the car is being
driven.
So this is an example of a protection proxy.
And you see these sorts of things in frameworks like ASP.Net where you want to check whether the current
user has the access rights to call a particular method or to access a particular rest resource.
For example, you simply make a proxy over the existing component.
You replicate the API.
So it’s very similar to other design patterns like decorator, where you replicate the API.
But the key thing here about the proxy is you don’t really add any new members.
You add new functionality in existing members, but you don’t go ahead and add some other members as
well.
Play Stop Play Play Play Play Play Play Play Start Play Play