Proxy Design Pattern in Java (Program) (Software Design Patterns Tutorial)


It is the sample implementation of Proxy Design Pattern. I have already covered about Proxy Design Pattern theory, principles and real-life examples in the previous video.If suppose if you haven’t watched the video, please go and watch that video and then come this video. The video link would be present in the description box of this video. And then if suppose if you want to buy Software Design Patterns book then the book link and the book name would be present at the description box of this video. You can click on that link for buying the book online on Amazon. So let’s come in.. l et’s come to the point So here we are taking a use case called Download. So in our use case We have to download a file XYZ.movie from internet. So we have downloaded this XYZ movie from internet and three or four of our friends are asking for this movie. So we can do two things so first one is we can once again download this movie from Internet and then give this copy to the.. to our friends. A second one is we have already downloaded this movie from the internet. So we have that copy. We have the copy on local machine . We can give that the copied version to our friends So if you follow the first approach then we would spend much more data, and also the time. If we follow the second approach then we could have save some amount of data and also time. So we are trying to implement this using Proxy Design Pattern in this video. So here, this is in Download interface. In this download interface. There is only one abstract method called “void download”. So this is the Download interface here. We have only one abstract method called “void download” and then there are two implementation classes “RealDownload” and the “ProxyDownload”. “RealDownload”, downloads the required file. Required target file from the internet and it will store in the variable. So this is a “RealDownload”. Here “RealDownload” which implements Download interface and it has two local class variables called “targetField” and then “targetData”. “targetField” is the file that needs to be downloaded from internet. “targetData” is the downloaded data of the “targetFile”. So here the constructor, “RealDownload” constructor. Here, we need to specify the “targetFile” that needs to be downloaded Here, and then when we call this constructor. The thing… This method would be executed “DownloadFromInternet”. So if we Look at the “DownloadFromInternet”. Here, what this happening is “this.targetData”. This is target data that is getting assign. Here, this is a test data is getting assigned into “this.targetData” and then Since it implements a Download interface. We need to “Override” the download method. Here we are overriding the download method “System.out.println(this.targetData)”. Here, we are printing the downloaded data and then there is a second implementing classes including implementing class that implements Download interface which is “ProxyDownload”. Here, what we are trying to do is we are trying to have the “RealDownload” object as a filled-in “ProxyDownload” object. So here, what we are trying to do is, if suppose if anyone calls this download method then first we will try to create the “RealDownload” object. So if “RealDownload=new” then we will try to create the “RealDownload” object using the “targetFile”. For the second, third, fourth and the next successive times, we are not trying to create the “RealDownload” object because it will check whether it’s null. So these.. for the second, third. fourth time the realDownload would be null So this stage would be skipped and then we will try to retain the local copy data. So this is the concept we can behind Proxy Design Pattern use case. Here, proxy demo. First what we are trying to do is, we are trying to create the object for proxy download class. This is the download “xyz.movie” is the target file. And then we are trying to print the data, download.download. For the first we wanted to happen is if realDownload=null. For the first thing the realDownload would be null and then realDownload=new realDownload. For the first time for the first invitation the real download object would be created. For the next successive method invitation This realDownload object won’t be created, it will return the locally copied data, so that’s the idea behind this sample implementation. if suppose if you have any other doubts, please try to comment those in the comment section. I will try to explain whenever I get time. Thanks for watching.

Leave a Reply

Your email address will not be published. Required fields are marked *