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: proxy/README.md
+23-22Lines changed: 23 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ tag:
14
14
15
15
## Also known as
16
16
17
-
Surrogate
17
+
*Surrogate
18
18
19
19
## Intent
20
20
@@ -24,7 +24,7 @@ Provide a surrogate or placeholder for another object to control access to it.
24
24
25
25
Real-world example
26
26
27
-
> 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.
27
+
> In a real-world scenario, consider a security guard at a gated community. The security guard acts as a proxy for the residents. When a visitor arrives, the guard checks the visitor's credentials and permissions before allowing them access to the community. If the visitor is authorized, the guard grants entry; if not, entry is denied. This ensures that only authorized individuals can access the community, much like a Proxy design pattern controls access to a specific object.
28
28
29
29
In plain words
30
30
@@ -36,13 +36,17 @@ Wikipedia says
36
36
37
37
**Programmatic Example**
38
38
39
-
Taking our wizard tower example from above. Firstly we have the `WizardTower` interface and the `IvoryTower` class.
39
+
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.
40
+
41
+
First, we have the `WizardTower` interface and the `IvoryTower` class.
40
42
41
43
```java
42
44
publicinterfaceWizardTower {
43
45
voidenter(Wizardwizard);
44
46
}
47
+
```
45
48
49
+
```java
46
50
@Slf4j
47
51
publicclassIvoryTowerimplementsWizardTower {
48
52
publicvoidenter(Wizardwizard) {
@@ -76,9 +80,7 @@ Then we have the `WizardTowerProxy` to add access control to `WizardTower`.
@@ -100,28 +102,27 @@ public class WizardTowerProxy implements WizardTower {
100
102
And here is the tower entering scenario.
101
103
102
104
```java
103
-
var proxy =newWizardTowerProxy(newIvoryTower());
104
-
proxy.enter(newWizard("Red wizard"));
105
-
proxy.enter(newWizard("White wizard"));
106
-
proxy.enter(newWizard("Black wizard"));
107
-
proxy.enter(newWizard("Green wizard"));
108
-
proxy.enter(newWizard("Brown wizard"));
105
+
publicstaticvoid main(String[] args) {
106
+
107
+
var proxy =newWizardTowerProxy(newIvoryTower());
108
+
proxy.enter(newWizard("Red wizard"));
109
+
proxy.enter(newWizard("White wizard"));
110
+
proxy.enter(newWizard("Black wizard"));
111
+
proxy.enter(newWizard("Green wizard"));
112
+
proxy.enter(newWizard("Brown wizard"));
113
+
}
109
114
```
110
115
111
116
Program output:
112
117
113
118
```
114
-
Red wizard enters the tower.
115
-
White wizard enters the tower.
116
-
Black wizard enters the tower.
117
-
Green wizard is not allowed to enter!
118
-
Brown wizard is not allowed to enter!
119
+
08:42:06.183 [main] INFO com.iluwatar.proxy.IvoryTower -- Red wizard enters the tower.
120
+
08:42:06.186 [main] INFO com.iluwatar.proxy.IvoryTower -- White wizard enters the tower.
121
+
08:42:06.186 [main] INFO com.iluwatar.proxy.IvoryTower -- Black wizard enters the tower.
122
+
08:42:06.186 [main] INFO com.iluwatar.proxy.WizardTowerProxy -- Green wizard is not allowed to enter!
123
+
08:42:06.186 [main] INFO com.iluwatar.proxy.WizardTowerProxy -- Brown wizard is not allowed to enter!
119
124
```
120
125
121
-
## Class diagram
122
-
123
-

124
-
125
126
## Applicability
126
127
127
128
Proxy is applicable whenever there is a need for a more versatile or sophisticated reference to an object than a simple pointer. Here are several common situations in which the Proxy pattern is applicable. Typically, the proxy pattern is used to
@@ -165,6 +166,6 @@ Trade-offs:
165
166
166
167
## Credits
167
168
168
-
*[Design Patterns: Elements of Reusable Object-Oriented Software](https://www.amazon.com/gp/product/0201633612/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0201633612&linkCode=as2&tag=javadesignpat-20&linkId=675d49790ce11db99d90bde47f1aeb59)
169
-
*[Head First Design Patterns: A Brain-Friendly Guide](https://www.amazon.com/gp/product/0596007124/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596007124&linkCode=as2&tag=javadesignpat-20&linkId=6b8b6eea86021af6c8e3cd3fc382cb5b)
169
+
*[Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
170
+
*[Head First Design Patterns: Building Extensible and Maintainable Object-Oriented Software](https://amzn.to/49NGldq)
170
171
*[Java Design Patterns: A Hands-On Experience with Real-World Examples](https://amzn.to/3yhh525)
0 commit comments