You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/2016-12-13-controlling-access-with-proxy-pattern.md
+16-12Lines changed: 16 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -16,43 +16,47 @@ Basically it wraps an object and acts as a proxy between the calls to the object
16
16
17
17

18
18
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
+
19
21
## Types of proxies
20
22
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.
22
26
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.
24
28
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.
26
30
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.
28
32
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.
30
34
31
35
## Proxy example
32
36
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.
34
38
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`.
`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/>
0 commit comments