Original post

In today’s article, we will talk about the refactoring, a new refactoring available since 2019.1.

This refactoring allows us to take a type and extract all the method definitions that we need, then create a new interface from them. This operation is particularly useful when going from a single implementation to using multiple implementations, such as in the example below.

To invoke this, you can use the Refactor This shortcut, Ctrl+Alt+Shift+T on Windows/Linux or ^T on macOS.

Refactoring - Extract Interface - 1

The Extract Interface dialog allows us to select any method from the existing type and control where will the interface be extracted. As such, you can create the interface in the current file/package, by default, or in a new file/package or existing ones but different than the current ones. You can also control if the methods should be exported or not, in case you do not want for external packages to be able to implement the interface.

Refactoring - Extract Interface - 2

That’s it for today. We learned how to use a new and powerful refactoring feature that will help us as the code base grows. Please let us know your feedback in the comments section below, on Twitter, or on our issue tracker.