java - Can IntelliJ automatically create a decorator class? -


sometimes, create decorator class this:

class myinterfacedecorator implements myinterface {    private final myinterface delegate;    ... constructor taking myinterface instance ...     @override    public object someinterfacemethod(some argument) {        return delegate.someinterfacemethod(argument);    }     ... etc, more methods here... } 

can intellij automatically create class me?

update//

i noticed intellij has "generate" option generating delegate methods. create new class:

public class mydecoratorclass {     private myinterfacewithmanymethods myinterface; } 

then mark myinterface, go menu > code > delegate methods, select methods want wrap , that's it.

//end of update

you try "refactoring" -> "replace inheritance delegation" refactoring. should able this, this. call "code alt+enter"

go interface want generate decorator for.

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

press alt+enter, select "implement interface", give name decorator "mydecorator". gives you

public class mydecorator implements myinterfacewithmanymethods {     public void method1() {     }     public void method2() {     }     public void method3() {     } } 

in new class, select class name, "refactor" -> "replace inheritance delegation", select interface, tick 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 inner class , object initializer manually. get:

public class mydecorator {       public void method1() {         object.method1();     }      public void method2() {         object.method2();     }      public void method3() {         object.method3();     }  } 

press alt+enter on "object" 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 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 done few strokes of alt+enter. reads lot of work can done in less 20 seconds. if have 2 or 3 methods might faster live template, if have many methods complex signatures you'll working result in 20 seconds method. alt+enter rocks :d


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -