Skip to content

Commit b0f700a

Browse files
committed
[Error] Creating Abstract Factory
* error TS2511: Cannot create an instance of an abstract class.
1 parent 808da8d commit b0f700a

File tree

5 files changed

+50
-2
lines changed

5 files changed

+50
-2
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ npm install
2929
npm start
3030
```
3131

32+
## Reference
33+
34+
https://sourcemaking.com/design_patterns
35+
3236
## Abstract Factory
3337

3438
## Factory Method

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"license": "MIT",
99
"scripts": {
1010
"start": "tsc-watch --onSuccess \"node dist/index.js\"",
11+
"start:abstract-factory": "tsc-watch --onSuccess \"node dist/index.js\"",
1112
"test": "jest --watch"
1213
},
1314
"dependencies": {

src/abstract-factory/book-store.ts

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Book Factory
3+
*/
4+
abstract class AbstractBookFactory {
5+
abstract createJSBook(): AbstractJSBook;
6+
// abstract createTSBook(): AbstractTSBook;
7+
}
8+
9+
class QvilBookFactory extends AbstractBookFactory {
10+
createJSBook() {
11+
return new AbstractJSBook(); // error TS2511: Cannot create an instance of an abstract class.
12+
}
13+
createTSBook() {
14+
return "ts";
15+
}
16+
}
17+
18+
/**
19+
* Book
20+
*/
21+
abstract class AbstractBook {
22+
abstract getAuthor(): string;
23+
abstract getTitle(): string;
24+
}
25+
abstract class AbstractJSBook extends AbstractBook {
26+
// constructor(value: string) {
27+
// super();
28+
// console.log(value);
29+
// }
30+
31+
getTitle() {
32+
return this.title;
33+
}
34+
35+
private title = "Javscript";
36+
}

src/abstract-factory/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import book from "./book-store";
2+
3+
console.log(book);

src/index.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
import { sum } from "./closure/ScopeChain";
1+
// import { sum } from "./closure/ScopeChain";
22

3-
console.log(sum(1)(2)(3)(4));
3+
// console.log(sum(1)(2)(3)(4));
4+
5+
import bookStore from "./abstract-factory/book-store";
6+
7+
console.log(bookStore());

0 commit comments

Comments
 (0)