- Overview
- Transcript
3.3 Proxy
The proxy pattern is a useful pattern when you want to add additional functionality to source code that you may not have access to. With the proxy pattern, we can wrap existing functionality in a common interface and add whatever sort of extra logic is necessary.
1.Introduction2 lessons, 04:27
1.1Introduction01:39
1.2Prerequisites02:48
2.Creational Patterns5 lessons, 52:47
2.1Factory10:16
2.2Abstract Factory12:24
2.3Singleton09:09
2.4Prototype09:18
2.5Builder11:40
3.Structural Patterns7 lessons, 1:05:54
3.1Adapter10:07
3.2Flyweight10:33
3.3Proxy05:53
3.4Bridge10:35
3.5Decorator11:44
3.6Composite09:43
3.7Facade07:19
4.Behavioral Patterns9 lessons, 1:25:42
4.1Iterator09:32
4.2Command07:48
4.3Chain of Responsibility13:47
4.4Mediator08:16
4.5Memento08:53
4.6Interpreter14:31
4.7Observer08:58
4.8Strategy07:36
4.9State06:21
5.Conclusion1 lesson, 02:42
5.1Conclusion02:42
3.3 Proxy
The next structural pattern that I would like to spend some time talking to you about is going to be the ProxyPattern. Now, the ProxyPattern is another one of those that you may have seen out in the wild before. And probably have even done before, but the basic idea is this. Let's say that we have some sort of protocol or some sort of class implementation that does some sort of work. Now, we may not have access to this. Maybe it's in the form of a third party library, or something that we don't necessarily have the source code for. But there are somethings that we wanna do around the operations of this class or this protocol that doesnt exist within it's implementation. And we aren't able to put it in that implementation. How are we able to do it outside of it? So that it feels like we are doing on the actual implementation itself, but we still have that flexibility to do whatever it is. We need to do around it, either before or after those operations. And that's where the ProxyPattern comes into play. So we're gonna create a new Playground. And within here, let's just kinda set things up a little bit. Let's create a protocol, and let's say this protocol is called the, how about the HardworkerProtocol. So this thing is gonna do some amazing things. And it's gonna have a function in it, and it's gonna be called doSomeHardwork. So this is really gonna be a big intensive protocol that has any sort of implementation that touches it is gonna be big time, absolutely be big time. So let's go ahead and create an instance of that. Let's say we wanna a class called HardWorker, and it's going to implement the HardWorkerProtocol. So we know then it's gonna need to do some hard work. And of course, we're just gonna print something out here that says look at me, I'm really doing some hard work, there you go. So imagine that this is the implementation of some sort of functionality, some method, or whatever have you. In some third party library that we're using but we wanna augment this. So maybe before we do some hard work or maybe after it or both, we need to do something. Maybe we need to check some authorisation, maybe we need to do some error handling, whatever have you. But we don't necessarily have access to this. This is locked away somewhere in another module and in a library somewhere. So how are we able to inject our own functionality around this? Well, like I said before, the way that we're gonna do that is via a proxy. So let's create another class. And this class is going to be called the HardWorkerProxy. And it's also going to implement the HhardWorkerProtocol. So, we're gonna create this proxy that's going to then basically wrap this functionality in its own functionality. So now, I'm going to create an instance, a private instance, of this worker. So this worker is going to be the HardWorkerProtocol or whatever implementation of that HardWorkerProtocol there is. And so, because of that, we're gonna have to initialize this. So I'm gonna pass in a worker that is going to be an instance of that HhardWorkerProtocol. So we are going to set our internal worker to be equal to whatever we passed in. So now, I have that initialized but then I also have to implement, do some hard work, and this is where this proxy kind of comes in to play. So the idea behind the proxy is I'm really just creating another wrapper around this implementation. And I'm going to proxy the call. So every time I wanna call do some hard work on this hard worker implementation, I'm gonna create my proxy. And I'm simply going to wrap this hard work do some hard work in my own proxy's version of that does some hard work. So what can I do once I've done that, well, I can do a number of things, I can do whatever sort of implication, or whatever sort of logic or functionality I need here. But for this instance I am just going to put some silly text in here. So let's say maybe before I do hard I need to take a deep breath or whatever sort of functionality I need in there. Then, I can go into that worker than I have and I can say do some hard work. And then maybe afterwards, I can say after I do hard work, I need to take a break cuz it's just too much hard work. All right, so now I have this proxy class that's wrapping this do some hard work functionality. And now, I'm able to add some logic either before or after I make the call to that sub instance of that HardWorkerProtocol. So let's go ahead and give this a run, let's say var worker is gonna be equal to a HardWorker. And now, I wanna create a proxy, so I'm gonna say my proxy is going to be a HardWorkerProxy. And I'm gonna pass into that my worker. So now, I can say proxy.doSomeHardWork, and once I execute that you're gonna see that I'm going to get not only the functionality of that hard worker that's been pre-defined in some sort of library or module or something like that. But I've also been able to execute some code around it. Based on my proxies you can see here, so I can do before I do hard work I need to take a deep breath. Look at me I'm really doing some hard work. And then finally, after I do that hard work I really need to take a break, that's definitely important. So there you have it, that is the basic idea of a proxy, where I'm wrapping functionality of some other bit of code. And then, I can put my own logic around it, either before or after it, based on that same protocol







