added proxy sample

This commit is contained in:
Ilkka Seppala
2014-08-16 20:28:07 +03:00
parent 5cd611bf19
commit a9971dc38f
6 changed files with 84 additions and 0 deletions

View File

@ -0,0 +1,16 @@
package com.iluwatar;
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"));
}
}

View File

@ -0,0 +1,16 @@
package com.iluwatar;
public class Wizard {
private String name;
public Wizard(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}

View File

@ -0,0 +1,9 @@
package com.iluwatar;
public class WizardTower {
public void enter(Wizard wizard) {
System.out.println(wizard + " enters the tower.");
}
}

View File

@ -0,0 +1,19 @@
package com.iluwatar;
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!");
}
}
}