Skip to content

Commit 33ce037

Browse files
committed
add docs
1 parent b15df82 commit 33ce037

File tree

1 file changed

+92
-60
lines changed

1 file changed

+92
-60
lines changed

README.md

Lines changed: 92 additions & 60 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,33 +18,63 @@ 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:
42+
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+
```
63+
64+
And some possible usages are:
65+
66+
```javascript
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}`);
75+
```
76+
77+
Our goal here is to introduce a factory function, so clients don't do `new` employees any longer.
8178

8279
### Test suite
8380

@@ -95,22 +92,55 @@ Magna ut tempor et ut elit culpa id minim Lorem aliqua laboris aliqua dolor. Iru
9592

9693
### Steps
9794

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.
95+
- introduce `createEmployee` function:
96+
97+
```diff
98+
diff --git top level...
99+
+export function createEmployee(name, typeCode) {
100+
+ return new Employee(name, typeCode);
101+
+}
102+
```
103+
104+
- update callers to use `createEmployee` instead of initializing the class themselves:
105+
106+
```diff
107+
diff --git top level...
108+
-const candidate = new Employee(candidateDoc.name, candidateDoc.empType);
109+
+const candidate = createEmployee(candidateDoc.name, candidateDoc.empType);
110+
// ...
111+
-const leadEngineer = new Employee(leadEngineerDoc.leadEngineer, 'E');
112+
+const leadEngineer = createEmployee(leadEngineerDoc.leadEngineer, 'E');
113+
```
114+
115+
- rename `createEmployee` to `createEngineer`:
116+
117+
```diff
118+
diff --git top level...
119+
-const candidate = createEmployee(candidateDoc.name, candidateDoc.empType);
120+
+const candidate = createEngineer(candidateDoc.name, candidateDoc.empType);
121+
// ...
122+
-const leadEngineer = createEmployee(leadEngineerDoc.leadEngineer, 'E');
123+
+const leadEngineer = createEngineer(leadEngineerDoc.leadEngineer, 'E');
124+
// ...
125+
-export function createEmployee(name, typeCode) {
126+
+export function createEngineer(name, typeCode) {
127+
```
128+
129+
- make employee type fixed to `'E'` at `createEngineer`:
99130

100131
```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;
132+
diff --git top level...
133+
export function createEngineer(name, typeCode) {
134+
- return new Employee(name, typeCode);
135+
+ return new Employee(name, 'E');
105136
```
106137

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.
138+
- remove `typeCode` argument from `createEngineer`:
108139

109140
```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;
141+
diff --git top level...
142+
-export function createEngineer(name, typeCode) {
143+
+export function createEngineer(name) {
114144
```
115145

116146
And that's it!
@@ -119,10 +149,12 @@ And that's it!
119149

120150
Below there's the commit history for the steps detailed above.
121151

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 |
152+
| Commit SHA | Message |
153+
| ------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------- |
154+
| [3ff0ba7](https://github.com/kaiosilveira/replace-constructor-with-factory-function-refactoring/commit/3ff0ba7a63d438f2c52835038fecbc32abd566dc) | introduce `createEmployee` function |
155+
| [75b2e05](https://github.com/kaiosilveira/replace-constructor-with-factory-function-refactoring/commit/75b2e05b7bc438e28634607907cea334524c8f13) | update callers to use `createEmployee` instead of initializing the class themselves |
156+
| [e48639d](https://github.com/kaiosilveira/replace-constructor-with-factory-function-refactoring/commit/e48639dc8b4318c8619e92a7a4d300fe98e9b08b) | rename `createEmployee` to `createEngineer` |
157+
| [9886939](https://github.com/kaiosilveira/replace-constructor-with-factory-function-refactoring/commit/988693979e648c571d8ed30097df3fe25329fc4a) | make employee type fixed to `'E'` at `createEngineer` |
158+
| [b15df82](https://github.com/kaiosilveira/replace-constructor-with-factory-function-refactoring/commit/b15df829e562eff6d02e1cc61b36dcf6e66634c4) | remove `typeCode` argument from `createEngineer` |
127159

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).
160+
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)