Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.