
//Subject
class Graphic {
constructor(fileName) {
this._fileName = fileName;
}
getFileName() {
return this._fileName;
}
}
//RealSubject
class CImage extends Graphic {
constructor(fileName) {
super(fileName);
}
//Request()
draw() {
console.log("draw " + this._fileName);
}
}
//Proxy
class ImageProxy extends Graphic {
constructor(fileName) {
super(fileName);
}
getImage() {
if (this._image == undefined) {
this._image = new CImage(this._fileName);
}
return this._image;
}
draw() {
this.getImage().draw();
}
}
//Client
let proxy = new ImageProxy("1.png");
//operation without creating a RealSubject
let fileName = proxy.getFileName();
//forwarded to the RealSubject
proxy.draw();
console.log("fileName is", fileName);