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: virtual-proxy/README.md
+53-24Lines changed: 53 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -3,13 +3,15 @@ title: Virtual Proxy
3
3
category: Structural
4
4
language: en
5
5
tag:
6
-
- Gang Of Four
7
-
- Decoupling
6
+
- Caching
7
+
- Decoupling
8
+
- Lazy initialization
8
9
---
9
10
10
11
## Also known as
11
12
12
-
Surrogate
13
+
* Lazy Initialization Proxy
14
+
* Virtual Surrogate
13
15
14
16
## Intent
15
17
@@ -19,14 +21,22 @@ Provide a surrogate or placeholder for another object to control its creation an
19
21
20
22
Real-world example
21
23
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.
23
27
24
28
In plain words
25
29
26
30
> The virtual proxy pattern allows a representative class to stand in for another class to control access to it, particularly for resource-intensive operations.
27
31
32
+
Wikipedia says
33
+
34
+
> A proxy that controls access to a resource that is expensive to create.
35
+
28
36
**Programmatic Example**
29
37
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
+
30
40
Given our example of a video streaming service, here is how it might be implemented:
31
41
32
42
First, we define an `ExpensiveObject` interface, which outlines the method for processing video.
@@ -40,22 +50,21 @@ public interface ExpensiveObject {
40
50
Here’s the implementation of a `RealVideoObject` that represents an expensive-to-create video object.
*[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.
124
152
125
153
## Credits
126
154
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