//image class created just when it needed
// Subject: Image
class Image {
constructor(url) {
this.url = url;
}
display() {
console.log(`Displaying image from ${this.url}`);
}
}
// Proxy: Virtual Image Proxy
class ImageProxy {
constructor(url) {
this.url = url;
this.realImage = null;
}
display() {
if (this.realImage === null) {
this.realImage = new Image(this.url);
}
this.realImage.display();
}
}
// Usage
const imageProxy = new ImageProxy('image.jpg');
// Image is not loaded yet
imageProxy.display();
// Image is loaded and displayed
imageProxy.display();
No comments:
Post a Comment