Skip to content

Commit 86eb585

Browse files
committed
update readme
1 parent b9832ab commit 86eb585

File tree

5 files changed

+16
-14
lines changed

5 files changed

+16
-14
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
*/node_modules
22
*/dist
3-
*/src/*/testfiles/*.txt
4-
*/src/testfiles/*.txt
3+
*/src/testfiles
54

65
TODO.txt

lsp/src/testfiles/.testfilesgohere

Whitespace-only changes.

ocp/src/testfiles/.testfilesgohere

Whitespace-only changes.

srp/README.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,18 @@ The answers are:
2525

2626
What we do is take each one of the reasons for change listed above and extract into a separate class.
2727

28-
So firstly, let's extract all _logging logic_ into a new class called [StoreLogger](./src/after/StoreLogger.ts) like so. Note that it is domain specific for our Store and not just a generic Logger.
28+
So firstly, let's extract all _logging logic_ into a new class called [StoreLogger](./src/StoreLogger.ts) like so. Note that it is domain specific for our Store and not just a generic Logger.
2929

3030
What we have done is extracted all the various log calls to a new class. Now this means that if we want to change the logging framework that we use - perhaps because the open source project that built the logger framework becomes depreciated or we find a better solution well now we can change in just this one place.
3131

32-
To use this new logger, we create an instance of it in the [message store class](./src/after/MessageStore.ts) - perviously called FileStore (I'll explain why the name was changed below).
32+
To use this new logger, we create an instance of it in the [MessageStore](./src/MessageStore.ts) class - perviously called FileStore (I'll explain why the name was changed below).
3333

3434
Now we replace the calls to `console.xyz(...)` to `this.log.xyz(...)` for example:
3535

3636
* Replace: `console.log(“some message: ”, id)`
3737
* With: `this.log.Saving(id)`
3838

39-
The next thing we need to extract is the logic for caching which can be applied in pretty much the same way. We create a new class [StoreCache](./src/after/StoreCache.ts).
39+
The next thing we need to extract is the logic for caching which can be applied in pretty much the same way. We create a new class [StoreCache](./src/StoreCache.ts).
4040

4141
Then we need to create an instance of this class in our MessageStore class and call that instead through the implementation.
4242

@@ -46,7 +46,7 @@ this.cache = new StoreCache(); // in the MessageStore constructor
4646
this.cache.AddOrUpdate(id, message); // in the Save method
4747
```
4848

49-
Next reason to change that we can address is the way that we apply storage. This will allow us to change where we save files - perhaps to a relational database instead of a filestore - who knows! So what we do is create a separate class called FileSore that is just for reading / writing files to the filestore and use that in our [MessageStore](./src/after/MessageStore.ts) class in the same way as before.
49+
Next reason to change that we can address is the way that we apply storage. This will allow us to change where we save files - perhaps to a relational database instead of a filestore - who knows! So what we do is create a separate class called FileSore that is just for reading / writing files to the filestore and use that in our [MessageStore](./src/MessageStore.ts) class in the same way as before.
5050

5151
So what are we left with? A better implementation for this class that is now split into other classes each with a Single Responsibility!
5252

@@ -60,17 +60,15 @@ Install the dependences. Note this includes `node-ts` which allows to run TypeSc
6060
npm install
6161
```
6262

63-
Now, it should be possible to run the application using the following commands. Note the code in [src/before](./src/before) is _before_ we apply any refactoring for the Single Responsibility Principle and the code in [src/after](./src/after) is _after_ we apply the refactoring for SRP.
63+
Now, it should be possible to run the application using the following commands. Note this is the _first step_ that we are taking to refactor the original file which is in the root of this project which is the file [FileStore.ts](../FileStore.ts).
6464

6565
```
66-
npx ts-node src/before/TestExamples.ts
67-
npx ts-node src/after/TestExamples.ts
66+
npx ts-node src/TestExamples.ts
6867
```
6968

7069
Alternatively, you can compile the TypeScript files and then run the output JavaScript files form the resuling `dist` folder:
7170

7271
```
7372
npm run build
74-
node dist/before/TestExamples.js
75-
node dist/after/TestExamples.js
73+
node dist/TestExamples.js
7674
```

srp/src/TestExamples.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import StoreLogger from './StoreLogger'
22
import StoreCache from './StoreCache'
33
import FileStore from './FileStore'
44
import MessageStore from './MessageStore';
5+
import fs from 'fs';
6+
import path from 'path'
7+
8+
var dirtest = "./testfiles";
9+
var dirpath = path.join(__dirname, dirtest)
10+
fs.mkdirSync(dirpath)
511

612
// Test the StoreLogger class
713
console.log("** Test the StoreLogger class **")
@@ -31,8 +37,7 @@ console.log();
3137
// Test the FileStore class
3238
console.log("** Test the FileStore class **")
3339
console.log()
34-
var directory = "./testfiles"
35-
var filestore = new FileStore(directory, logger)
40+
var filestore = new FileStore(dirtest, logger)
3641
var fileInfo = filestore.getFileInfo(1)
3742
console.log(fileInfo);
3843
await filestore.save(1, 'Message File 1')
@@ -48,7 +53,7 @@ console.log();
4853
// Test the MessageStore class
4954
console.log("** Test the MessageStore class **")
5055
console.log()
51-
var messagestore = new MessageStore(directory);
56+
var messagestore = new MessageStore(dirtest);
5257
await messagestore.save(99, 'Message 99 saved via MessageStore class')
5358
var fileMessage99 = messagestore.read(99)
5459
console.log(fileMessage99)

0 commit comments

Comments
 (0)