Design Patterns in Java : Dynamic Proxy for Logging

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
alright so in the previous examples we looked at how to construct proxies by specifically making new classes now what I'm going to show you is a much more powerful but somewhat computationally costly approach and that is the approach of dynamic proxies now this is important dynamic proxies are used in many places they're used in many frameworks and you should be aware of what dynamic proxies are and how to use them so the simple answer to what is a dynamic proxy is it's a proxy which is constructed at runtime as opposed to compile time so at runtime you take an existing object and you make a wrapper around it for example in order to intercept every single call to every single one of its methods so the best way to understand what the dynamic proxy is is to actually build one and see how it works so let me set up a very simple scenario so first of all I'll make an interface called human and let's suppose that our human is going to be able to do two things the human can walk and they can also talk all right so having made this interface we can now make a class let's call the class person which is going to implement this interface human and here I will simply generate the implementations and we're gonna fill them in with fairly simple stuff so here I'm just going to assist them out print line something like I am walking and the same goes for I am talking right here okay so we now have a class called person as well as an interface called human and we're going to try and build a dynamic proxy which takes an existing object of type person and counts the number of methods inside person that have actually been called so we need to build a dynamic proxy object and luckily for us Java gives us an interface for doing exactly that so we're going to build some sort of logging Handler and we're going to implement an interface which Java gives us called invocation handler so we're gonna say implements invocation handler and this is something from Java long reflect so it's a reflection interface and it's precisely the interface which allows us to intercept different methods now the way this is done is using the invoke method right here and we'll get to it in just a moment but first of all I want to initialize the logging Handler and show you what kind of information we're going to track inside it so we're gonna have two things the first thing that we're gonna have is a reference to the object which we are actually providing a proxy for remember this is all happening at run time so at run time you have to give it an existing object and tell it to basically take over its functionality while providing additional things so private final object talk that's what we're going to build a proxy for and the second thing that I want is I want a map which is going to record the number of method calls to the various methods that are called on the underlying object so here I have a private map and this map is going to go all the way from a string to an integer so it's going to map the names of different methods to how many times they've been called so I'm going to call this calls it's gonna be a new hash map like so now we're going to have to initialize the targets in the constructor so I'm going to define the constructor like this simply taking the target and saving it and now we are finally ready to implement the invoke method so the invoke method is basically a the idea that you get to invoke a particular method which is this method with this particular set of arguments so not a particularly scary concept but before we actually perform the invocation we might want to do some additional work like for example noting down the number of times that each method has been called so let's get the method name so here we have the reflection object for a method and I can say method don't get named make a variable out of it and then what I can do is I can increment the number of calls that have been made to this name inside our calls map so I can say calls don't merge I can specify the key of name and here I will either initialize the value one or I will add it to the existing value using integer sum so fairly simple stuff and then of course towards the end we actually do use the reflection api's to perform the invocation of the method so we return method invoke it gets to invoke on the target and the art set of arguments is args which is provided as another argument into this method okay so this is great now I'm going to do another trick here let's suppose that somebody calls to string on the decorated object let's suppose that that is precisely the point where I want to output the number of calls that have been made to the different methods so if the name of the method contains the word to string then what I'm going to do is instead of returning a default implementation like calling the underlying object I'm going to take my calls map I'm gonna turn into a string and return that instead we'll take a look at how it works in just a moment so let's go into our demo class and we can actually start using all of this to build a dynamic proxy for a person now we're going to have a utility method here for constructing a dynamic proxy with logging on any kind of object it doesn't have to be a person it can be virtually anything so it's going to be a static method it's going to be parameterized on a type T and it's going to return that type T by the way so I'm gonna call it with logging so we're going to take two things we're going to take the target which is the object for which the logging is required and we're going to specify as a class of T the interface that we want to receive on the output because remember we actually want to get a particular interface and a dynamic proxy for that interface so you cannot simply just take the underlying class and get that as the end result because that wouldn't work but you can get an interface so you'll get a dynamic proxy which conforms precisely to ITF we should hopefully explain why in this demo I have a class and I also have this class implementing an interface because it is precisely this interface that we're going to give out at the end of the with logging invocation okay so here we use the Java API suisse a proxy dot new proxy instance and we make a proxy now this method call it takes three arguments the first is the class loader for whatever interface you want so ITF dot I get class load the second argument is just a list of all the interfaces that you're interested in so here we'll just make a new array of class objects and there's only really one here but you can expand this example to have more than one interface being wrapped and then of course the final argument is the actual type that we are using for the dynamic proxy which in our case is called a logging Handler and remember it takes the target as the first and only argument so we are almost done we have to remember to cast the result of proxy new instance to the type T that we're returning and also seeing how we have an unchecked cast here and your ID may or may not complain about this I'm going to suppress a warning here so I'm gonna go in to suppress warnings and suppress the unchecked cast warning like so okay so now that we are done with this entire setup what we can do is we can start using it so coming down here first of all let's make a person so here is just an ordinary person and then I'm going to construct a dynamic proxy with logging so I'm gonna say with logging I'll provide the person as the argument and the second argument is that interface that I want to receive now remember person implements the human interface so here I can say human class and that specifies the actual interface and let's make a variable called logged for example now let's just check that it actually works so what I'm going to do is I'll take logged which remember it implements the human interface I will call the talk method once and maybe I will call the walk method twice like so and then we can try out this two string thing that we've implemented right here so this chunk of code basically checks that if somebody's calling to string on the proxy you return the number of calls so that map that we've constructed up above we actually return the contents of this map turned into a string so let's try doing exactly that now this will be done automatically when you do system dot out dot print line because remember if you just pass in an object it's gonna call to a string on it so here I just pass logged and we are done this is our demo essentially so let's actually run this and let's see that we get the right output here and as you can see we are getting the correct the output so we're calling I am talking I am walking I am walking and then we're getting the summary and the summary shows us that the talk method has been called once and the walk method has been called twice so this is a very small illustration of the kind of power that is afforded to us by dynamic proxy so here all that's happened is essentially just by implementing a single interface for an invocation handler you have a point where you intercept the invocation of every single method and when that method gets invoked before it actually gets invoked right here you can perform some additional processing which is pretty much what proxies are actually all about
Info
Channel: hamza djamaa
Views: 5,451
Rating: undefined out of 5
Keywords: udemy, udemy course, online tutorial, online learning, udemy review, udemy coupon, udemy voucher, udemy discount
Id: T3VucYqdoRo
Channel Id: undefined
Length: 9min 38sec (578 seconds)
Published: Fri Mar 02 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.