0

I have gone thru several examples but I didn't found any examples having displayed same example With Proxy Pattern and Without Proxy Pattern,

Any one have a generic example ? by seen such example surely will understand do we really need Proxy Pattern to be used or not

1 Answer 1

1

The Proxy pattern is applicable whenever there is a need for a more versatile or sophisticated reference to an object than a simple pointer. Here are several common situations in which the Proxy pattern is applicable:

  • control access to another object
  • lazy initialization
  • implement logging
  • facilitate network connection
  • to count references to an object

For a simple example let's consider a scenario where a group of wizards want to enter a tower but there is a proxy guarding the resource and allowing only three first wizards to pass.

public class Wizard {

    private String name;

    public Wizard(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }

}

public class WizardTower {

    public void enter(Wizard wizard) {
        System.out.println(wizard + " enters the tower.");
    }

}

public class WizardTowerProxy extends WizardTower {

    private static final int NUM_WIZARDS_ALLOWED = 3;

    private int numWizards;

    @Override
    public void enter(Wizard wizard) {
        if (numWizards < NUM_WIZARDS_ALLOWED) {
            super.enter(wizard);
            numWizards++;
        } else {
            System.out.println(wizard + " is not allowed to enter!");
        }
    }
}

public class App {

    public static void main(String[] args) {

        WizardTowerProxy tower = new WizardTowerProxy();
        tower.enter(new Wizard("Red wizard"));
        tower.enter(new Wizard("White wizard"));
        tower.enter(new Wizard("Black wizard"));
        tower.enter(new Wizard("Green wizard"));
        tower.enter(new Wizard("Brown wizard"));

    }
}

Console ouput:

Red wizard enters the tower.
White wizard enters the tower.
Black wizard enters the tower.
Green wizard is not allowed to enter!
Brown wizard is not allowed to enter!

As requested, here is the same example code now with the WizardTowerProxy code merged into WizardTower.

public class Wizard {

    private String name;

    public Wizard(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }
}

public class WizardTower {

    private static final int NUM_WIZARDS_ALLOWED = 3;

    private int numWizards;

    public void enter(Wizard wizard) {
        if (numWizards < NUM_WIZARDS_ALLOWED) {
            System.out.println(wizard + " enters the tower.");
            numWizards++;
        } else {
            System.out.println(wizard + " is not allowed to enter!");
        }
    }
}

public class App {

    public static void main(String[] args) {

        WizardTower tower = new WizardTower();
        tower.enter(new Wizard("Red wizard"));
        tower.enter(new Wizard("White wizard"));
        tower.enter(new Wizard("Black wizard"));
        tower.enter(new Wizard("Green wizard"));
        tower.enter(new Wizard("Brown wizard"));
    }
}

Console ouput:

Red wizard enters the tower.
White wizard enters the tower.
Black wizard enters the tower.
Green wizard is not allowed to enter!
Brown wizard is not allowed to enter!
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for reply , I was expecting 2 examples side by side comparing without proxy implementation and with proxy implementation.So for the above example , implementing "only three first wizards to pass" constraint/condition inside single classed example (i.e. all written inside Wizard class) would be the NON PROXY EXAMPLE I guess ???
@Sanjeewa I have added another example that has the same functionality, this time without the proxy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.