Remove middleman
The Remove Middleman refactoring lets you replace all calls to delegating methods in a class with the equivalent calls directly to the field delegated to. Additionally, you can automatically remove the classes delegating methods, which will now be unused.
This refactoring is useful if you have a class that simply forwards many of its method calls to objects of other classes, and you wish to simplify your design.
Open the class in question in the editor, and position the caret at the name of the delegating field.
On the main or context menu, select
.In the dialog that opens, select methods that you want to inline.
Example
Before | After |
---|---|
public class Foo {
Bar bar;
public Foo getImpValue() {
return bar.getImpValue();
}
}
public class Bar {
private Foo impValue1;
public Bar(Foo impValue) {
impValue1 = impValue;
}
public Foo getImpValue() {
return impValue1;
}
}
public class Client {
Foo a;
Foo impValue = a.getImpValue();
}
|
public class Foo {
Bar bar;
public Bar getbar() {
return bar;
}
}
public class Bar {
private Foo impValue1;
public Bar(Foo impValue) {
impValue1 = impValue;
}
public Foo getImpValue(){
return impValue1;
}
}
public class Client {
Foo a;
Foo impValue = a.getbar().getImpValue();
}
|
Last modified: 01 August 2022