Skip to content

Commit

Permalink
Proxy pattern: Improve the example
Browse files Browse the repository at this point in the history
  • Loading branch information
iluwatar committed Nov 23, 2016
1 parent 92f8501 commit 82d3d7b
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 89 deletions.
18 changes: 9 additions & 9 deletions proxy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,24 @@ Surrogate
Provide a surrogate or placeholder for another object to control
access to it.

![alt text](./etc/proxy_1.png "Proxy")
![alt text](./etc/proxy.png "Proxy")

## Applicability
Proxy 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

* a remote proxy provides a local representative for an object in a different address space.
* a virtual proxy creates expensive objects on demand.
* a protection proxy controls access to the original object. Protection proxies are useful when objects should have different access rights.
* Remote proxy provides a local representative for an object in a different address space.
* Virtual proxy creates expensive objects on demand.
* Protection proxy controls access to the original object. Protection proxies are useful when objects should have different access rights.

## Typical Use Case

* control access to another object
* lazy initialization
* implement logging
* facilitate network connection
* to count references to an object
* Control access to another object
* Lazy initialization
* Implement logging
* Facilitate network connection
* Count references to an object

## Real world examples

Expand Down
Binary file modified proxy/etc/proxy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 39 additions & 8 deletions proxy/etc/proxy.ucls
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
<class-diagram version="1.1.11" icons="true" automaticImage="PNG" always-add-relationships="false"
generalizations="true" realizations="true" associations="true" dependencies="false" nesting-relationships="true"
router="FAN">
<class id="1" language="java" name="com.iluwatar.proxy.WizardTowerProxy" project="proxy"
file="/proxy/src/main/java/com/iluwatar/proxy/WizardTowerProxy.java" binary="false" corner="BOTTOM_RIGHT">
<position height="142" width="214" x="260" y="438"/>
<position height="149" width="191" x="388" y="271"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
Expand All @@ -12,26 +13,56 @@
</class>
<class id="2" language="java" name="com.iluwatar.proxy.Wizard" project="proxy"
file="/proxy/src/main/java/com/iluwatar/proxy/Wizard.java" binary="false" corner="BOTTOM_RIGHT">
<position height="124" width="117" x="187" y="274"/>
<position height="113" width="102" x="619" y="271"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="3" language="java" name="com.iluwatar.proxy.WizardTower" project="proxy"
<interface id="3" language="java" name="com.iluwatar.proxy.WizardTower" project="proxy"
file="/proxy/src/main/java/com/iluwatar/proxy/WizardTower.java" binary="false" corner="BOTTOM_RIGHT">
<position height="106" width="130" x="344" y="274"/>
<position height="77" width="116" x="388" y="460"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</interface>
<class id="4" language="java" name="com.iluwatar.proxy.IvoryTower" project="proxy"
file="/proxy/src/main/java/com/iluwatar/proxy/IvoryTower.java" binary="false" corner="BOTTOM_RIGHT">
<position height="113" width="116" x="761" y="271"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="5" language="java" name="com.iluwatar.proxy.App" project="proxy"
file="/proxy/src/main/java/com/iluwatar/proxy/App.java" binary="false" corner="BOTTOM_RIGHT">
<position height="95" width="114" x="917" y="271"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<generalization id="4">
<association id="6">
<end type="SOURCE" refId="1" navigable="false">
<attribute id="7" name="tower"/>
<multiplicity id="8" minimum="0" maximum="1"/>
</end>
<end type="TARGET" refId="3" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<realization id="9">
<end type="SOURCE" refId="4"/>
<end type="TARGET" refId="3"/>
</realization>
<realization id="10">
<end type="SOURCE" refId="1"/>
<end type="TARGET" refId="3"/>
</generalization>
</realization>
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
Expand Down
Binary file removed proxy/etc/proxy_1.png
Binary file not shown.
14 changes: 7 additions & 7 deletions proxy/src/main/java/com/iluwatar/proxy/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* functionality to the object of interest without changing the object's code.
* <p>
* In this example the proxy ({@link WizardTowerProxy}) controls access to the actual object (
* {@link WizardTower}).
* {@link IvoryTower}).
*
*/
public class App {
Expand All @@ -45,12 +45,12 @@ 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"));
WizardTowerProxy proxy = new WizardTowerProxy(new IvoryTower());
proxy.enter(new Wizard("Red wizard"));
proxy.enter(new Wizard("White wizard"));
proxy.enter(new Wizard("Black wizard"));
proxy.enter(new Wizard("Green wizard"));
proxy.enter(new Wizard("Brown wizard"));

}
}
41 changes: 41 additions & 0 deletions proxy/src/main/java/com/iluwatar/proxy/IvoryTower.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.proxy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*
* The object to be proxyed.
*
*/
public class IvoryTower implements WizardTower {

private static final Logger LOGGER = LoggerFactory.getLogger(IvoryTower.class);

public void enter(Wizard wizard) {
LOGGER.info("{} enters the tower.", wizard);
}

}
2 changes: 1 addition & 1 deletion proxy/src/main/java/com/iluwatar/proxy/Wizard.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*/
public class Wizard {

private String name;
private final String name;

public Wizard(String name) {
this.name = name;
Expand Down
50 changes: 9 additions & 41 deletions proxy/src/main/java/com/iluwatar/proxy/WizardTower.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,9 @@
/**
* The MIT License
* Copyright (c) 2014 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.proxy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*
* The object to be proxyed.
*
*/
public class WizardTower {

private static final Logger LOGGER = LoggerFactory.getLogger(WizardTower.class);

public void enter(Wizard wizard) {
LOGGER.info("{} enters the tower.", wizard);
}

}
package com.iluwatar.proxy;

/**
* WizardTower interface
*/
public interface WizardTower {

void enter(Wizard wizard);
}
12 changes: 9 additions & 3 deletions proxy/src/main/java/com/iluwatar/proxy/WizardTowerProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,27 @@

/**
*
* The proxy controlling access to the {@link WizardTower}.
* The proxy controlling access to the {@link IvoryTower}.
*
*/
public class WizardTowerProxy extends WizardTower {
public class WizardTowerProxy implements WizardTower {

private static final Logger LOGGER = LoggerFactory.getLogger(WizardTowerProxy.class);

private static final int NUM_WIZARDS_ALLOWED = 3;

private int numWizards;

private final WizardTower tower;

public WizardTowerProxy(WizardTower tower) {
this.tower = tower;
}

@Override
public void enter(Wizard wizard) {
if (numWizards < NUM_WIZARDS_ALLOWED) {
super.enter(wizard);
tower.enter(wizard);
numWizards++;
} else {
LOGGER.info("{} is not allowed to enter!", wizard);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@
import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
* Date: 12/28/15 - 9:18 PM
*
* @author Jeroen Meulemeester
* Tests for {@link IvoryTower}
*/
public class WizardTowerTest {
public class IvoryTowerTest {

private InMemoryAppender appender;

@Before
public void setUp() {
appender = new InMemoryAppender(WizardTower.class);
appender = new InMemoryAppender(IvoryTower.class);
}

@After
Expand All @@ -58,8 +58,8 @@ public void testEnter() throws Exception {
new Wizard("Merlin")
};

final WizardTower tower = new WizardTower();
for (final Wizard wizard : wizards) {
IvoryTower tower = new IvoryTower();
for (Wizard wizard : wizards) {
tower.enter(wizard);
}

Expand All @@ -69,5 +69,4 @@ public void testEnter() throws Exception {
assertTrue(appender.logContains("Merlin enters the tower."));
assertEquals(4, appender.getLogSize());
}

}
7 changes: 2 additions & 5 deletions proxy/src/test/java/com/iluwatar/proxy/WizardTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,15 @@
import static org.junit.Assert.assertEquals;

/**
* Date: 12/28/15 - 9:02 PM
*
* @author Jeroen Meulemeester
* Tests for {@link Wizard}
*/
public class WizardTest {

@Test
public void testToString() throws Exception {
final String[] wizardNames = {"Gandalf", "Dumbledore", "Oz", "Merlin"};
for (final String name : wizardNames) {
for (String name : wizardNames) {
assertEquals(name, new Wizard(name).toString());
}
}

}
11 changes: 4 additions & 7 deletions proxy/src/test/java/com/iluwatar/proxy/WizardTowerProxyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@
import static org.junit.Assert.assertTrue;

/**
* Date: 12/28/15 - 9:18 PM
*
* @author Jeroen Meulemeester
* Tests for {@link WizardTowerProxy}
*/
public class WizardTowerProxyTest {

Expand All @@ -58,9 +56,9 @@ public void testEnter() throws Exception {
new Wizard("Merlin")
};

final WizardTowerProxy tower = new WizardTowerProxy();
for (final Wizard wizard : wizards) {
tower.enter(wizard);
final WizardTowerProxy proxy = new WizardTowerProxy(new IvoryTower());
for (Wizard wizard : wizards) {
proxy.enter(wizard);
}

assertTrue(appender.logContains("Gandalf enters the tower."));
Expand All @@ -69,5 +67,4 @@ public void testEnter() throws Exception {
assertTrue(appender.logContains("Merlin is not allowed to enter!"));
assertEquals(4, appender.getLogSize());
}

}

0 comments on commit 82d3d7b

Please sign in to comment.