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
+31-14Lines changed: 31 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -12,35 +12,52 @@ One of the most useful and versatile design patterns is the classic Proxy.
12
12
13
13
## Proxy concept
14
14
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.
16
28
17
29
## Proxy example
18
30
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.
20
32
21
33

22
34
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`.
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.
38
48
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.
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.
42
52
43
-
Remote proxies are used in distributed object communication. Invoking a local object method causes execution on the remote object.
0 commit comments