The next step, then, is to remove all use of new
keyword from our non-Factory classes, and inject the necessary dependencies instead. We will also use Factory objects as needed to deal with repeated creation dependencies. This is the general process we will follow:
Find a class with the
new
keyword in it. If the class is already aFactory
, we can ignore it and move on.For each one-time creation in the class:
Extract each instantiation to a constructor parameter.
Assign the constructor parameter to a property.
Remove any constructor parameters and class properties that are used only for the
new
call.
For each repeated creation in the class:
Extract each block of creation code to a new
Factory
class.Create a constructor parameter for each
Factory
and assign it to a property.Modify the previous creation logic in the class to use the Factory.
Change all instantiation calls for the modified class throughout the project so that the necessary dependency objects are passed to the...