0

I am newbie to java, and developing a real life project. I have many methods and about 2500 lines of code thus far. Many of the methods are slightly different(usually a difference of mere a single identifier) due to which i have to copy the code again and again with slight changes. What i want is to pass a method as parameter to another method, I've gone through lambda expressions but i could not find it enough appealing, off-course due to my own conceptual shortcomings. because it tells to define functional interface of each method to be passed. but as per my thoughts it would not give me a generic code so that i would be able to simply add some other Tables in future.

i am putting a piece of code to demonstrate and better explain my problem.

if(houseTabPanel.getComponentCount()==0){
                     houseTableDb();
                    }
                    if(isSelected){
                        selection(houseTable);
                    }
                    else {
                        houseTable.setColumnSelectionAllowed(false);
                        houseTable.setRowSelectionAllowed(false);
                        houseTable.setCellSelectionEnabled(false);
                        Rselection(houseTable);
                    }

now i have different methods named houseTableDb() , plotTableDb() , adminTableDb() etc. i want to make a method of this piece of code and pass plotTableDb() etc as parameter.. something like...

public void genericMethod(JPanel p, JTable t, some method reference to use instead of houseTableDb){}

pardon me if am not descriptive enough.. any response would be truly appreciated by core of the heart.

  • Possible duplicate of [Java Pass Method as Parameter](http://stackoverflow.com/questions/2186931/java-pass-method-as-parameter) – OO7 Nov 19 '14 at 08:28

2 Answers2

1

Provided that all of these methods have the same signature you can define an interface with a single method with that signature (return value, parameter list). Then you write classes implementing the method, one for each method's implementation. For passing the method, you create an object of that class and pass the object. The call to the actual method is replaced by the call to the method defined in the interface.

interface Callee {
    void meth();
}
class MethOne implements Callee {
    public void meth(){...}
}

void caller( Callee callee ){
    callee.meth();
}

Callee ofOne = new MethOne();
caller( ofOne );

But to avoid all this hazzle: that's why lambdas have been added...

laune
  • 31,114
  • 3
  • 29
  • 42
  • I appreciate your response, but sir, houseTableDb and other are different in the way that they access different tables in database, i just want a method signature like // callee(JPanel p, JTable t, some method reference to use instead of houseTableDb){}.. – Abubakar Iqbal Nov 19 '14 at 09:12
  • and i feel really sorry to bother u.. :( – Abubakar Iqbal Nov 19 '14 at 09:13
  • You write: "some method reference to use..." But that's exactly what I've shown you: a method reference. It just happens that the method reference is an object reference, and the method call to the (wrapped) method reference is a call to the (wrapping) object's only method. Also, see the link cited in that other 007 comment. – laune Nov 19 '14 at 18:51
0

You can do like this :

public void genericMethod(JPanel p, JTable t, TableDbCallBack tableDb)
{
    if(p.getComponentCount()==0)
    {
        tableDb.run();
    }
    if(isSelected)
    {
        selection(t);
    }
   else
   {
        t.setColumnSelectionAllowed(false);
        t.setRowSelectionAllowed(false);
        t.setCellSelectionEnabled(false);
        Rselection(t);
    }
}

usage :

genericMethod(p, t, new HouseTableDb());
genericMethod(p, t, new AdminTableDb());

Implementation :

public interface TableDbCallBack extends Runnable {}

public class HouseTableDb implements TableDbCallBack
{
    @Override
    public void run()
    {
         // Whatever it should do   
    }
} 

public class AdminTableDb implements TableDbCallBack
{
    @Override
    public void run()
    {
         // Whatever it should do   
    }
} 
ToYonos
  • 16,469
  • 2
  • 54
  • 70