#590 Add explanation for Proxy pattern
This commit is contained in:
parent
f9d1e9aa3f
commit
2e90f82cab
@ -18,7 +18,97 @@ Surrogate
|
|||||||
Provide a surrogate or placeholder for another object to control
|
Provide a surrogate or placeholder for another object to control
|
||||||
access to it.
|
access to it.
|
||||||
|
|
||||||

|
## Explanation
|
||||||
|
Real world example
|
||||||
|
|
||||||
|
> 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.
|
||||||
|
|
||||||
|
In plain words
|
||||||
|
|
||||||
|
> Using the proxy pattern, a class represents the functionality of another class.
|
||||||
|
|
||||||
|
Wikipedia says
|
||||||
|
|
||||||
|
> 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.
|
||||||
|
|
||||||
|
**Programmatic Example**
|
||||||
|
|
||||||
|
Taking our wizard tower example from above. Firstly we have the wizard tower interface and the ivory tower class
|
||||||
|
|
||||||
|
```
|
||||||
|
public interface WizardTower {
|
||||||
|
|
||||||
|
void enter(Wizard wizard);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Then a simple wizard class
|
||||||
|
|
||||||
|
```
|
||||||
|
public class Wizard {
|
||||||
|
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
public Wizard(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Then we have the proxy to add access control to wizard tower
|
||||||
|
|
||||||
|
```
|
||||||
|
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) {
|
||||||
|
tower.enter(wizard);
|
||||||
|
numWizards++;
|
||||||
|
} else {
|
||||||
|
LOGGER.info("{} is not allowed to enter!", wizard);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
And here is tower entering scenario
|
||||||
|
|
||||||
|
```
|
||||||
|
WizardTowerProxy proxy = new WizardTowerProxy(new IvoryTower());
|
||||||
|
proxy.enter(new Wizard("Red wizard")); // Red wizard enters the tower.
|
||||||
|
proxy.enter(new Wizard("White wizard")); // White wizard enters the tower.
|
||||||
|
proxy.enter(new Wizard("Black wizard")); // Black wizard enters the tower.
|
||||||
|
proxy.enter(new Wizard("Green wizard")); // Green wizard is not allowed to enter!
|
||||||
|
proxy.enter(new Wizard("Brown wizard")); // Brown wizard is not allowed to enter!
|
||||||
|
```
|
||||||
|
|
||||||
## Applicability
|
## Applicability
|
||||||
Proxy is applicable whenever there is a need for a more
|
Proxy is applicable whenever there is a need for a more
|
||||||
|
Loading…
x
Reference in New Issue
Block a user