-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTemplateMethodPattern.js
62 lines (50 loc) · 1.24 KB
/
TemplateMethodPattern.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class Datastore {
constructor() {
if (this.constructor.name === 'Datastore') {
throw new Error('datastore is abstract and need to be implemented');
}
}
connect() {
throw new Error('method not implemented');
}
query(query) {
throw new Error('method not implemented');
}
disconnect() {
throw new Error('method not implemented');
}
process(query){
this.connect();
const result = this.query(query);
this.disconnect();
return result;
}
}
class MySQLDatastore extends Datastore {
connect() {
console.log('mysql connect step');
}
query(query) {
console.log(`mysql execute query: ${query}`);
return ['some data'];
}
disconnect() {
console.log('mysql disconnect step');
}
}
class PostgreSQLDatastore extends Datastore {
connect() {
console.log('postgresql connect step');
}
query(query) {
console.log(`postgresql execute query: ${query}`);
return ['some data'];
}
disconnect() {
console.log('postgresql disconnect step');
}
}
const mySQLDatastore = new MySQLDatastore();
const postgreSQLDatastore = new PostgreSQLDatastore();
mySQLDatastore.process('SELECT * FROM users');
postgreSQLDatastore.process('SELECT * FROM users');