IntelliJ IDEA 2017.3 Help

Extract Delegate

The Extract Delegate refactoring lets you extract some of the fields and methods of a class into a separate, newly created class. This refactoring is useful, when a class has grown too large and "does too many things". In such cases, it might be a good idea to split the class into smaller, more cohesive classes.

Example

BeforeAfter
public class Foo { private String b; public String getInfo() { return ("(" + b + ")"); } ... } public class Bar { Foo foo; String t2 = foo.getInfo(); ... }
public class Foo { private final Info info = new Info(); public String getInfo() { return info.getInfo(); } ... } public class Info { private String b; public Info() {} public String getInfo() { return ("(" + b + ")"); } } public class Bar { Foo foo; String t2 = foo.getInfo(); ... }

To perform Extract Delegate refactoring, follow these steps

  1. Open the class in the editor, or select it in the Project tool window.
  2. Select Refactor | Extract | Delegate from the main or the context menu.
  3. In the Extract Class dialog that opens:
    • Specify the name and package for the class to be created.
    • Selects the fields and methods to be included in the new class.
    • Click Preview to see the usages of the selected fields or methods in the Find tool window. Select the usages to be included in the refactoring and click Do Refactor.

Note that this refactoring can also be accessed from a UML Class diagram.

Last modified: 6 March 2018

See Also