We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 01410fc commit e2d1ebfCopy full SHA for e2d1ebf
Design Pattern in JavaScript/AdapterPattern.js
@@ -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