mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-10-16 08:15:22 +00:00
docs: update virtual proxy
This commit is contained in:
1 parent
c9c73490c8
commit
33df05a516
3 files changed
+55
-24
No files matched your search
@@ -6,6 +6,7 @@ tag:
|
|||||||
- Decoupling
|
- Decoupling
|
||||||
- Encapsulation
|
- Encapsulation
|
||||||
- Gang Of Four
|
- Gang Of Four
|
||||||
|
- Lazy initialization
|
||||||
- Proxy
|
- Proxy
|
||||||
- Security
|
- Security
|
||||||
- Wrapping
|
- Wrapping
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ language: en
|
|||||||
tag:
|
tag:
|
||||||
- Gang of Four
|
- Gang of Four
|
||||||
- Instantiation
|
- Instantiation
|
||||||
|
- Lazy initialization
|
||||||
- Resource management
|
- Resource management
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
+53
-24
@@ -3,13 +3,15 @@ title: Virtual Proxy
|
|||||||
category: Structural
|
category: Structural
|
||||||
language: en
|
language: en
|
||||||
tag:
|
tag:
|
||||||
- Gang Of Four
|
- Caching
|
||||||
- Decoupling
|
- Decoupling
|
||||||
|
- Lazy initialization
|
||||||
---
|
---
|
||||||
|
|
||||||
## Also known as
|
## Also known as
|
||||||
|
|
||||||
Surrogate
|
* Lazy Initialization Proxy
|
||||||
|
* Virtual Surrogate
|
||||||
|
|
||||||
## Intent
|
## Intent
|
||||||
|
|
||||||
@@ -19,14 +21,22 @@ Provide a surrogate or placeholder for another object to control its creation an
|
|||||||
|
|
||||||
Real-world example
|
Real-world example
|
||||||
|
|
||||||
> 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.
|
> 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.
|
||||||
|
>
|
||||||
|
> 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.
|
||||||
|
|
||||||
In plain words
|
In plain words
|
||||||
|
|
||||||
> The virtual proxy pattern allows a representative class to stand in for another class to control access to it, particularly for resource-intensive operations.
|
> The virtual proxy pattern allows a representative class to stand in for another class to control access to it, particularly for resource-intensive operations.
|
||||||
|
|
||||||
|
Wikipedia says
|
||||||
|
|
||||||
|
> A proxy that controls access to a resource that is expensive to create.
|
||||||
|
|
||||||
**Programmatic Example**
|
**Programmatic Example**
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
Given our example of a video streaming service, here is how it might be implemented:
|
Given our example of a video streaming service, here is how it might be implemented:
|
||||||
|
|
||||||
First, we define an `ExpensiveObject` interface, which outlines the method for processing video.
|
First, we define an `ExpensiveObject` interface, which outlines the method for processing video.
|
||||||
@@ -40,22 +50,21 @@ public interface ExpensiveObject {
|
|||||||
Here’s the implementation of a `RealVideoObject` that represents an expensive-to-create video object.
|
Here’s the implementation of a `RealVideoObject` that represents an expensive-to-create video object.
|
||||||
|
|
||||||
```java
|
```java
|
||||||
|
@Slf4j
|
||||||
@Getter
|
@Getter
|
||||||
public class RealVideoObject implements ExpensiveObject {
|
public class RealVideoObject implements ExpensiveObject {
|
||||||
private String videoData;
|
|
||||||
|
|
||||||
public RealVideoObject() {
|
public RealVideoObject() {
|
||||||
this.videoData = heavyInitialConfiguration();
|
heavyInitialConfiguration();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String heavyInitialConfiguration() {
|
private void heavyInitialConfiguration() {
|
||||||
System.out.println("Loading video data from the database or heavy computation...");
|
LOGGER.info("Loading initial video configurations...");
|
||||||
return "Some loaded video data";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void process() {
|
public void process() {
|
||||||
System.out.println("Playing video content with data: " + videoData);
|
LOGGER.info("Processing and playing video content...");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -74,7 +83,6 @@ public class VideoObjectProxy implements ExpensiveObject {
|
|||||||
@Override
|
@Override
|
||||||
public void process() {
|
public void process() {
|
||||||
if (realVideoObject == null) {
|
if (realVideoObject == null) {
|
||||||
System.out.println("RealVideoObject is created on demand.");
|
|
||||||
realVideoObject = new RealVideoObject();
|
realVideoObject = new RealVideoObject();
|
||||||
}
|
}
|
||||||
realVideoObject.process();
|
realVideoObject.process();
|
||||||
@@ -84,45 +92,66 @@ public class VideoObjectProxy implements ExpensiveObject {
|
|||||||
|
|
||||||
And here’s how the proxy is used in the system.
|
And here’s how the proxy is used in the system.
|
||||||
|
|
||||||
```
|
```java
|
||||||
ExpensiveObject object = new VirtualProxy();
|
ExpensiveObject object = new VirtualProxy();
|
||||||
object.process(); // The real object is created at this point
|
object.process(); // The real object is created at this point
|
||||||
object.process(); // Uses the already created object
|
object.process(); // Uses the already created object
|
||||||
```
|
```
|
||||||
|
|
||||||
Program output:
|
Program output:
|
||||||
|
|
||||||
```
|
```
|
||||||
Creating RealExpensiveObject only when it is needed.
|
13:11:13.583 [main] INFO com.iluwatar.virtual.proxy.RealVideoObject -- Loading initial video configurations...
|
||||||
Processing and playing video content...
|
13:11:13.585 [main] INFO com.iluwatar.virtual.proxy.RealVideoObject -- Processing and playing video content...
|
||||||
Processing and playing video content...
|
13:11:13.585 [main] INFO com.iluwatar.virtual.proxy.RealVideoObject -- Processing and playing video content...
|
||||||
```
|
```
|
||||||
|
|
||||||
## Class diagram
|
## Class diagram
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
## Applicability
|
## Applicability
|
||||||
|
|
||||||
The virtual proxy pattern is useful when:
|
Use the Virtual Proxy pattern when:
|
||||||
|
|
||||||
* Object creation is resource-intensive, and not all instances are utilized immediately or ever.
|
* Object creation is resource-intensive, and not all instances are utilized immediately or ever.
|
||||||
* The performance of a system can be significantly improved by deferring the creation of objects until they are needed.
|
* The performance of a system can be significantly improved by deferring the creation of objects until they are needed.
|
||||||
* There is a need for control over resource usage in systems dealing with large quantities of high-overhead objects.
|
* There is a need for control over resource usage in systems dealing with large quantities of high-overhead objects.
|
||||||
|
|
||||||
The virtual proxy pattern is typically used to:
|
## Tutorials
|
||||||
|
|
||||||
|
* [The Proxy Pattern in Java - Baeldung](https://www.baeldung.com/java-proxy-pattern)
|
||||||
|
|
||||||
|
## Known Uses
|
||||||
|
|
||||||
* Lazy Initialization: Create objects only when they are actually needed.
|
* Lazy Initialization: Create objects only when they are actually needed.
|
||||||
* Resource Management: Efficiently manage resources by creating heavy objects only on demand.
|
* Resource Management: Efficiently manage resources by creating heavy objects only on demand.
|
||||||
* Access Control: Include logic to check conditions before delegating calls to the actual object.
|
* Access Control: Include logic to check conditions before delegating calls to the actual object.
|
||||||
* Performance Optimization: Incorporate caching of results or states that are expensive to obtain or compute.
|
* Performance Optimization: Incorporate caching of results or states that are expensive to obtain or compute.
|
||||||
* Data Binding and Integration: Delay integration and binding processes until the data is actually needed.
|
* Data Binding and Integration: Delay integration and binding processes until the data is actually needed.
|
||||||
|
* In Java, the `java.awt.Image` class uses virtual proxies to load images on demand.
|
||||||
|
* Hibernate ORM framework uses proxies to implement lazy loading of entities.
|
||||||
|
|
||||||
## Related patterns
|
## Consequences
|
||||||
|
|
||||||
* [Proxy](https://github.com/iluwatar/java-design-patterns/tree/master/proxy)
|
Benefits:
|
||||||
|
|
||||||
|
* Reduces memory usage by deferring object creation.
|
||||||
|
* Can improve performance by delaying heavy operations until needed.
|
||||||
|
|
||||||
|
Trade-offs:
|
||||||
|
|
||||||
|
* Introduces complexity in the codebase.
|
||||||
|
* Can lead to unexpected behaviors if not handled properly, especially in multithreaded environments.
|
||||||
|
|
||||||
|
## Related Patterns
|
||||||
|
|
||||||
|
* [Proxy](https://java-design-patterns.com/patterns/proxy/): Virtual Proxy is a specific type of the Proxy pattern focused on lazy initialization.
|
||||||
|
* [Lazy Loading](https://java-design-patterns.com/patterns/lazy-loading/): Directly related as the core idea of Virtual Proxy is to defer object creation.
|
||||||
|
* [Decorator](https://java-design-patterns.com/patterns/decorator/): Similar in structure, but Decorators add behavior to the objects while proxies control access.
|
||||||
|
|
||||||
## Credits
|
## Credits
|
||||||
|
|
||||||
* [The Proxy Pattern in Java](https://www.baeldung.com/java-proxy-pattern)
|
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
|
||||||
* [What is the virtual proxy design pattern?](https://www.educative.io/answers/what-is-the-virtual-proxy-design-pattern)
|
* [Effective Java](https://amzn.to/4cGk2Jz)
|
||||||
|
* [Patterns of Enterprise Application Architecture](https://amzn.to/3WfKBPR)
|
||||||
Reference in new issue
Block a user