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
ℹ️ _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._
37
4
38
5
---
39
6
40
7
# Replace Constructor With Factory Function
41
8
42
-
**Formerly: Old name**
9
+
**Formerly: Replace Constructor with Factory Method**
**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.
77
38
78
39
## Working example
79
40
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:
Our goal here is to introduce a factory function, so clients don't do `new` employees any longer.
81
78
82
79
### Test suite
83
80
@@ -95,22 +92,55 @@ Magna ut tempor et ut elit culpa id minim Lorem aliqua laboris aliqua dolor. Iru
95
92
96
93
### Steps
97
94
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);
**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`:
|[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`|
127
159
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