Sanjeewa
Sanjeewa

Reputation: 83

With Proxy Pattern and Without Proxy Pattern

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

Upvotes: 0

Views: 141

Answers (1)

iluwatar
iluwatar

Reputation: 1803

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!

Upvotes: 1

Related Questions