added proxy sample
This commit is contained in:
16
proxy/src/main/java/com/iluwatar/App.java
Normal file
16
proxy/src/main/java/com/iluwatar/App.java
Normal 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"));
|
||||
|
||||
}
|
||||
}
|
16
proxy/src/main/java/com/iluwatar/Wizard.java
Normal file
16
proxy/src/main/java/com/iluwatar/Wizard.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
9
proxy/src/main/java/com/iluwatar/WizardTower.java
Normal file
9
proxy/src/main/java/com/iluwatar/WizardTower.java
Normal file
@ -0,0 +1,9 @@
|
||||
package com.iluwatar;
|
||||
|
||||
public class WizardTower {
|
||||
|
||||
public void enter(Wizard wizard) {
|
||||
System.out.println(wizard + " enters the tower.");
|
||||
}
|
||||
|
||||
}
|
19
proxy/src/main/java/com/iluwatar/WizardTowerProxy.java
Normal file
19
proxy/src/main/java/com/iluwatar/WizardTowerProxy.java
Normal 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!");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user