Skip to content

Commit a8177a3

Browse files
committed
add docs
1 parent d4ea521 commit a8177a3

File tree

1 file changed

+90
-68
lines changed

1 file changed

+90
-68
lines changed

README.md

Lines changed: 90 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,12 @@
11
[![Continuous Integration](https://github.com/kaiosilveira/replace-constructor-with-factory-function-refactoring/actions/workflows/ci.yml/badge.svg)](https://github.com/kaiosilveira/replace-constructor-with-factory-function-refactoring/actions/workflows/ci.yml)
22

3-
# Refactoring catalog repository template
4-
5-
This is a quick template to help me get a new refactoring repo going.
6-
7-
## Things to do after creating a repo off of this template
8-
9-
1. Run `GITHUB_TOKEN=$(gh auth token) yarn tools:cli prepare-repository -r <repo_name>`. It will:
10-
11-
- Update the `README.md` file with the actual repository name, CI badge, and commit history link
12-
- Update `package.json` with the repository's name and remote URL
13-
- Update the repo's homepage on GitHub with:
14-
- A description
15-
- A website link to https://github.com/kaiosilveira/refactoring
16-
- The following labels: javascript, refactoring, replace-constructor-with-factory-function-refactoring
17-
18-
2. Replace the lorem ipsum text sections below with actual text
19-
20-
## Useful commands
21-
22-
- Generate markdown containing a diff with patch information based on a range of commits:
23-
24-
```bash
25-
yarn tools:cli generate-diff -f <first_commit_sha> -l <last_commit_sha>
26-
```
27-
28-
- To generate the commit history table for the last section, including the correct links:
29-
30-
```bash
31-
yarn tools:cli generate-cmt-table -r replace-constructor-with-factory-function-refactoring
32-
```
33-
34-
---
35-
363
ℹ️ _This repository is part of my Refactoring catalog based on Fowler's book with the same title. Please see [kaiosilveira/refactoring](https://github.com/kaiosilveira/refactoring) for more details._
374

385
---
396

407
# Replace Constructor With Factory Function
418

42-
**Formerly: Old name**
9+
**Formerly: Replace Constructor with Factory Method**
4310

4411
<table>
4512
<thead>
@@ -51,66 +18,119 @@ yarn tools:cli generate-cmt-table -r replace-constructor-with-factory-function-r
5118
<td>
5219

5320
```javascript
54-
result = initial.code;
21+
leadEngineer = new Employee(document.leadEngineer, 'E');
5522
```
5623

5724
</td>
5825

5926
<td>
6027

6128
```javascript
62-
result = newCode();
63-
64-
function newCode() {
65-
return 'new code';
66-
}
29+
leadEngineer = createEngineer(document.leadEngineer);
6730
```
6831

6932
</td>
7033
</tr>
7134
</tbody>
7235
</table>
7336

74-
**Inverse of: [Another refactoring](https://github.com/kaiosilveira/refactoring)**
75-
76-
**Refactoring introduction and motivation** dolore sunt deserunt proident enim excepteur et cillum duis velit dolor. Aute proident laborum officia velit culpa enim occaecat officia sunt aute labore id anim minim. Eu minim esse eiusmod enim nulla Lorem. Enim velit in minim anim anim ad duis aute ipsum voluptate do nulla. Ad tempor sint dolore et ullamco aute nulla irure sunt commodo nulla aliquip.
37+
Sometimes we need more control over initialization than a constructor can possibly provide, and that's where a [Factory Method](https://github.com/kaiosilveira/design-patterns/tree/main/factory-method) comes in handy: it can hide complex initialization logic, replace the resulting instance with a proxy or decorate it with complementary behavior. This refactoring helps with moving towards this approach.
7738

7839
## Working example
7940

80-
**Working example general explanation** proident reprehenderit mollit non voluptate ea aliquip ad ipsum anim veniam non nostrud. Cupidatat labore occaecat labore veniam incididunt pariatur elit officia. Aute nisi in nulla non dolor ullamco ut dolore do irure sit nulla incididunt enim. Cupidatat aliquip minim culpa enim. Fugiat occaecat qui nostrud nostrud eu exercitation Lorem pariatur fugiat ea consectetur pariatur irure. Officia dolore veniam duis duis eu eiusmod cupidatat laboris duis ad proident adipisicing. Minim veniam consectetur ut deserunt fugiat id incididunt reprehenderit.
41+
Our working example is a program that creates `Employee` instances based on a document input plus employee type. The `Employee` class is straightforward:
8142

82-
### Test suite
43+
```javascript
44+
export class Employee {
45+
constructor(name, typeCode) {
46+
this._name = name;
47+
this._typeCode = typeCode;
48+
}
49+
50+
get name() {
51+
return this._name;
52+
}
53+
54+
get type() {
55+
return Employee.legalTypeCodes[this._typeCode];
56+
}
57+
58+
static get legalTypeCodes() {
59+
return { E: 'Engineer', M: 'Manager', S: 'Salesperson' };
60+
}
61+
}
62+
```
8363

84-
Occaecat et incididunt aliquip ex id dolore. Et excepteur et ea aute culpa fugiat consectetur veniam aliqua. Adipisicing amet reprehenderit elit qui.
64+
And some possible usages are:
8565

8666
```javascript
87-
describe('functionBeingRefactored', () => {
88-
it('should work', () => {
89-
expect(0).toEqual(1);
90-
});
91-
});
67+
const candidateDoc = { name: 'John Doe', empType: 'E' };
68+
const candidate = new Employee(candidateDoc.name, candidateDoc.empType);
69+
70+
const leadEngineerDoc = { leadEngineer: 'Jane Doe' };
71+
const leadEngineer = new Employee(leadEngineerDoc.leadEngineer, 'E');
72+
73+
console.log(`Candidate: ${candidate.name}, ${candidate.type}`);
74+
console.log(`Lead engineer: ${leadEngineer.name}, ${leadEngineer.type}`);
9275
```
9376

94-
Magna ut tempor et ut elit culpa id minim Lorem aliqua laboris aliqua dolor. Irure mollit ad in et enim consequat cillum voluptate et amet esse. Fugiat incididunt ea nulla cupidatat magna enim adipisicing consequat aliquip commodo elit et. Mollit aute irure consequat sunt. Dolor consequat elit voluptate aute duis qui eu do veniam laborum elit quis.
77+
Our goal here is to introduce a factory function, so clients don't do `new` employees any longer.
78+
79+
### Test suite
80+
81+
Since we're doing with a simple class, there is no test suite in place. Tests will be introduced as we go, though, for the `createEmployee` factory function.
9582

9683
### Steps
9784

98-
**Step 1 description** mollit eu nulla mollit irure sint proident sint ipsum deserunt ad consectetur laborum incididunt aliqua. Officia occaecat deserunt in aute veniam sunt ad fugiat culpa sunt velit nulla. Pariatur anim sit minim sit duis mollit.
85+
We start by introducing the `createEmployee` function:
86+
87+
```diff
88+
diff --git top level...
89+
+export function createEmployee(name, typeCode) {
90+
+ return new Employee(name, typeCode);
91+
+}
92+
```
93+
94+
Then, we update the callers to use the function instead of initializing the class themselves:
95+
96+
```diff
97+
diff --git top level...
98+
-const candidate = new Employee(candidateDoc.name, candidateDoc.empType);
99+
+const candidate = createEmployee(candidateDoc.name, candidateDoc.empType);
100+
// ...
101+
-const leadEngineer = new Employee(leadEngineerDoc.leadEngineer, 'E');
102+
+const leadEngineer = createEmployee(leadEngineerDoc.leadEngineer, 'E');
103+
```
104+
105+
With that in place, and since we're only creating engineer employees, a decision was made to make the function more specific. We start by renaming `createEmployee` to `createEngineer`:
106+
107+
```diff
108+
diff --git top level...
109+
-const candidate = createEmployee(candidateDoc.name, candidateDoc.empType);
110+
+const candidate = createEngineer(candidateDoc.name, candidateDoc.empType);
111+
// ...
112+
-const leadEngineer = createEmployee(leadEngineerDoc.leadEngineer, 'E');
113+
+const leadEngineer = createEngineer(leadEngineerDoc.leadEngineer, 'E');
114+
// ...
115+
-export function createEmployee(name, typeCode) {
116+
+export function createEngineer(name, typeCode) {
117+
```
118+
119+
And then, we make the employee type fixed to `'E'` at `createEngineer`:
99120

100121
```diff
101-
diff --git a/src/price-order/index.js b/src/price-order/index.js
102-
@@ -3,6 +3,11 @@
103-
-module.exports = old;
104-
+module.exports = new;
122+
diff --git top level...
123+
export function createEngineer(name, typeCode) {
124+
- return new Employee(name, typeCode);
125+
+ return new Employee(name, 'E');
105126
```
106127

107-
**Step n description** mollit eu nulla mollit irure sint proident sint ipsum deserunt ad consectetur laborum incididunt aliqua. Officia occaecat deserunt in aute veniam sunt ad fugiat culpa sunt velit nulla. Pariatur anim sit minim sit duis mollit.
128+
Finally, we can [remove the flag argument](https://github.com/kaiosilveira/remove-flag-argument-refactoring) `typeCode` from `createEngineer`:
108129

109130
```diff
110-
diff --git a/src/price-order/index.js b/src/price-order/index.js
111-
@@ -3,6 +3,11 @@
112-
-module.exports = old;
113-
+module.exports = new;
131+
diff --git top level...
132+
-export function createEngineer(name, typeCode) {
133+
+export function createEngineer(name) {
114134
```
115135

116136
And that's it!
@@ -119,10 +139,12 @@ And that's it!
119139

120140
Below there's the commit history for the steps detailed above.
121141

122-
| Commit SHA | Message |
123-
| --------------------------------------------------------------------------- | ------------------------ |
124-
| [cmt-sha-1](https://github.com/kaiosilveira/replace-constructor-with-factory-function-refactoring/commit-SHA-1) | description of commit #1 |
125-
| [cmt-sha-2](https://github.com/kaiosilveira/replace-constructor-with-factory-function-refactoring/commit-SHA-2) | description of commit #2 |
126-
| [cmt-sha-n](https://github.com/kaiosilveira/replace-constructor-with-factory-function-refactoring/commit-SHA-n) | description of commit #n |
142+
| Commit SHA | Message |
143+
| ------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------- |
144+
| [3ff0ba7](https://github.com/kaiosilveira/replace-constructor-with-factory-function-refactoring/commit/3ff0ba7a63d438f2c52835038fecbc32abd566dc) | introduce `createEmployee` function |
145+
| [75b2e05](https://github.com/kaiosilveira/replace-constructor-with-factory-function-refactoring/commit/75b2e05b7bc438e28634607907cea334524c8f13) | update callers to use `createEmployee` instead of initializing the class themselves |
146+
| [e48639d](https://github.com/kaiosilveira/replace-constructor-with-factory-function-refactoring/commit/e48639dc8b4318c8619e92a7a4d300fe98e9b08b) | rename `createEmployee` to `createEngineer` |
147+
| [9886939](https://github.com/kaiosilveira/replace-constructor-with-factory-function-refactoring/commit/988693979e648c571d8ed30097df3fe25329fc4a) | make employee type fixed to `'E'` at `createEngineer` |
148+
| [b15df82](https://github.com/kaiosilveira/replace-constructor-with-factory-function-refactoring/commit/b15df829e562eff6d02e1cc61b36dcf6e66634c4) | remove `typeCode` argument from `createEngineer` |
127149

128-
For the full commit history for this project, check the [Commit History tab](https://github.com/kaiosilveira/replace-constructor-with-factory-function-refactoring/commits/main).
150+
For the full commit history for this project, check the [Commit History tab](https://github.com/kaiosilveira/replace-constructor-with-factory-function-refactoring/commits/main).

0 commit comments

Comments
 (0)