Skip to content

Commit 33df05a

Browse files
committed
docs: update virtual proxy
1 parent c9c7349 commit 33df05a

File tree

3 files changed

+55
-24
lines changed

3 files changed

+55
-24
lines changed

proxy/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ tag:
66
- Decoupling
77
- Encapsulation
88
- Gang Of Four
9+
- Lazy initialization
910
- Proxy
1011
- Security
1112
- Wrapping

singleton/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ language: en
55
tag:
66
- Gang of Four
77
- Instantiation
8+
- Lazy initialization
89
- Resource management
910
---
1011

virtual-proxy/README.md

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ title: Virtual Proxy
33
category: Structural
44
language: en
55
tag:
6-
- Gang Of Four
7-
- Decoupling
6+
- Caching
7+
- Decoupling
8+
- Lazy initialization
89
---
910

1011
## Also known as
1112

12-
Surrogate
13+
* Lazy Initialization Proxy
14+
* Virtual Surrogate
1315

1416
## Intent
1517

@@ -19,14 +21,22 @@ Provide a surrogate or placeholder for another object to control its creation an
1921

2022
Real-world example
2123

22-
> Consider an online video streaming platform where video objects are resource-intensive due to their large data size and required processing power. To efficiently manage resources, the system uses a virtual proxy to handle video objects. The virtual proxy defers the creation of actual video objects until they are explicitly required for playback, thus saving system resources and improving response times for users.
24+
> Imagine a high-end art gallery that showcases expensive and delicate pieces of art. To protect the actual artwork and reduce the risk of damage or theft, the gallery initially displays high-quality photographs of the artworks. When a serious buyer expresses genuine interest, the gallery then brings out the original artwork from a secure storage area for viewing.
25+
>
26+
> In this analogy, the high-quality photograph serves as the virtual proxy for the actual artwork. The real artwork is only fetched and displayed when truly necessary, thus saving resources and reducing risk, similar to how the Virtual Proxy pattern defers object creation until it is needed.
2327
2428
In plain words
2529

2630
> The virtual proxy pattern allows a representative class to stand in for another class to control access to it, particularly for resource-intensive operations.
2731
32+
Wikipedia says
33+
34+
> A proxy that controls access to a resource that is expensive to create.
35+
2836
**Programmatic Example**
2937

38+
Consider an online video streaming platform where video objects are resource-intensive due to their large data size and required processing power. To efficiently manage resources, the system uses a virtual proxy to handle video objects. The virtual proxy defers the creation of actual video objects until they are explicitly required for playback, thus saving system resources and improving response times for users.
39+
3040
Given our example of a video streaming service, here is how it might be implemented:
3141

