-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapter.js
46 lines (39 loc) · 787 Bytes
/
adapter.js
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
/**
* Adapter
*
* Adapter is a structural design pattern that allows objects with
* incompatible interfaces to work together by wrapping the object
* with an interface that the client expects.
*/
class OldPrinter {
printOld() {
return 'Old Printer'
}
}
class NewPrinter {
printNew() {
return 'New Printer'
}
}
class PrinterAdapter {
constructor(oldPrinter) {
this.oldPrinter = oldPrinter
}
printNew() {
return this.oldPrinter.printOld()
}
}
const ENV = 'New System'
const newPrinter = new NewPrinter()
console.log(
`${ENV}:`,
newPrinter.printNew()
)
// New System: New Printer
const oldPrinter = new OldPrinter()
const adapter = new PrinterAdapter(oldPrinter)
console.log(
`${ENV}:`,
adapter.printNew()
)
// New System: Old Printer