Difference between Extract Delegate and Extract Method Object?
Answered
Hi,
As per the documentation,
- The Extract Delegate refactoring lets you extract some of the fields and methods of a class into a separate, newly created class.
- The Extract Method Object refactoring moves method into a new class, converting all the local variables to its fields, allowing you to decompose the method into other methods on the same object
As such the only difference I see is that Extract Method is for only ONE method, where as Extract delegate is for multiple methods.
Is there any other difference? Can someone please explain the difference of intent here?
thanks
-Gaurav
Please sign in to leave a comment.
> As such the only difference I see is that Extract Method is for only ONE method, where as Extract delegate is for multiple methods.
Indeed, Extract Delegate can extract multiple class components compared to the Extract Method Object feature.
> Is there any other difference?
In addition to moving the method to a separate class, Extract Method Object creates new class fields based on used local variables of the method. For example:
Code sample before refactoring:
Code sample after applying refactoring for expression: x * z * Math.sqrt(y);
As you may notice a new field was added in the CaclNew class. It can be useful if you want to decompose your method into multiple methods in the newest created class.
More information and samples you can find in "Extract into class refactorings" guide.
Thanks a lot!
That helps...