Last active
September 21, 2018 03:28
-
-
Save tom76kimo/7bd9d686cf1233bfb5c2a3978a0780a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Extent { | |
width: number; | |
height: number; | |
} | |
abstract class Graphic { | |
protected fileName: string; | |
abstract getExtent(): Extent; | |
abstract draw(): void; | |
} | |
class Img extends Graphic { | |
private extent: Extent; | |
constructor(fileName, width, height) { | |
super(); | |
this.fileName = fileName; | |
this.extent = { | |
width, | |
height | |
} | |
console.log('- Load image data'); | |
} | |
draw(): void { | |
console.log('- Draw image'); | |
} | |
getExtent(): Extent { | |
console.log('- Get extent from real image meta'); | |
return this.extent; | |
} | |
} | |
class ImgProxy extends Graphic { | |
private imgInstance: Img; | |
private extent: Extent; | |
constructor(fileName, width, height) { | |
super(); | |
this.fileName = fileName; | |
this.extent = { | |
width, | |
height | |
} | |
console.log(`- Doesn\'t have image ${this.fileName} currently`); | |
} | |
draw() { | |
if (!this.imgInstance) { | |
console.log('- Initiate img instance'); | |
this.imgInstance = new Img(this.fileName, this.extent.width, this.extent.height); | |
} | |
this.imgInstance.draw(); | |
} | |
getExtent(): Extent { | |
if (this.imgInstance) { | |
return this.imgInstance.getExtent(); | |
} | |
console.log('- Get extent from proxy cache'); | |
return this.extent; | |
} | |
} | |
const img = new ImgProxy('a.jpg', 100, 60); | |
img.getExtent(); | |
img.draw(); | |
img.getExtent(); | |
/** | |
- Doesn't have image a.jpg currently | |
- Get extent from proxy cache | |
- Initiate img instance | |
- Load image data | |
- Draw image | |
- Get extent from real image meta | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment