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

@ -29,6 +29,7 @@
<module>decorator</module>
<module>facade</module>
<module>flyweight</module>
<module>proxy</module>
</modules>
<build>

23
proxy/pom.xml Normal file
View File

@ -0,0 +1,23 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.iluwatar</groupId>
<artifactId>proxy</artifactId>
<version>1.0-SNAPSHOT</version>
<name>proxy</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

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!");
}
}
}