Welcome to my Proxy Design Pattern Tutorial! The Proxy design pattern limits access to just the methods you want made accessible in another class.
It can be used for security reasons, because an Object is intensive to create, or is accessed from a remote location. You can think of it as a gate keeper that blocks access to another Object. I demonstrate how the proxy pattern works using some code used in my State Design Pattern Tutorial. You may want to check that tutorial out before proceeding.
If you like videos like this, it helps to tell Google by clicking here [googleplusone]
Share it if you like
Code from the Video
ATMMACHINE.JAVA
public class ATMMachine implements GetATMData{ public ATMState getYesCardState() { return hasCard; } public ATMState getNoCardState() { return noCard; } public ATMState getHasPin() { return hasCorrectPin; } public ATMState getNoCashState() { return atmOutOfMoney; } // NEW STUFF public ATMState getATMState() { return atmState; } public int getCashInMachine() { return cashInMachine; } }
GETATMDATA.JAVA
// This interface will contain just those methods // that you want the proxy to provide access to public interface GetATMData { public ATMState getATMState(); public int getCashInMachine(); }
ATMPROXY.JAVA
// In this situation the proxy both creates and destroys // an ATMMachine Object public class ATMProxy implements GetATMData { // Allows the user to access getATMState in the // Object ATMMachine public ATMState getATMState() { ATMMachine realATMMachine = new ATMMachine(); return realATMMachine.getATMState(); } // Allows the user to access getCashInMachine // in the Object ATMMachine public int getCashInMachine() { ATMMachine realATMMachine = new ATMMachine(); return realATMMachine.getCashInMachine(); } }
TESTATMMACHINE.JAVA
public class TestATMMachine { public static void main(String[] args){ ATMMachine atmMachine = new ATMMachine(); atmMachine.insertCard(); atmMachine.ejectCard(); atmMachine.insertCard(); atmMachine.insertPin(1234); atmMachine.requestCash(2000); atmMachine.insertCard(); atmMachine.insertPin(1234); // NEW STUFF : Proxy Design Pattern Code // The interface limits access to just the methods you want // made accessible GetATMData realATMMachine = new ATMMachine(); GetATMData atmProxy = new ATMProxy(); System.out.println("\nCurrent ATM State " + atmProxy.getATMState()); System.out.println("\nCash in ATM Machine $" + atmProxy.getCashInMachine()); // The user can't perform this action because ATMProxy doesn't // have access to that potentially harmful method // atmProxy.setCashInMachine(10000); } }
ATMState.java
public interface ATMState { void insertCard(); void ejectCard(); void insertPin(int pinEntered); void requestCash(int cashToWithdraw); }
The best place on the internet that I found to teach you Design Patterns.
Thank you very much!
You’re very welcome 🙂 I thought this topic needed some attention
Hi,
Thanks for providing the best ever tutorial on design patterns.
Do you have something in Java concurrency also?
Regards.
ashg
You’re very welcome 🙂 Sorry, but I haven’t covered concurrency yet, but I’ll definitely cover it as soon as possible
Excellent site to learn Design Pattern.THanks a ton for your effort.
Request you to kindly add some tutorials for Java Concurrency.
Thanks
Thank you 🙂 I will definitely cover that topic. I won’t stop making java tutorials until I cover everything
Thanks for ur reply.
these video lectures helped me a lot.
thanx sir 🙂
You are very welcome 🙂
Nice tutorials, but you forget to include “ATMState.java” code.
Thank you 🙂 I updated the page and added the code. thank you for telling me about that
Hi Derek,
The code you provided here is incomplete. There are a lot of staff missing here, including some classes from your “State Design Pattern” code, where some classes should be refactored to suit “Proxy Design Pattern” needs.
Thanks
Just update, classes from “state dp” should be included, but not refactored. It’s “ATMMachine.java” class is incomplete
Hello sir,
I have seen almost all of your videos about design patterns. This help me a lot in polishing my java skills. And yes Of’cousre You have the Most innovative method of teaching. God Bless You. Keep Rocking !!
Thank you very much 🙂 I did my best to teach in a new way and I’m glad that it seems to help some people. May God bless you as well.
Your videos are simply awesome. Keep it up
Thank you very much 🙂
Very well explained. Is there any way we can get access to the slide and UML diagram shown at the beginning of the video? This goes for all videos on design patterns.
Thank you 🙂 I have every presentation available as a big PDF. It is a bit messy because I never meant to make it available to the public, but here is the link
Thank you so much for sharing the link. Really appreciate it.
You’re very welcome 🙂
Hi Derek,
I have a confusion. Does the methods in Proxy not return initial states of a fresh object all the time? Since, it is freshly allocated there and returned.
Regards
Abhinav
Hi Abhinav,
Yes you are correct. I dramatically simplified the ATM part here. I’m assuming that it would actually be connected to a database and that the ATM object would just provide already created and stored data. I should have made it clear in the tutorial that I wasn’t fully developing the ATM because I thought that would distract from the pattern. Sorry about that
Awesome example and great explanation. Hats off to you for such nice tutorials on design patterns. Thank you so much. Please keep up the good work. God bless you.
Thank you very much 🙂 May God bless you and your loved ones as well.