Results 1 to 14 of 14
  1. #1
    StokedOnMe is offline Member
    Join Date
    Sep 2009
    Posts
    14
    Rep Power
    0

    Default 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?

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You could do it using reflection but something smells here.
    Could you describe the problem that you are trying to solve with this solution?

  3. #3
    StokedOnMe is offline Member
    Join Date
    Sep 2009
    Posts
    14
    Rep Power
    0

    Default

    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;
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    How many different options can the user input?

  6. #6
    StokedOnMe is offline Member
    Join Date
    Sep 2009
    Posts
    14
    Rep Power
    0

    Default

    The program that originally got me to think about this was a pretty simple sort and search program. It had a class Contact with:
    Java Code:
        private String name;
        private String relation;
        private String bday;
        private String phone;
        private String email;
    and appropriate getter and setter methods.
    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:
    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 )
    etc.
    Same thing with the find methods.

  7. #7
    StokedOnMe is offline Member
    Join Date
    Sep 2009
    Posts
    14
    Rep Power
    0

    Default

    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?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  9. #9
    StokedOnMe is offline Member
    Join Date
    Sep 2009
    Posts
    14
    Rep Power
    0

    Default

    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:
    Java Code:
    if( list[in].getVariable().compareTo( list[in + 1].getVariable() ) > 0 )
    with a previously assigned getVariable() to perform getName(), getEmail(), etc.?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  11. #11
    StokedOnMe is offline Member
    Join Date
    Sep 2009
    Posts
    14
    Rep Power
    0

    Default

    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:
    Java Code:
    double CtoF( double t )
    double CtoK( double t )
    double FtoC( double t )
    etc.
    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.

    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?

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  13. #13
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by StokedOnMe View Post
    ...
    I'm not actually writing this exact program, but I'm really curious about this idea.
    ..
    Like I said it can be done using reflection but it should be avoided. It's not a sign of good design because it violates OOP principles.

  14. #14
    StokedOnMe is offline Member
    Join Date
    Sep 2009
    Posts
    14
    Rep Power
    0

    Default

    Thanks, reflection worked ))))
    Sorry it took me a while to actually try it out. But yeah, that's exactly what I was looking for.
    Thanks again.

Similar Threads

  1. Command line argument
    By denisatandi in forum New To Java
    Replies: 8
    Last Post: 10-17-2012, 12:37 AM
  2. Replies: 4
    Last Post: 03-03-2009, 10:39 AM
  3. Entering a space as argument
    By Arne Bjarne in forum Eclipse
    Replies: 1
    Last Post: 03-02-2009, 01:43 PM
  4. Parsing Argument Values
    By vipvan2000 in forum New To Java
    Replies: 1
    Last Post: 03-06-2008, 12:01 PM
  5. Parsing Argument Values
    By vipvan2000 in forum Advanced Java
    Replies: 1
    Last Post: 02-17-2008, 02:41 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •