Skip to content

Commit f71aa86

Browse files
committed
docs: update proxy
1 parent 94b9022 commit f71aa86

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

proxy/README.md

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ tag:
1414

1515
## Also known as
1616

17-
Surrogate
17+
* Surrogate
1818

1919
## Intent
2020

@@ -24,7 +24,7 @@ Provide a surrogate or placeholder for another object to control access to it.
2424

2525
Real-world example
2626

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.
2828
2929
In plain words
3030

@@ -36,13 +36,17 @@ Wikipedia says
3636
3737
**Programmatic Example**
3838

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.
4042

4143
```java
4244
public interface WizardTower {
4345
void enter(Wizard wizard);
4446
}
47+
```
4548

49+
```java
4650
@Slf4j
4751
public class IvoryTower implements WizardTower {
4852
public void enter(Wizard wizard) {
@@ -76,9 +80,7 @@ Then we have the `WizardTowerProxy` to add access control to `WizardTower`.
7680
public class WizardTowerProxy implements WizardTower {
7781

7882
private static final int NUM_WIZARDS_ALLOWED = 3;
79-
8083
private int numWizards;
81-
8284
private final WizardTower tower;
8385

8486
public WizardTowerProxy(WizardTower tower) {
@@ -100,28 +102,27 @@ public class WizardTowerProxy implements WizardTower {
100102
And here is the tower entering scenario.
101103

102104
```java
103-
var proxy = new WizardTowerProxy(new IvoryTower());
104-
proxy.enter(new Wizard("Red wizard"));
105-
proxy.enter(new Wizard("White wizard"));
106-
proxy.enter(new Wizard("Black wizard"));
107-
proxy.enter(new Wizard("Green wizard"));
108-
proxy.enter(new Wizard("Brown wizard"));
105+
public static void main(String[] args) {
106+
107+
var proxy = new WizardTowerProxy(new IvoryTower());
108+
proxy.enter(new Wizard("Red wizard"));
109+
proxy.enter(new Wizard("White wizard"));
110+
proxy.enter(new Wizard("Black wizard"));
111+
proxy.enter(new Wizard("Green wizard"));
112+
proxy.enter(new Wizard("Brown wizard"));
113+
}
109114
```
110115

111116
Program output:
112117

113118
```
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!
119124
```
120125

121-
## Class diagram
122-
123-
![Proxy](./etc/proxy.urm.png "Proxy pattern class diagram")
124-
125126
## Applicability
126127

127128
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:
165166

166167
## Credits
167168

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)
170171
* [Java Design Patterns: A Hands-On Experience with Real-World Examples](https://amzn.to/3yhh525)

0 commit comments

Comments
 (0)