Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tom76kimo
Last active September 21, 2018 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tom76kimo/7bd9d686cf1233bfb5c2a3978a0780a4 to your computer and use it in GitHub Desktop.
Save tom76kimo/7bd9d686cf1233bfb5c2a3978a0780a4 to your computer and use it in GitHub Desktop.
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