java - Refactoring - Methods share the same code except for one function call -



java - Refactoring - Methods share the same code except for one function call -

i have several methods in class test have same code except 1 specific method call. there possibility merge these methods (bellow function foo) , phone call foo , tell method phone call without doing bigger switch or if/else statement? of import note foo called within class test (therefore foo private) , function foo calls different methods 1 class bar. class bar , test not in same inheritance tree.

class test { /* ... */ private void foo("parameter specifies method phone call class bar") { /* merged code equal */ bar bar = new bar(); bar.whichmethod(); // phone call method (from class bar) specified in parameter /* merged code equal*/ } /* ... */ }

sure straight forwards i'd add together kind of switch("which method call") statement. there improve way this?

you can pass method phone call argument. let's assume method phone call has next signature:

private void m() {...}

you write:

private void foo(runnable methodtorun) { //... methodtorun.run(); //... }

and various foo methods like:

private void foo1() { foo(new runnable() { public void run() { somemethod(); } }); }

with java 8 pass lambda or method reference.

java refactoring

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -