Skip to content

Commit 169b2b0

Browse files
committed
Revised text.
1 parent 2ff0a00 commit 169b2b0

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,43 +16,47 @@ Basically it wraps an object and acts as a proxy between the calls to the object
1616

1717
![Proxy class diagram]({{ site.baseurl }}/assets/proxy.png)
1818

19+
In the class diagram we see that the client depends only on the interface so it can as well use the proxy instead of the real subject. When the proxy object is called it does its thing and eventually forwards the call to the real subject.
20+
1921
## Types of proxies
2022

21-
Protection proxy limits access to the object. Protection proxies can also be used to implement more fine grained access control.
23+
There are multiple use cases where the proxy pattern is useful.
24+
25+
- Protection proxy limits access to the real subject. Based on some condition the proxy filters the calls and only some of them are let through to the real subject. The code example below is an example of protection proxy.
2226

23-
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.
27+
- Another type is the virtual proxy. Virtual proxies are used when an object is expensive to instantiate. In the implementation the proxy manages the lifetime of the real subject. It decides when the instance creation is needed and when it can be reused. Virtual proxies are used to optimize performance.
2428

25-
Cache proxy can be used to cache expensive calls to the object improving performance.
29+
- Caching proxies are used to cache expensive calls to the real subject. There are multiple caching strategies that the proxy can use. Some examples are read-through, write-through, cache-aside and time-based. The caching proxies are used for enhancing performance.
2630

27-
Remote proxies are used in distributed object communication. Invoking a local object method causes execution on the remote object.
31+
- Remote proxies are used in distributed object communication. Invoking a local object method on the remote proxy causes execution on the remote object.
2832

29-
Smart proxies are used to implement reference counting and log calls to the object.
33+
- Smart proxies are used to implement reference counting and log calls to the object.
3034

3135
## Proxy example
3236

33-
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.
37+
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 local archwizard and it has become very popular among the wizards. However, to keep his tower tidy and not too crowded the archwizard 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.
3438

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`.
39+
In the example `WizardTower` is the interface for all the towers and `IvoryTower` implements it. The plain `IvoryTower` allows everyone to enter but the archwizard 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`.
3640

3741
<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>
3842

39-
`WizardTower` is the interface for all towers. It contains one simple method for entering the tower.
43+
`WizardTower` is the interface for all towers. It contains one simple method for entering the tower.<br/><br/>
4044

4145
<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>
4246

43-
`IvoryTower` is a simple implementation of `WizardTower`. It prints the entering wizard's name.
47+
`IvoryTower` is a simple implementation of `WizardTower`. It prints the entering wizard's name.<br/><br/>
4448

4549
<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>
4650

47-
`Wizard` is a simple immutable object with name.
51+
`Wizard` is a simple immutable object with name.<br/><br/>
4852

4953
<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>
5054

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.
55+
`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.<br/><br/>
5256

5357
<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>
5458

55-
Running the application produces the following output.
59+
Running the application produces the following output.<br/><br/>
5660

5761
```
5862
22:01:56.571 [main] INFO com.iluwatar.proxy.IvoryTower - Red wizard enters the tower.

0 commit comments

Comments
 (0)