Skip to content

Commit 68dac63

Browse files
committed
Work on Proxy blog post
1 parent 6f022b2 commit 68dac63

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

_posts/2016-12-13-controlling-access-with-proxy-pattern.md

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,52 @@ One of the most useful and versatile design patterns is the classic Proxy.
1212

1313
## Proxy concept
1414

15-
Basically it wraps an object and acts as a proxy between calls to the object. At first thought it might not sound so useful but this simple concept is a great solution to many problems.
15+
Basically it wraps an object and acts as a proxy between the calls to the object. At first thought it might not sound so useful but this simple concept is a great solution to many problems.
16+
17+
## Types of proxies
18+
19+
Protection proxy limits access to the object. Protection proxies can also be used to implement more fine grained access control.
20+
21+
Another type is virtual proxy. Virtual proxies are used when an object is expensive to instantiate. We let the proxy manage the lifetime of the object, namely when the instance creation is needed and when it can be reused.
22+
23+
Cache proxy can be used to cache expensive calls to the object improving performance.
24+
25+
Remote proxies are used in distributed object communication. Invoking a local object method causes execution on the remote object.
26+
27+
Smart proxies are used to implement reference counting and log calls to the object.
1628

1729
## Proxy example
1830

19-
To elaborate let's think of a simple example. In a fantasy land far away inhabited by creatures from hobbits to dragons the mystic profession of wizard also exists. Wizards are known from their great magic but this power comes with a price. To upkeep and gain spell power the wizards must spend their time studying spellbooks and practising their spells. The best place in the neighborhood to study is the famous Ivory Tower built by the archmage Ivorious and it has become very popular among the wizards. However, to keep his tower tidy and not too crowded Ivorious decided to limit the amount of wizards that are allowed to enter simultaneously. He cast a protection spell on the tower that allows only the three first wizards to enter.
31+
To elaborate how Proxy design pattern works in practise let's think of a simple example. In a fantasy land far away inhabited by creatures from hobbits to dragons the mystic profession of wizard also exists. Wizards are known from their great magic but this power comes with a price. To upkeep and gain spell power the wizards must spend their time studying spellbooks and practising their spells. The best place in the neighborhood to study is the famous Ivory Tower built by the archmage Ivorious and it has become very popular among the wizards. However, to keep his tower tidy and not too crowded Ivorious decided to limit the amount of wizards that are allowed to enter the tower simultaneously. He cast a protection spell on the tower that allows only the three first wizards to enter.
2032

2133
![Proxy class diagram]({{ site.baseurl }}/assets/proxy-class-diagram.png)
2234

23-
The class diagram shows the structure used to implement the Proxy pattern. WizardTower is the interface for all the towers and IvoryTower implements it. The plain IvoryTower allows everyone to enter but Ivorius has specifically cast a protection spell on it to limit the number of simultaneous visitors. The protection spell WizardTowerProxy also implements WizardTower but then it wraps IvoryTower. Now everyone wanting to access IvoryTower needs to go through WizardTowerProxy.
35+
The class diagram shows the structure used to implement the Proxy pattern. `WizardTower` is the interface for all the towers and `IvoryTower` implements it. The plain `IvoryTower` allows everyone to enter but Ivorius has specifically cast a protection spell on it to limit the number of simultaneous visitors. The protection spell enhanced `IvoryTower` is called `WizardTowerProxy`. It implements `WizardTower` and wraps `IvoryTower`. Now everyone wanting to access `IvoryTower` needs to go through the `WizardTowerProxy`.
2436

2537
<script src="http://gist-it.appspot.com/http://github.com/iluwatar/java-design-patterns/raw/master/proxy/src/main/java/com/iluwatar/proxy/WizardTower.java?slice=27:"></script>
2638

27-
<script src="http://gist-it.appspot.com/http://github.com/iluwatar/java-design-patterns/raw/master/proxy/src/main/java/com/iluwatar/proxy/IvoryTower.java?slice=32:"></script>
39+
`WizardTower` is the interface for all towers. It contains one simple method for entering the tower.
2840

29-
<script src="http://gist-it.appspot.com/http://github.com/iluwatar/java-design-patterns/raw/master/proxy/src/main/java/com/iluwatar/proxy/Wizard.java?slice=29:"></script>
30-
31-
<script src="http://gist-it.appspot.com/http://github.com/iluwatar/java-design-patterns/raw/master/proxy/src/main/java/com/iluwatar/proxy/WizardTowerProxy.java?slice=32:"></script>
41+
<script src="http://gist-it.appspot.com/http://github.com/iluwatar/java-design-patterns/raw/master/proxy/src/main/java/com/iluwatar/proxy/IvoryTower.java?slice=32:"></script>
3242

33-
<script src="http://gist-it.appspot.com/http://github.com/iluwatar/java-design-patterns/raw/master/proxy/src/main/java/com/iluwatar/proxy/App.java?slice=40:"></script>
43+
`IvoryTower` is a simple implementation of `WizardTower`. It prints the entering wizard's name.
3444

35-
## Types of proxies
45+
<script src="http://gist-it.appspot.com/http://github.com/iluwatar/java-design-patterns/raw/master/proxy/src/main/java/com/iluwatar/proxy/Wizard.java?slice=29:"></script>
3646

37-
What we have seen in the previous tutorial is an example of protection proxy that limits access to the object. Protection proxies can also be used to implement more fine grained access control.
47+
`Wizard` is a simple immutable object with name.
3848

39-
Another type is virtual proxy. Virtual proxies are used when an object is expensive to instantiate. We let the proxy manage the lifetime of the object, namely when the instance creation is needed and when it can be reused.
49+
<script src="http://gist-it.appspot.com/http://github.com/iluwatar/java-design-patterns/raw/master/proxy/src/main/java/com/iluwatar/proxy/WizardTowerProxy.java?slice=32:"></script>
4050

41-
Cache proxy can be used to cache expensive calls to the object improving performance.
51+
`WizardTowerProxy` is the proxy class that wraps any kind of `WizardTower` while implementing its interface. It keeps count how many wizards have entered the wrapped `WizardTower` and when the number reaches three it refuses to let any more wizards in.
4252

43-
Remote proxies are used in distributed object communication. Invoking a local object method causes execution on the remote object.
53+
<script src="http://gist-it.appspot.com/http://github.com/iluwatar/java-design-patterns/raw/master/proxy/src/main/java/com/iluwatar/proxy/App.java?slice=40:"></script>
4454

45-
Smart proxies are used to implement reference counting and log calls to the object.
55+
Running the application produces the following output.
4656

57+
```
58+
22:01:56.571 [main] INFO com.iluwatar.proxy.IvoryTower - Red wizard enters the tower.
59+
22:01:56.576 [main] INFO com.iluwatar.proxy.IvoryTower - White wizard enters the tower.
60+
22:01:56.576 [main] INFO com.iluwatar.proxy.IvoryTower - Black wizard enters the tower.
61+
22:01:56.576 [main] INFO com.iluwatar.proxy.WizardTowerProxy - Green wizard is not allowed to enter!
62+
22:01:56.576 [main] INFO com.iluwatar.proxy.WizardTowerProxy - Brown wizard is not allowed to enter!
63+
```

0 commit comments

Comments
 (0)