Skip to content

Commit 2e90f82

Browse files
committed
iluwatar#590 Add explanation for Proxy pattern
1 parent f9d1e9a commit 2e90f82

File tree

1 file changed

+91
-1
lines changed

1 file changed

+91
-1
lines changed

proxy/README.md

+91-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,97 @@ Surrogate
1818
Provide a surrogate or placeholder for another object to control
1919
access to it.
2020

21-
![alt text](./etc/proxy.png "Proxy")
21+
## Explanation
22+
Real world example
23+
24+
> Imagine a tower where the local wizards go to study their spells. The ivory tower can only be accessed through a proxy which ensures that only the first three wizards can enter. Here the proxy represents the functionality of the tower and adds access control to it.
25+
26+
In plain words
27+
28+
> Using the proxy pattern, a class represents the functionality of another class.
29+
30+
Wikipedia says
31+
32+
> A proxy, in its most general form, is a class functioning as an interface to something else. A proxy is a wrapper or agent object that is being called by the client to access the real serving object behind the scenes. Use of the proxy can simply be forwarding to the real object, or can provide additional logic. In the proxy extra functionality can be provided, for example caching when operations on the real object are resource intensive, or checking preconditions before operations on the real object are invoked.
33+
34+
**Programmatic Example**
35+
36+
Taking our wizard tower example from above. Firstly we have the wizard tower interface and the ivory tower class
37+
38+
```
39+
public interface WizardTower {
40+
41+
void enter(Wizard wizard);
42+
}
43+
44+
public class IvoryTower implements WizardTower {
45+
46+
private static final Logger LOGGER = LoggerFactory.getLogger(IvoryTower.class);
47+
48+
public void enter(Wizard wizard) {
49+
LOGGER.info("{} enters the tower.", wizard);
50+
}
51+
52+
}
53+
```
54+
55+
Then a simple wizard class
56+
57+
```
58+
public class Wizard {
59+
60+
private final String name;
61+
62+
public Wizard(String name) {
63+
this.name = name;
64+
}
65+
66+
@Override
67+
public String toString() {
68+
return name;
69+
}
70+
}
71+
```
72+
73+
Then we have the proxy to add access control to wizard tower
74+
75+
```
76+
public class WizardTowerProxy implements WizardTower {
77+
78+
private static final Logger LOGGER = LoggerFactory.getLogger(WizardTowerProxy.class);
79+
80+
private static final int NUM_WIZARDS_ALLOWED = 3;
81+
82+
private int numWizards;
83+
84+
private final WizardTower tower;
85+
86+
public WizardTowerProxy(WizardTower tower) {
87+
this.tower = tower;
88+
}
89+
90+
@Override
91+
public void enter(Wizard wizard) {
92+
if (numWizards < NUM_WIZARDS_ALLOWED) {
93+
tower.enter(wizard);
94+
numWizards++;
95+
} else {
96+
LOGGER.info("{} is not allowed to enter!", wizard);
97+
}
98+
}
99+
}
100+
```
101+
102+
And here is tower entering scenario
103+
104+
```
105+
WizardTowerProxy proxy = new WizardTowerProxy(new IvoryTower());
106+
proxy.enter(new Wizard("Red wizard")); // Red wizard enters the tower.
107+
proxy.enter(new Wizard("White wizard")); // White wizard enters the tower.
108+
proxy.enter(new Wizard("Black wizard")); // Black wizard enters the tower.
109+
proxy.enter(new Wizard("Green wizard")); // Green wizard is not allowed to enter!
110+
proxy.enter(new Wizard("Brown wizard")); // Brown wizard is not allowed to enter!
111+
```
22112

23113
## Applicability
24114
Proxy is applicable whenever there is a need for a more

0 commit comments

Comments
 (0)