Proxy Design Pattern Tutorial

Proxy Design Pattern TutorialWelcome 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

Share it if you like

Code from the Video


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; }
	public ATMState getATMState() { return atmState; }
	public int getCashInMachine() { return cashInMachine; }


// 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();


// 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();


public class TestATMMachine {
	public static void main(String[] args){
		ATMMachine atmMachine = new ATMMachine();
		// 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);

public interface ATMState {
	void insertCard();
	void ejectCard();
	void insertPin(int pinEntered);
	void requestCash(int cashToWithdraw);

25 Responses to “Proxy Design Pattern Tutorial”

  1. Tadeu says:

    The best place on the internet that I found to teach you Design Patterns.
    Thank you very much!

  2. Ashgonline says:

    Thanks for providing the best ever tutorial on design patterns.
    Do you have something in Java concurrency also?


  3. Pratap says:

    Excellent site to learn Design Pattern.THanks a ton for your effort.

    Request you to kindly add some tutorials for Java Concurrency.


  4. Pratap says:

    Thanks for ur reply.

  5. Hamraz says:

    these video lectures helped me a lot.
    thanx sir 🙂

  6. Vassili says:

    Nice tutorials, but you forget to include “” code.

    • Derek Banas says:

      Thank you 🙂 I updated the page and added the code. thank you for telling me about that

      • Vassili says:

        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.

      • Vassili says:

        Just update, classes from “state dp” should be included, but not refactored. It’s “” class is incomplete

  7. 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 !!

  8. Edward says:

    Your videos are simply awesome. Keep it up

  9. Anonymous says:

    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.

  10. Abhinav says:

    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.


    • Derek Banas says:

      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

  11. Kedar says:

    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.

Leave a Reply

Your email address will not be published.