
//Subject
class Graphic {
protected _fileName = ""
constructor(fileName: string) {
this._fileName = fileName
}
getFileName() {
return this._fileName
}
}
//RealSubject
class Picture extends Graphic {
constructor(fileName: string) {
super(fileName)
}
//Request()
draw() {
console.log("draw " + this._fileName)
}
}
//Proxy
class ImageProxy extends Graphic {
private _image: Picture = undefined
constructor(fileName: string) {
super(fileName)
}
getImage() {
if (this._image == undefined) {
this._image = new Picture(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)