-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathabstract-document.ts
51 lines (40 loc) · 1.32 KB
/
abstract-document.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// An abstract document containing a combination of hooks and abstract methods
export interface Document {
[id: string]: string;
}
export abstract class AbstractDocument {
// A template class containing a template method and primitive methods
document: Document = {};
abstract title(document: Document): void;
// Must implement
description?(document: Document): void;
// Optional
author?(document: Document): void;
// Optional
backgroundColour(document: Document): void {
// Optional with a default behavior
document['bg-col'] = 'white';
}
abstract text(document: Document, text: string): void;
// Must implement
footer?(document: Document): void;
// Optional
print(document: Document): void {
// Optional with a default behavior"
console.log('----------------------');
Object.keys(document).forEach((attribute: string) => {
console.log(`${attribute}\t: ${document[attribute]}`);
});
console.log();
}
createDocument(text: string): void {
// The template method
this.title(this.document);
if (this.description) this.description(this.document);
if (this.author) this.author(this.document);
this.backgroundColour(this.document);
this.text(this.document, text);
if (this.footer) this.footer(this.document);
this.print(this.document);
}
}