Skip to content

Commit e2d1ebf

Browse files
committed
Ergänzung Code-Beispiel für das Adapter-Pattern in JavaScript
1 parent 01410fc commit e2d1ebf

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Adaptee class
2+
class ThirdPartyLibrary {
3+
printText(text) {
4+
console.log(text);
5+
}
6+
}
7+
8+
// Target interface
9+
class TextPrinter {
10+
displayText(text) {}
11+
}
12+
13+
// Adapter class
14+
class TextPrinterAdapter extends TextPrinter {
15+
constructor(thirdPartyLibrary) {
16+
super();
17+
this.thirdPartyLibrary = thirdPartyLibrary;
18+
}
19+
20+
displayText(text) {
21+
this.thirdPartyLibrary.printText(text);
22+
}
23+
}
24+
25+
// Client code
26+
const text = "Hello, world!";
27+
const thirdPartyLibrary = new ThirdPartyLibrary();
28+
const textPrinter = new TextPrinterAdapter(thirdPartyLibrary);
29+
textPrinter.displayText(text);

0 commit comments

Comments
 (0)