-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate-method.js
More file actions
97 lines (81 loc) · 2.08 KB
/
template-method.js
File metadata and controls
97 lines (81 loc) · 2.08 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* Template Method
*
* Template Method is a behavioral design pattern that defines the
* skeleton of an algorithm in a method, allowing subclasses to
* redefine certain steps of the algorithm without changing its
* structure.
*/
class DocumentProcessor {
// template method
processDocument() {
this.openDocument()
this.readDocument()
this.analyzeDocument()
this.closeDocument()
}
getMetadata() {
throw new Error('This method should be overridden')
}
openDocument() {
const metadata = this.getMetadata()
console.log(`- opening ${metadata.type} document`)
}
readDocument() {
const metadata = this.getMetadata()
console.log(`- reading contents of ${metadata.type} document`)
}
analyzeDocument() {
const metadata = this.getMetadata()
console.log(`- analyzing ${metadata.type} document`)
}
closeDocument() {
const metadata = this.getMetadata()
console.log(`- closing ${metadata.type} document`)
}
}
class PDFProcessor extends DocumentProcessor {
getMetadata() {
return {
type: 'PDF',
}
}
}
class WordProcessor extends DocumentProcessor {
getMetadata() {
return {
type: 'Word',
}
}
}
class TextProcessor extends DocumentProcessor {
getMetadata() {
return {
type: 'text file',
}
}
}
const pdfProcessor = new PDFProcessor()
const wordProcessor = new WordProcessor()
const textProcessor = new TextProcessor()
console.log('Processing PDF file:')
pdfProcessor.processDocument()
console.log('\nProcessing Word file:')
wordProcessor.processDocument()
console.log('\nProcessing text file:')
textProcessor.processDocument()
// Processing PDF file:
// - opening PDF document
// - reading contents of PDF document
// - analyzing PDF document
// - closing PDF document
// Processing Word file:
// - opening Word document
// - reading contents of Word document
// - analyzing Word document
// - closing Word document
// Processing text file:
// - opening text file document
// - reading contents of text file document
// - analyzing text file document
// - closing text file document