added proxy sample
This commit is contained in:
parent
5cd611bf19
commit
a9971dc38f
pom.xml
proxy
1
pom.xml
1
pom.xml
@ -29,6 +29,7 @@
|
||||
<module>decorator</module>
|
||||
<module>facade</module>
|
||||
<module>flyweight</module>
|
||||
<module>proxy</module>
|
||||
</modules>
|
||||
|
||||
<build>
|
||||
|
23
proxy/pom.xml
Normal file
23
proxy/pom.xml
Normal 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>
|
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!");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user