IntelliJ能自动创建一个装饰类吗? [英] Can IntelliJ automatically create a decorator class?

查看:107
本文介绍了IntelliJ能自动创建一个装饰类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候,我创建一个这样的装饰类:

  class MyInterfaceDecorator实现MyInterface {
private final MyInterface delegate ;
...构造函数采用MyInterface实例...

@Override
public Object someInterfaceMethod(Some argument){
return delegate.someInterfaceMethod(argument);
}

...等,这里有更多的方法...
}

IntelliJ可以为我自动创建这个类吗?

解决方案

更新//



我注意到IntelliJ有一个Generate选项来生成委托方法。创建一个新类:

  public class MyDecoratorClass {
private MyInterfaceWithManyMethods myInterface;
}

然后标记myInterface,转到菜单>代码>委托方法,选择所有方法你想要包装就是这样。



//更新结束



您可以尝试重构 - >使用委托替换继承重构。它应该能够这样做,像这样。我将这个Alt + Enter代码称为

转到要为其生成装饰器的界面。

  public interface MyInterfaceWithManyMethods {
void method1();
void method2();
void method3();
}

按Alt + Enter,选择实现界面,给你的名字装饰器像MyDecorator。这给你

  public class MyDecorator implements MyInterfaceWithManyMethods {
public void method1(){
}
public void method2(){
}
public void method3(){
}
}

在新类中,选择类名,然后选择Refactor - >使用委托替换继承,选择您的界面,勾选所有方法名称,按回车键。你会得到:

  public class MyDecorator {

private final MyObject object = new MyObject() ;

public void method1(){
object.method1();
}

public void method2(){
object.method2();
}

public void method3(){
object.method3();
}

私有类MyObject实现MyInterfaceWithManyMethods {
public void method1(){

}

public void method2 ){

}

public void method3(){

}
}
}

手动删除内部类和对象初始值设置。你得到:

  public class MyDecorator {


public void method1(){
object.method1();
}

public void method2(){
object.method2();
}

public void method3(){
object.method3();
}

}

按Alt + Enter对象,现在标记为红色,选择创建字段,选择MyInterfaceWithManyMethods。

  public class MyDecorator {


private MyInterfaceWithManyMethods对象;

public void method1(){
object.method1();
}

public void method2(){
object.method2();
}

public void method3(){
object.method3();
}

}

选择对象变量,按Alt + Enter,选择添加构造函数参数:

  public class MyDecorator {


私人MyInterfaceWithManyMethods对象;

public MyDecorator(MyInterfaceWithManyMethods对象){
this.object = object;
}

public void method1(){
object.method1();
}

public void method2(){
object.method2();
}

public void method3(){
object.method3();
}

}

你看到这一切都是用一个Alt + Enter几行。阅读像很多工作,但可以在不到20秒钟内完成。如果你只有2或3种方法,你可能会更快地使用实际的模板,但是如果你有很多方法与复杂的签名,你会得到一个工作的结果在这个方法约20秒。 Alt +输入简单的岩石:D


Sometimes, I create a decorator class like this:

class MyInterfaceDecorator implements MyInterface {
   private final MyInterface delegate;
   ... constructor taking a MyInterface instance ...

   @Override
   public Object someInterfaceMethod(Some argument) {
       return delegate.someInterfaceMethod(argument);
   }

   ... etc, more methods here...
}

Can IntelliJ automatically create this class for me?

解决方案

Update//

I noticed that IntelliJ has a "Generate" option for generating delegate methods. Create a new class:

public class MyDecoratorClass {
    private MyInterfaceWithManyMethods myInterface;
}

Then mark myInterface, go to Menu > Code > Delegate Methods, select all methods you want to wrap and that's it.

//End of update

You could try the "Refactoring" -> "Replace inheritance with delegation" refactoring. It should be able to do this, like this. I call this "Code with Alt+Enter"

Go to the interface you want to generate a decorator for.

public interface MyInterfaceWithManyMethods {
    void method1();
    void method2();
    void method3();
}

Press Alt+Enter, select "Implement Interface", give a name to your Decorator like "MyDecorator". This gives you

public class MyDecorator implements MyInterfaceWithManyMethods {
    public void method1() {
    }
    public void method2() {
    }
    public void method3() {
    }
}

In new class, select the class name, then "Refactor" -> "Replace inheritance with delegation", select your interface, tick all method names, press enter. You'll get:

public class MyDecorator {

    private final MyObject object = new MyObject();

    public void method1() {
        object.method1();
    }

    public void method2() {
        object.method2();
    }

    public void method3() {
        object.method3();
    }

    private class MyObject implements MyInterfaceWithManyMethods {
        public void method1() {

        }

        public void method2() {

        }

        public void method3() {

        }
    }
}

Delete the inner class and the object initializer manually. You get:

public class MyDecorator {


    public void method1() {
        object.method1();
    }

    public void method2() {
        object.method2();
    }

    public void method3() {
        object.method3();
    }

}

Press Alt+Enter on the "object" which is now marked red, select "Create field", select MyInterfaceWithManyMethods.

public class MyDecorator {


    private MyInterfaceWithManyMethods object;

    public void method1() {
        object.method1();
    }

    public void method2() {
        object.method2();
    }

    public void method3() {
        object.method3();
    }

}

Select the object variable, press Alt+Enter, select "Add constructor Parameter":

public class MyDecorator {


    private MyInterfaceWithManyMethods object;

    public MyDecorator(MyInterfaceWithManyMethods object) {
        this.object = object;
    }

    public void method1() {
        object.method1();
    }

    public void method2() {
        object.method2();
    }

    public void method3() {
        object.method3();
    }

}

You see it's all done with a few strokes of Alt+Enter. Reads like a lot of work but it can be done in less than 20 seconds. If you just have like 2 or 3 methods you might be faster with a live template, however if you have many methods with complex signatures you'll get a working result in about 20 seconds with this method. Alt+Enter simply rocks :D

这篇关于IntelliJ能自动创建一个装饰类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