Results 1 to 14 of 14
Thread: Method as an argument?
- 09-13-2009, 09:50 PM #1
Member
- Join Date
- Sep 2009
- Posts
- 14
- Rep Power
- 0
Method as an argument?
So, I want to write a method that will perform different methods according to the argument. I really don't want to use an if/else or switch structure, but rather have something like this:
...
if( ... )
someMethod = method1; //address or pointer to a method?
else
someMethod = method2;
...
... myMethod( someMethod, someArray ) {
someArray[i].someMethod; //or someMethod( someArray[i] )
...
}
So, is there a way to make a variable point to a specified method of a specified class?
- 09-13-2009, 10:01 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
You could do it using reflection but something smells here.
Could you describe the problem that you are trying to solve with this solution?
- 09-13-2009, 10:13 PM #3
Member
- Join Date
- Sep 2009
- Posts
- 14
- Rep Power
- 0
Ok, let's say I have an array of objects and depending on the user input, I want to perform different tasks to the objects in the array.
Let's say that in option x, I want to perform methods a and b while in option y I want to perform actions c and d.
I could write it as
if( x )
for( object o : array ) {
o.a();
o.b();
}
if( y )
for( object o : array ) {
o.c();
o.d();
}
But I'd rather have something like this:
if( x ) {
method1 = a;
method2 = b;
}
if( y ) {
method1 = c;
method2 = d;
}
for( object o : array ) {
o.method1;
o.method2;
}
-
You may want to look up and use a design pattern. I think that the Strategy Pattern would work well here.
Oh, could you please use code tags when posting code here? It would make it much easier to read your code.
- 09-13-2009, 10:25 PM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
How many different options can the user input?
- 09-14-2009, 12:45 AM #6
Member
- Join Date
- Sep 2009
- Posts
- 14
- Rep Power
- 0
The program that originally got me to think about this was a pretty simple sort and search program. It had a class Contact with:
and appropriate getter and setter methods.Java Code:private String name; private String relation; private String bday; private String phone; private String email;
The Tester class created Contact[] and had to perform a sort according to one of those parameters. I ended up writing all those sorts as different methods, but noticed how inefficient that was since the only difference between them was the comparison statements:
etc.Java Code:if( list[in].[COLOR="Red"]getName[/COLOR]().compareTo( list[in + 1].[COLOR="#ff0000"]getName[/COLOR]() ) > 0 ) if( list[in].[COLOR="RoyalBlue"]getEmail[/COLOR]().compareTo( list[in + 1].[COLOR="#4169e1"]getEmail[/COLOR]() ) > 0 )
Same thing with the find methods.
- 09-14-2009, 12:51 AM #7
Member
- Join Date
- Sep 2009
- Posts
- 14
- Rep Power
- 0
I haven't tried the Strategy Pattern yet, but it seems like that would work. Only there's a problem:
it seems that I would have to create separate classes for the getter methods. Could I use this to assign different methods from the same class to a variable?
-
You could use a HashMap of String or Enum as key and a Comparator object as your value, and then sort with the Comparator. No if statements would be necessary.
- 09-14-2009, 03:30 AM #9
Member
- Join Date
- Sep 2009
- Posts
- 14
- Rep Power
- 0
If I understand right, this works but isn't the solution I'm looking for.
Is there no way for me to write just one sorting method with a comparison line like this:
with a previously assigned getVariable() to perform getName(), getEmail(), etc.?Java Code:if( list[in].getVariable().compareTo( list[in + 1].getVariable() ) > 0 )
-
Again, why not use Comparator objects here?
- 09-14-2009, 05:10 AM #11
Member
- Join Date
- Sep 2009
- Posts
- 14
- Rep Power
- 0
Because I'm not asking just for the previously described program. Comparator and Comparable work well for comparing objects and it would be fine for sorting, but that program was just what got me thinking about this concept. Let's say that I need a program that will iterate through an array and perform different methods on it. For example, I want to convert Celsius temperatures to Fahrenheit or to convert them to Kelvin or visa-versa. So I write multiple methods:
Then, once I know which two scales I'm working with, I assign the right method to something (call it Convert). So, now, instead of using a switch or if/else structure, I can perform the right methods using Convert and go through the array calling it for each slot.Java Code:double CtoF( double t ) double CtoK( double t ) double FtoC( double t ) etc.
I'm not actually writing this exact program, but I'm really curious about this idea.
Also (sorry for repeating myself), can I use the Strategy Pattern idea for differently-named methods belonging to the same class?
-
The Java implementation of the Strategy pattern requires that you create classes that match the same interface. You can use the methods of these classes as you please within your code.
If you have the money and the time, you may want to purchase this and give it a read: Head First Design Patterns. It's well worth it.
- 09-14-2009, 08:33 AM #13
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 09-18-2009, 06:29 AM #14
Member
- Join Date
- Sep 2009
- Posts
- 14
- Rep Power
- 0
Similar Threads
-
Command line argument
By denisatandi in forum New To JavaReplies: 8Last Post: 10-16-2012, 11:37 PM -
getting the method and the class names (as argument, String varialbe)
By itaipee in forum New To JavaReplies: 4Last Post: 03-03-2009, 09:39 AM -
Entering a space as argument
By Arne Bjarne in forum EclipseReplies: 1Last Post: 03-02-2009, 12:43 PM -
Parsing Argument Values
By vipvan2000 in forum New To JavaReplies: 1Last Post: 03-06-2008, 11:01 AM -
Parsing Argument Values
By vipvan2000 in forum Advanced JavaReplies: 1Last Post: 02-17-2008, 01:41 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks