You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: srp/README.md
+7-9Lines changed: 7 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,18 +25,18 @@ The answers are:
25
25
26
26
What we do is take each one of the reasons for change listed above and extract into a separate class.
27
27
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.
29
29
30
30
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.
31
31
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).
33
33
34
34
Now we replace the calls to `console.xyz(...)` to `this.log.xyz(...)` for example:
35
35
36
36
* Replace: `console.log(“some message: ”, id)`
37
37
* With: `this.log.Saving(id)`
38
38
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).
40
40
41
41
Then we need to create an instance of this class in our MessageStore class and call that instead through the implementation.
42
42
@@ -46,7 +46,7 @@ this.cache = new StoreCache(); // in the MessageStore constructor
46
46
this.cache.AddOrUpdate(id, message); // in the Save method
47
47
```
48
48
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.
50
50
51
51
So what are we left with? A better implementation for this class that is now split into other classes each with a Single Responsibility!
52
52
@@ -60,17 +60,15 @@ Install the dependences. Note this includes `node-ts` which allows to run TypeSc
60
60
npm install
61
61
```
62
62
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).
64
64
65
65
```
66
-
npx ts-node src/before/TestExamples.ts
67
-
npx ts-node src/after/TestExamples.ts
66
+
npx ts-node src/TestExamples.ts
68
67
```
69
68
70
69
Alternatively, you can compile the TypeScript files and then run the output JavaScript files form the resuling `dist` folder:
0 commit comments