3242
First, we define an `ExpensiveObject` interface, which outlines the method for processing video.
@@ -40,22 +50,21 @@ public interface ExpensiveObject {
4050
Here’s the implementation of a `RealVideoObject` that represents an expensive-to-create video object.
4151

4252
```java
53+
@Slf4j
4354
@Getter
4455
public class RealVideoObject implements ExpensiveObject {
45-
private String videoData;
4656

4757
public RealVideoObject() {
48-
this.videoData = heavyInitialConfiguration();
58+
heavyInitialConfiguration();
4959
}
5060

51-
private String heavyInitialConfiguration() {
52-
System.out.println("Loading video data from the database or heavy computation...");
53-
return "Some loaded video data";
61+
private void heavyInitialConfiguration() {
62+
LOGGER.info("Loading initial video configurations...");
5463
}
5564

5665
@Override
5766
public void process() {
58-
System.out.println("Playing video content with data: " + videoData);
67+
LOGGER.info("Processing and playing video content...");
5968
}
6069
}
6170
```
@@ -74,7 +83,6 @@ public class VideoObjectProxy implements ExpensiveObject {
7483
@Override
7584
public void process() {
7685
if (realVideoObject == null) {
77-
System.out.println("RealVideoObject is created on demand.");
7886
realVideoObject = new RealVideoObject();
7987
}
8088
realVideoObject.process();
@@ -84,45 +92,66 @@ public class VideoObjectProxy implements ExpensiveObject {
8492

8593
And here’s how the proxy is used in the system.
8694

87-
```
95+
```java
8896
ExpensiveObject object = new VirtualProxy();
89-
object.process(); // The real object is created at this point
90-
object.process(); // Uses the already created object
97+
object.process(); // The real object is created at this point
98+
object.process(); // Uses the already created object
9199
```
92100

93101
Program output:
94102

95103
```
96-
Creating RealExpensiveObject only when it is needed.
97-
Processing and playing video content...
98-
Processing and playing video content...
104+
13:11:13.583 [main] INFO com.iluwatar.virtual.proxy.RealVideoObject -- Loading initial video configurations...
105+
13:11:13.585 [main] INFO com.iluwatar.virtual.proxy.RealVideoObject -- Processing and playing video content...
106+
13:11:13.585 [main] INFO com.iluwatar.virtual.proxy.RealVideoObject -- Processing and playing video content...
99107
```
100108

101109
## Class diagram
102110

103-
![alt text](./etc/virtual.proxy.urm.png "Virtual Proxy pattern class diagram")
111+
![Virtual Proxy](./etc/virtual.proxy.urm.png "Virtual Proxy pattern class diagram")
104112

105113
## Applicability
106114

107-
The virtual proxy pattern is useful when:
115+
Use the Virtual Proxy pattern when:
108116

109117
* Object creation is resource-intensive, and not all instances are utilized immediately or ever.
110118
* The performance of a system can be significantly improved by deferring the creation of objects until they are needed.
111119
* There is a need for control over resource usage in systems dealing with large quantities of high-overhead objects.
112120

113-
The virtual proxy pattern is typically used to:
121+
## Tutorials
122+
123+
* [The Proxy Pattern in Java - Baeldung](https://www.baeldung.com/java-proxy-pattern)
124+
125+
## Known Uses
114126

115127
* Lazy Initialization: Create objects only when they are actually needed.
116128
* Resource Management: Efficiently manage resources by creating heavy objects only on demand.
117129
* Access Control: Include logic to check conditions before delegating calls to the actual object.
118130
* Performance Optimization: Incorporate caching of results or states that are expensive to obtain or compute.
119131
* Data Binding and Integration: Delay integration and binding processes until the data is actually needed.
132+
* In Java, the `java.awt.Image` class uses virtual proxies to load images on demand.
133+
* Hibernate ORM framework uses proxies to implement lazy loading of entities.
134+
135+
## Consequences
136+
137+
Benefits:
138+
139+
* Reduces memory usage by deferring object creation.
140+
* Can improve performance by delaying heavy operations until needed.
141+
142+
Trade-offs:
143+
144+
* Introduces complexity in the codebase.
145+
* Can lead to unexpected behaviors if not handled properly, especially in multithreaded environments.
120146

121-
## Related patterns
147+
## Related Patterns
122148

123-
* [Proxy](https://github.com/iluwatar/java-design-patterns/tree/master/proxy)
149+
* [Proxy](https://java-design-patterns.com/patterns/proxy/): Virtual Proxy is a specific type of the Proxy pattern focused on lazy initialization.
150+
* [Lazy Loading](https://java-design-patterns.com/patterns/lazy-loading/): Directly related as the core idea of Virtual Proxy is to defer object creation.
151+
* [Decorator](https://java-design-patterns.com/patterns/decorator/): Similar in structure, but Decorators add behavior to the objects while proxies control access.
124152

125153
## Credits
126154

127-
* [The Proxy Pattern in Java](https://www.baeldung.com/java-proxy-pattern)
128-
* [What is the virtual proxy design pattern?](https://www.educative.io/answers/what-is-the-virtual-proxy-design-pattern)
155+
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
156+
* [Effective Java](https://amzn.to/4cGk2Jz)
157+
* [Patterns of Enterprise Application Architecture](https://amzn.to/3WfKBPR)

0 commit comments

Comments
 (0)