Skip to content

Commit e807bf8

Browse files
committed
aadd structural adapter js
Signed-off-by: Nimit <nimitagg95@gmail.com>
1 parent fdace44 commit e807bf8

File tree

5 files changed

+123
-11
lines changed

5 files changed

+123
-11
lines changed

Creational/factoryMethod.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,5 @@ class ClientHTTP extends ClientTcp {
6868
}
6969
}
7070

71-
let c = new ClientHTTP;
71+
let c = new ClientHTTP();
7272
c.main();

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,13 @@ Creational patterns are ones that create objects for you, rather than having you
1313
- **Prototype:** specify the kinds of objects to create using a prototypical instance, and create new objects from the 'skeleton' of an existing object, thus boosting performance and keeping memory footprints to a minimum.
1414
- **Singleton:** ensure a class has only one instance, and provide a global point of access to it.
1515

16+
### Structural Patterns
17+
These concern class and object composition. They use inheritance to compose interfaces and define ways to compose objects to obtain new functionality.
18+
19+
- **Adapter:** allows classes with incompatible interfaces to work together by wrapping its own interface around that of an already existing class.
20+
- **Bridge:** decouples an abstraction from its implementation so that the two can vary independently.
21+
- **Composite:** composes zero-or-more similar objects so that they can be manipulated as one object.
22+
- **Decorator:** dynamically adds/overrides behaviour in an existing method of an object.
23+
- **Facade:** provides a simplified interface to a large body of code.
24+
- **Flyweight:** reduces the cost of creating and manipulating a large number of similar objects.
25+
- **Proxy:** provides a placeholder for another object to control access, reduce cost, and reduce complexity.

Structural/adapter.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// fs is the adaptee class
2+
const fs = require('fs');
3+
4+
// delegator class
5+
class fsDelegator {
6+
read() {
7+
return new Error("Please implement read method!");
8+
}
9+
}
10+
/**
11+
* Adapter class implements the delegate
12+
* Converts fs callbacks to fs promisified
13+
*/
14+
class fsAdapter extends fsDelegator {
15+
read(path) {
16+
return new Promise((resolve, reject) => {
17+
// eslint-disable-next-line node/prefer-promises/fs
18+
fs.readFile(__dirname + "/" + path, (err, buf) => {
19+
if(err) {
20+
return reject(err);
21+
}
22+
resolve(buf.toString());
23+
});
24+
});
25+
}
26+
}
27+
28+
class Client {
29+
constructor() {
30+
this.setDelegate();
31+
}
32+
async reader() {
33+
return this._fsDelegate.read("adapter.js");
34+
}
35+
setDelegate() {
36+
this._fsDelegate = new fsAdapter();
37+
}
38+
}
39+
40+
const client = new Client();
41+
client.reader().then(res => console.log("Reading " + res));

buildDocs.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,24 @@ const nPath = require('path');
44
/* Path Constants */
55
const DOCS_FILE = './docs.md';
66

7-
const sections = [{
8-
name: 'Creational', topics: [
9-
{ name: 'abstractFactory', path: './Creational' },
10-
{ name: 'builder', path: './Creational' },
11-
{ name: 'factoryMethod', path: './Creational' },
12-
{ name: 'prototype', path: './Creational' },
13-
{ name: 'singleton', path: './Creational' }
14-
]
15-
}];
7+
const sections = [
8+
{
9+
name: 'Creational',
10+
topics: [
11+
{ name: 'abstractFactory', path: './Creational' },
12+
{ name: 'builder', path: './Creational' },
13+
{ name: 'factoryMethod', path: './Creational' },
14+
{ name: 'prototype', path: './Creational' },
15+
{ name: 'singleton', path: './Creational' }
16+
]
17+
},
18+
{
19+
name: 'Structural',
20+
topics: [
21+
{ name: 'adapter', path: './Structural' }
22+
]
23+
}
24+
];
1625

1726
/* Utility functions */
1827
const stripPunctuation = (str) =>

docs.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
* [Prototype](#prototype)
88
* [Singleton](#singleton)
99

10+
**[ Structural](#structural)**
11+
* [Adapter](#adapter)
12+
1013

1114

1215
## Creational
@@ -232,7 +235,7 @@ class ClientHTTP extends ClientTcp {
232235
}
233236
}
234237

235-
let c = new ClientHTTP;
238+
let c = new ClientHTTP();
236239
c.main();
237240
```
238241

@@ -292,4 +295,53 @@ Server.getInstance().status();
292295
```
293296

294297

298+
## Structural
299+
### Adapter
300+
##### adapter.js
301+
```Javascript
302+
// fs is the adaptee class
303+
const fs = require('fs');
304+
305+
// delegator class
306+
class fsDelegator {
307+
read() {
308+
return new Error("Please implement read method!");
309+
}
310+
}
311+
/**
312+
* Adapter class implements the delegate
313+
* Converts fs callbacks to fs promisified
314+
*/
315+
class fsAdapter extends fsDelegator {
316+
read(path) {
317+
return new Promise((resolve, reject) => {
318+
// eslint-disable-next-line node/prefer-promises/fs
319+
fs.readFile(__dirname + "/" + path, (err, buf) => {
320+
if(err) {
321+
return reject(err);
322+
}
323+
resolve(buf.toString());
324+
});
325+
});
326+
}
327+
}
328+
329+
class Client {
330+
constructor() {
331+
this.setDelegate();
332+
}
333+
async reader() {
334+
return this._fsDelegate.read("adapter.js");
335+
}
336+
setDelegate() {
337+
this._fsDelegate = new fsAdapter();
338+
}
339+
}
340+
341+
const client = new Client();
342+
client.reader().then(res => console.log("Reading " + res));
343+
344+
```
345+
346+
295347

0 commit comments

Comments
 (0)