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:
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.
95
82
96
83
### Steps
97
84
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);
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`:
**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`:
|[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`|
127
149
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